mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-24 00:54:17 +02:00
Implement classification of token types
This commit is contained in:
parent
4dcab32056
commit
3925cdbec0
1 changed files with 43 additions and 4 deletions
47
src/main.rs
47
src/main.rs
|
|
@ -3,16 +3,37 @@ extern crate ansi_term;
|
|||
use std::io::{BufRead, BufReader};
|
||||
use std::fs::File;
|
||||
use std::{env, process};
|
||||
use std::convert::From;
|
||||
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum LineType {
|
||||
Empty,
|
||||
Title(String),
|
||||
Empty(String),
|
||||
Description(String),
|
||||
ExampleText(String),
|
||||
ExampleCode(String),
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for LineType {
|
||||
/// Convert a string slice to a LineType. Newlines and whitespace are trimmed.
|
||||
fn from(line: &'a str) -> LineType {
|
||||
let trimmed = line.trim();
|
||||
let mut chars = trimmed.chars();
|
||||
match chars.next() {
|
||||
None => LineType::Empty,
|
||||
Some('#') => LineType::Title(trimmed.trim_left_matches(|chr: char| chr == '#' || chr.is_whitespace()).into()),
|
||||
Some('>') => LineType::Description(trimmed.trim_left_matches(|chr: char| chr == '>' || chr.is_whitespace()).into()),
|
||||
Some('-') => LineType::ExampleText(trimmed.trim_left_matches(|chr: char| chr == '-' || chr.is_whitespace()).into()),
|
||||
Some('`') if chars.last() == Some('`') => LineType::ExampleCode(trimmed.trim_matches(|chr: char| chr == '`' || chr.is_whitespace()).into()),
|
||||
_ => LineType::Other(trimmed.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Tokenizer<R: BufRead> {
|
||||
reader: R,
|
||||
current_line: String,
|
||||
|
|
@ -27,12 +48,12 @@ impl<R> Tokenizer<R> where R: BufRead {
|
|||
}
|
||||
}
|
||||
|
||||
fn next(&mut self) -> Option<&str> {
|
||||
fn next(&mut self) -> Option<LineType> {
|
||||
self.current_line.clear();
|
||||
let bytes_read = self.reader.read_line(&mut self.current_line).unwrap();
|
||||
match bytes_read {
|
||||
0 => None,
|
||||
_ => Some(&self.current_line),
|
||||
_ => Some(LineType::from(&self.current_line[..])),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -68,7 +89,25 @@ fn main() {
|
|||
|
||||
// Tokenize and print output
|
||||
while let Some(token) = tokenizer.next() {
|
||||
print!("{}", token);
|
||||
println!("{:?}", token);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::LineType;
|
||||
|
||||
#[test]
|
||||
fn test_linetype_from_str() {
|
||||
assert_eq!(LineType::from(""), LineType::Empty);
|
||||
assert_eq!(LineType::from(" \n \r"), LineType::Empty);
|
||||
assert_eq!(LineType::from("# Hello there"), LineType::Title("Hello there".into()));
|
||||
assert_eq!(LineType::from("> tis a description \n"), LineType::Description("tis a description".into()));
|
||||
assert_eq!(LineType::from("- some command"), LineType::ExampleText("some command".into()));
|
||||
assert_eq!(LineType::from("`$ cargo run`"), LineType::ExampleCode("$ cargo run".into()));
|
||||
assert_eq!(LineType::from("`$ cargo run"), LineType::Other("`$ cargo run".into()));
|
||||
assert_eq!(LineType::from("asdf"), LineType::Other("asdf".into()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue