oxedyne/fe2o3/fe2o3_text/src/phrase.rs
2.8 KiB, 3 runs
created by r1870400018:1107, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_core::prelude::*; |
| 2 | |
| 3 | use std::str; |
| 4 | |
| 5 | #[derive(Clone, Debug, PartialEq)] |
| 6 | pub enum PhraseType { |
| 7 | Word, |
| 8 | HyphenRight, |
| 9 | HyphenLeft, |
| 10 | EndSentence, |
| 11 | } |
| 12 | |
| 13 | #[derive(Clone, Debug, PartialEq)] |
| 14 | pub enum PhraseTag { |
| 15 | BreakBefore, |
| 16 | } |
| 17 | |
| 18 | #[derive(Clone, Debug, Default)] |
| 19 | pub struct PhraseMeta { |
| 20 | pub typ: Option<PhraseType>, |
| 21 | pub tag: Option<PhraseTag>, |
| 22 | pub len: usize, // unicode length |
| 23 | } |
| 24 | |
| 25 | impl PhraseMeta { |
| 26 | |
| 27 | pub fn new() -> Self { |
| 28 | Self { |
| 29 | ..Default::default() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | pub fn len(&self) -> usize { |
| 34 | self.len |
| 35 | } |
| 36 | |
| 37 | pub fn set_type(&mut self, c: char) { |
| 38 | self.typ = match c { |
| 39 | ' ' => Some(PhraseType::Word), |
| 40 | '-' => Some(PhraseType::HyphenRight), |
| 41 | '.' => Some(PhraseType::EndSentence), |
| 42 | _ => None, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | //fn get_type(&self) -> Option<PhraseType> { |
| 47 | // self.typ.clone() |
| 48 | //} |
| 49 | |
| 50 | //pub fn val_ref<'a>(&'a self) -> &'a str { |
| 51 | // &self.val |
| 52 | //} |
| 53 | |
| 54 | //pub fn val_clone(&self) -> String { |
| 55 | // self.val.clone() |
| 56 | //} |
| 57 | } |
| 58 | |
| 59 | #[derive(Clone, Debug)] |
| 60 | pub enum Phrase { |
| 61 | Plain(String), |
| 62 | Classified(String, PhraseMeta), |
| 63 | } |
| 64 | |
| 65 | impl Phrase { |
| 66 | |
| 67 | pub fn push(&mut self, c: char) { |
| 68 | match self { |
| 69 | Self::Plain(s) => s.push(c), |
| 70 | Self::Classified(s, _) => s.push(c), |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pub fn to_val(self) -> String { |
| 75 | match self { |
| 76 | Self::Plain(s) => s, |
| 77 | Self::Classified(s, _) => s, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | pub fn val_ref(&self) -> &str { |
| 82 | match self { |
| 83 | Self::Plain(ref s) => s, |
| 84 | Self::Classified(ref s, _) => s, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub fn get_type(&self) -> Option<PhraseType> { |
| 89 | match self { |
| 90 | Self::Plain(_) => None, |
| 91 | Self::Classified(_, meta) => meta.typ.clone(), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub fn len(&self) -> usize { |
| 96 | match self { |
| 97 | Self::Plain(s) => s.len(), |
| 98 | Self::Classified(_, meta) => meta.len, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn get_tag(&self) -> Option<PhraseTag> { |
| 103 | match self { |
| 104 | Self::Plain(_) => None, |
| 105 | Self::Classified(_, meta) => meta.tag.clone(), |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | pub fn inc_len(&mut self) { |
| 110 | match self { |
| 111 | Self::Plain(_) => {}, |
| 112 | Self::Classified(_, meta) => { |
| 113 | meta.len += 1; |
| 114 | }, |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub fn set_len(&mut self, len: usize) { |
| 119 | match self { |
| 120 | Self::Plain(_) => {}, |
| 121 | Self::Classified(_, meta) => { |
| 122 | meta.len = len; |
| 123 | }, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub fn set_hyphen_left(&mut self) { |
| 128 | match self { |
| 129 | Self::Plain(_) => {}, |
| 130 | Self::Classified(_, meta) => { |
| 131 | meta.typ = Some(PhraseType::HyphenLeft); |
| 132 | }, |
| 133 | } |
| 134 | } |
| 135 | } |