diff options
Diffstat (limited to 'src/lex.rs')
-rw-r--r-- | src/lex.rs | 33 |
1 files changed, 21 insertions, 12 deletions
@@ -1,5 +1,6 @@ | |||
1 | use crate::utils::FromStaticStr; | 1 | use crate::utils::FromStaticStr; |
2 | 2 | ||
3 | #[derive(Debug)] | ||
3 | pub enum Stanza { | 4 | pub enum Stanza { |
4 | Entry(&'static str), | 5 | Entry(&'static str), |
5 | Defn(&'static str), | 6 | Defn(&'static str), |
@@ -11,7 +12,8 @@ pub enum Stanza { | |||
11 | 12 | ||
12 | impl Stanza { | 13 | impl Stanza { |
13 | fn is_entry(s: &str) -> bool { | 14 | fn is_entry(s: &str) -> bool { |
14 | s.chars().all(|c| c.is_uppercase() || c.is_ascii_whitespace() || "-;'.".contains(c)) | 15 | s.chars() |
16 | .all(|c| c.is_uppercase() || c.is_ascii_whitespace() || "-;'.".contains(c)) | ||
15 | } | 17 | } |
16 | 18 | ||
17 | fn is_defn(s: &str) -> bool { | 19 | fn is_defn(s: &str) -> bool { |
@@ -27,7 +29,9 @@ impl Stanza { | |||
27 | } | 29 | } |
28 | 30 | ||
29 | fn is_bullet(s: &str) -> bool { | 31 | fn is_bullet(s: &str) -> bool { |
30 | s.find('.').map(|idx| s[..idx].chars().all(char::is_numeric)).unwrap_or_default() | 32 | s.find('.') |
33 | .map(|idx| s[..idx].chars().all(char::is_numeric)) | ||
34 | .unwrap_or_default() | ||
31 | } | 35 | } |
32 | 36 | ||
33 | fn is_sub_bullet(s: &str) -> bool { | 37 | fn is_sub_bullet(s: &str) -> bool { |
@@ -49,21 +53,23 @@ impl FromStaticStr for Stanza { | |||
49 | if let Some(first_line) = lines.next() { | 53 | if let Some(first_line) = lines.next() { |
50 | if !first_line.is_empty() { | 54 | if !first_line.is_empty() { |
51 | if Stanza::is_entry(first_line) { | 55 | if Stanza::is_entry(first_line) { |
52 | Ok(Self::Entry(s)) | 56 | Ok(Self::Entry(first_line.trim())) |
53 | } else if Stanza::is_defn(first_line) { | 57 | } else if Stanza::is_defn(first_line) { |
54 | Ok(Self::Defn(s)) | 58 | Ok(Self::Defn(s.strip_prefix("Defn: ").unwrap_or(s).trim())) |
55 | } else if Stanza::is_note(first_line) { | 59 | } else if Stanza::is_note(first_line) { |
56 | Ok(Self::Note(s)) | 60 | Ok(Self::Note(s.strip_prefix("Note: ").unwrap_or(s).trim())) |
57 | } else if Stanza::is_synonym(first_line) { | 61 | } else if Stanza::is_synonym(first_line) { |
58 | Ok(Self::Synonym(s)) | 62 | Ok(Self::Synonym(s.strip_prefix("Syn.").unwrap_or(s))) |
59 | } else if Stanza::is_bullet(first_line) { | 63 | } else if Stanza::is_bullet(first_line) { |
60 | Ok(Self::Bullet(s)) | 64 | Ok(Self::Defn( |
61 | } else if Stanza::is_sub_bullet(first_line) { | 65 | s.trim_start_matches(|c| "0123456789. ".contains(c)), |
62 | Ok(Self::SubBullet(s)) | 66 | )) |
67 | // } else if Stanza::is_sub_bullet(first_line) { | ||
68 | // Ok(Self::SubBullet(s)) | ||
63 | } else { | 69 | } else { |
64 | Err(Self::Err { | 70 | Err(Self::Err { |
65 | data: format!("weird stanza: {}", s), | 71 | data: format!("weird stanza: {}", s), |
66 | }) | 72 | }) |
67 | } | 73 | } |
68 | } else { | 74 | } else { |
69 | Err(Self::Err { | 75 | Err(Self::Err { |
@@ -78,3 +84,6 @@ impl FromStaticStr for Stanza { | |||
78 | } | 84 | } |
79 | } | 85 | } |
80 | 86 | ||
87 | pub fn lex(src: &'static str) -> impl Iterator<Item = Result<Stanza, StanzaLexError>> { | ||
88 | src.split("\n\n").map(Stanza::from_str) | ||
89 | } | ||