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
use super::types::{
    FormatType, NumberAlign, NumberFormat, Sign, DEFAULT_PRECISION, DEFAULT_TIMEZONE,
};
use crate::FormatError;
use regex::{Captures, Regex};
use std::{fmt, str::FromStr};

impl fmt::Display for FormatError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // let message = match self {
        //     FormatError::CouldNotParseFormatType => "could not parse format type",
        //     FormatError::CouldNotDecomposeCoefficientExponent => {
        //         "could not deomponse coefficient exponent"
        //     }
        //     FormatError::CouldNotCreateRegex => "could not create regex",
        //     FormatError::CouldNotMatchRegex => "regex could not match",
        //     FormatError::InvalidFormat(s) => s,
        // };
        let s = format!("{:?}", self);
        write!(f, "{}", s)
    }
}

impl FromStr for NumberAlign {
    type Err = FormatError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            ">" => Ok(NumberAlign::Right),
            "<" => Ok(NumberAlign::Left),
            "^" => Ok(NumberAlign::Center),
            "=" => Ok(NumberAlign::SignedRight),
            _ => Err(FormatError::CouldNotParseFormatType),
        }
    }
}

impl FromStr for FormatType {
    type Err = FormatError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "e" => Ok(FormatType::Exponent),
            "E" => Ok(FormatType::ExponentUppercase),
            "f" => Ok(FormatType::FixedPoint),
            "s" => Ok(FormatType::SI),
            "%" => Ok(FormatType::Percentage),
            "b" => Ok(FormatType::Binary),
            "o" => Ok(FormatType::Octal),
            "O" => Ok(FormatType::OctalUppercase),
            "d" => Ok(FormatType::Decimal),
            "x" => Ok(FormatType::Hex),
            "X" => Ok(FormatType::HexUppercase),
            _ => Err(FormatError::CouldNotParseFormatType),
        }
    }
}

impl TryFrom<&str> for NumberFormat {
    type Error = FormatError;

    fn try_from(pattern: &str) -> Result<NumberFormat, FormatError> {
        let re =
            Regex::new(r"^(?:(.)?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?([A-Za-z%])?$")
                .map_err(|_| FormatError::CouldNotCreateRegex)?;
        let captures = re.captures(pattern).ok_or(FormatError::CouldNotMatchRegex)?;
        Ok(NumberFormat::from(captures))
    }
}

impl From<Captures<'_>> for NumberFormat {
    /// Create a `NumberFormat` instance from a parsed format pattern string.
    fn from(c: Captures<'_>) -> Self {
        let fill = c.get(1).and_then(|m| m.as_str().chars().next()).unwrap_or(' ');
        let align = c
            .get(2)
            .map(|s| s.as_str().parse().unwrap_or(NumberAlign::Right))
            .unwrap_or(NumberAlign::Right);
        let sign = match c.get(3).map(|m| m.as_str()) {
            Some("-") => Sign::OnlyNegative,
            Some("+") => Sign::Always,
            Some(" ") => Sign::SpaceOrDash,
            _ => Sign::OnlyNegative,
        };
        let type_prefix = matches!(c.get(4).map(|m| m.as_str()), Some("#"));
        let zero_padding = c.get(5).is_some();
        let min_width = c.get(6).map(|m| m.as_str().parse().unwrap_or(0)).unwrap_or(0);
        let commas = matches!(c.get(7).map(|m| m.as_str()), Some(","));
        let precision = c
            .get(8)
            .map(|m| m.as_str().get(1..).unwrap_or_default().parse().unwrap_or(DEFAULT_PRECISION))
            .unwrap_or(DEFAULT_PRECISION);
        let format_type: FormatType =
            c.get(9).and_then(|s| s.as_str().parse().ok()).unwrap_or(FormatType::None);

        let timezone = DEFAULT_TIMEZONE;

        let max_width = usize::MAX;
        let mut spec = Self {
            fill,
            align,
            sign,
            type_prefix,
            zero_padding,
            min_width,
            max_width,
            commas,
            precision,
            format_type,
            timezone,
        };

        // If zero fill is specified, padding goes after sign and before digits.
        if spec.zero_padding || (spec.fill == '0' && spec.align == NumberAlign::SignedRight) {
            spec.zero_padding = true;
            spec.fill = '0';
            spec.align = NumberAlign::SignedRight;
        }

        // Ignore precision for decimal notation.
        if spec.format_type == FormatType::Decimal {
            spec.precision = 0;
        };

        spec
    }
}