f_office_docx/configuration/
separators.rs1use std::fmt;
4use std::str::FromStr;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub struct NumberSeparators {
10 decimal: char,
11 group: char,
12}
13
14impl NumberSeparators {
15 pub fn new(decimal: char, group: char) -> Result<Self, NumberSeparatorsError> {
21 if decimal == group || decimal.is_ascii_digit() || group.is_ascii_digit() {
22 return Err(NumberSeparatorsError(format!("{decimal}{group}")));
23 }
24 Ok(Self { decimal, group })
25 }
26
27 #[must_use]
29 pub const fn decimal(self) -> char {
30 self.decimal
31 }
32
33 #[must_use]
35 pub const fn group(self) -> char {
36 self.group
37 }
38}
39
40impl FromStr for NumberSeparators {
41 type Err = NumberSeparatorsError;
42
43 fn from_str(text: &str) -> Result<Self, NumberSeparatorsError> {
45 let mut chars = text.chars();
46 match (chars.next(), chars.next(), chars.next()) {
47 (Some(decimal), Some(group), None) => Self::new(decimal, group),
48 _ => Err(NumberSeparatorsError(text.to_owned())),
49 }
50 }
51}
52
53impl fmt::Display for NumberSeparators {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 write!(f, "{}{}", self.decimal, self.group)
57 }
58}
59
60#[derive(Clone, Debug, PartialEq, Eq)]
62pub struct NumberSeparatorsError(pub String);
63
64impl fmt::Display for NumberSeparatorsError {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 write!(
67 f,
68 "{:?} are no number separators: write the decimal separator, then another group \
69 separator, neither a digit, such as .,",
70 self.0
71 )
72 }
73}
74
75impl std::error::Error for NumberSeparatorsError {}
76
77impl serde::Serialize for NumberSeparators {
78 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
79 serializer.collect_str(self)
80 }
81}
82
83impl<'de> serde::Deserialize<'de> for NumberSeparators {
84 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
86 let text = String::deserialize(deserializer)?;
87 text.parse().map_err(serde::de::Error::custom)
88 }
89}
90
91#[cfg(feature = "development")]
92impl schemars::JsonSchema for NumberSeparators {
93 fn schema_name() -> std::borrow::Cow<'static, str> {
94 "NumberSeparators".into()
95 }
96
97 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
98 schemars::json_schema!({
99 "description": "The decimal separator, then another group separator, neither a digit, such as \".,\".",
100 "type": "string",
101 "minLength": 2,
102 "maxLength": 2
103 })
104 }
105}