1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
use serde::ser::{Serialize, SerializeStruct, Serializer};
/// Specifies how to break the highlighted fragments. Defaults to
/// [`sentence`](UnifiedBoundaryScanner::Sentence).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum UnifiedBoundaryScanner {
/// Break highlighted fragments at the next sentence boundary, as determined by Java’s
/// [BreakIterator](https://docs.oracle.com/javase/8/docs/api/java/text/BreakIterator.html).
/// You can specify the locale to use with `boundary_scanner_locale`.
///
/// > **Warning**<br/>
/// > The `sentence` scanner splits sentences bigger than `fragment_size` at the first word
/// boundary next to `fragment_size`. You can set `fragment_size` to 0 to never split any
/// sentence.
Sentence(Option<String>),
/// Break highlighted fragments at the next word boundary, as determined by Java’s
/// [BreakIterator](https://docs.oracle.com/javase/8/docs/api/java/text/BreakIterator.html).
/// You can specify the locale to use with `boundary_scanner_locale`.
Word(Option<String>),
}
/// Specifies how to break the highlighted fragments. Defaults to
/// [`sentence`](FvhBoundaryScanner::Chars).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum FvhBoundaryScanner {
/// Use the characters specified by `boundary_chars` as highlighting boundaries. The
/// `boundary_max_scan` setting controls how far to scan for boundary characters.
Chars,
/// Break highlighted fragments at the next sentence boundary, as determined by Java’s
/// [BreakIterator](https://docs.oracle.com/javase/8/docs/api/java/text/BreakIterator.html).
/// You can specify the locale to use with `boundary_scanner_locale`.
Sentence(Option<String>),
/// Break highlighted fragments at the next word boundary, as determined by Java’s
/// [BreakIterator](https://docs.oracle.com/javase/8/docs/api/java/text/BreakIterator.html).
/// You can specify the locale to use with `boundary_scanner_locale`.
Word(Option<String>),
}
impl Serialize for UnifiedBoundaryScanner {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Sentence(locale) => match locale {
Some(locale) => {
let mut map =
serializer.serialize_struct("UnifiedBoundaryScannerSentence2", 2)?;
map.serialize_field("boundary_scanner", "sentence")?;
map.serialize_field("boundary_scanner_locale", locale)?;
map.end()
}
None => {
let mut map =
serializer.serialize_struct("UnifiedBoundaryScannerSentence1", 1)?;
map.serialize_field("boundary_scanner", "sentence")?;
map.end()
}
},
Self::Word(locale) => match locale {
Some(locale) => {
let mut map = serializer.serialize_struct("UnifiedBoundaryScannerWord2", 2)?;
map.serialize_field("boundary_scanner", "word")?;
map.serialize_field("boundary_scanner_locale", locale)?;
map.end()
}
None => {
let mut map = serializer.serialize_struct("UnifiedBoundaryScannerWord1", 1)?;
map.serialize_field("boundary_scanner", "word")?;
map.end()
}
},
}
}
}
impl Serialize for FvhBoundaryScanner {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Chars => {
let mut map = serializer.serialize_struct("FvhBoundaryScannerChars", 1)?;
map.serialize_field("boundary_scanner", "chars")?;
map.end()
}
Self::Sentence(locale) => match locale {
Some(locale) => {
let mut map = serializer.serialize_struct("FvhBoundaryScannerSentence2", 2)?;
map.serialize_field("boundary_scanner", "sentence")?;
map.serialize_field("boundary_scanner_locale", locale)?;
map.end()
}
None => {
let mut map = serializer.serialize_struct("FvhBoundaryScannerSentence1", 1)?;
map.serialize_field("boundary_scanner", "sentence")?;
map.end()
}
},
Self::Word(locale) => match locale {
Some(locale) => {
let mut map = serializer.serialize_struct("FvhBoundaryScannerWord2", 2)?;
map.serialize_field("boundary_scanner", "word")?;
map.serialize_field("boundary_scanner_locale", locale)?;
map.end()
}
None => {
let mut map = serializer.serialize_struct("FvhBoundaryScannerWord1", 1)?;
map.serialize_field("boundary_scanner", "word")?;
map.end()
}
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::*;
#[test]
fn serialization() {
assert_serialize(
UnifiedBoundaryScanner::Sentence(None),
json!({ "boundary_scanner": "sentence" }),
);
assert_serialize(
UnifiedBoundaryScanner::Sentence(Some("en-US".into())),
json!({ "boundary_scanner": "sentence", "boundary_scanner_locale": "en-US" }),
);
assert_serialize(
UnifiedBoundaryScanner::Word(None),
json!({ "boundary_scanner": "word" }),
);
assert_serialize(
UnifiedBoundaryScanner::Word(Some("en-US".into())),
json!({ "boundary_scanner": "word", "boundary_scanner_locale": "en-US" }),
);
assert_serialize(
FvhBoundaryScanner::Chars,
json!({ "boundary_scanner": "chars" }),
);
assert_serialize(
FvhBoundaryScanner::Sentence(None),
json!({ "boundary_scanner": "sentence" }),
);
assert_serialize(
FvhBoundaryScanner::Sentence(Some("en-US".into())),
json!({ "boundary_scanner": "sentence", "boundary_scanner_locale": "en-US" }),
);
assert_serialize(
FvhBoundaryScanner::Word(None),
json!({ "boundary_scanner": "word" }),
);
assert_serialize(
FvhBoundaryScanner::Word(Some("en-US".into())),
json!({ "boundary_scanner": "word", "boundary_scanner_locale": "en-US" }),
);
}
}