Skip to main content

f_office_docx/configuration/
separators.rs

1//! The separators of the numbers of Word's machine (`number_separators`).
2
3use std::fmt;
4use std::str::FromStr;
5
6/// The decimal separator and the group separator that a machine writes in numbers, such as `.`
7/// and `,` in `1,234.5`.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub struct NumberSeparators {
10    decimal: char,
11    group: char,
12}
13
14impl NumberSeparators {
15    /// The separators `decimal` and `group`.
16    ///
17    /// # Errors
18    ///
19    /// Returns an error when they are the same character, or when one is a digit.
20    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    /// The decimal separator.
28    #[must_use]
29    pub const fn decimal(self) -> char {
30        self.decimal
31    }
32
33    /// The group separator, between each group of digits of a whole part.
34    #[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    /// The two characters of the decimal and the group separator, such as `.,`.
44    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    /// The decimal separator, then the group separator.
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        write!(f, "{}{}", self.decimal, self.group)
57    }
58}
59
60/// A text that is no pair of number separators.
61#[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    /// The two characters as one text, such as `".,"`.
85    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}