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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::formatter::{new_formatter, Formatter};
use super::{parse, FormatError, Fragment, Values};
use crate::config::SharedConfig;
use crate::errors::*;

use std::str::FromStr;
use std::sync::Arc;

#[derive(Debug, Clone)]
pub struct FormatTemplate(Arc<[TokenList]>);

impl Default for FormatTemplate {
    fn default() -> Self {
        Self(Arc::new([]))
    }
}

#[derive(Debug)]
pub struct TokenList(pub Vec<Token>);

#[derive(Debug)]
pub enum Token {
    Text(String),
    Recursive(FormatTemplate),
    Placeholder {
        name: String,
        formatter: Option<Box<dyn Formatter>>,
    },
    Icon {
        name: String,
    },
}

impl FormatTemplate {
    pub fn contains_key(&self, key: &str) -> bool {
        self.0.iter().any(|token_list| {
            token_list.0.iter().any(|token| match token {
                Token::Placeholder { name, .. } => name == key,
                Token::Recursive(rec) => rec.contains_key(key),
                _ => false,
            })
        })
    }

    pub fn render(
        &self,
        values: &Values,
        config: &SharedConfig,
    ) -> Result<Vec<Fragment>, FormatError> {
        for (i, token_list) in self.0.iter().enumerate() {
            match token_list.render(values, config) {
                Ok(res) => return Ok(res),
                Err(
                    FormatError::PlaceholderNotFound(_)
                    | FormatError::IncompatibleFormatter { .. }
                    | FormatError::NumberOutOfRange(_),
                ) if i != self.0.len() - 1 => (),
                Err(e) => return Err(e),
            }
        }
        Ok(Vec::new())
    }

    pub fn init_intervals(&self, intervals: &mut Vec<u64>) {
        for tl in self.0.iter() {
            for t in &tl.0 {
                match t {
                    Token::Recursive(r) => r.init_intervals(intervals),
                    Token::Placeholder {
                        formatter: Some(f), ..
                    } => {
                        if let Some(i) = f.interval() {
                            intervals.push(i.as_millis() as u64);
                        }
                    }
                    _ => (),
                }
            }
        }
    }
}

impl TokenList {
    pub fn render(
        &self,
        values: &Values,
        config: &SharedConfig,
    ) -> Result<Vec<Fragment>, FormatError> {
        let mut retval = Vec::new();
        let mut cur = Fragment::default();
        for token in &self.0 {
            match token {
                Token::Text(text) => {
                    if cur.metadata.is_default() {
                        cur.text.push_str(text);
                    } else {
                        if !cur.text.is_empty() {
                            retval.push(cur);
                        }
                        cur = text.clone().into();
                    }
                }
                Token::Recursive(rec) => {
                    if !cur.text.is_empty() {
                        retval.push(cur);
                    }
                    retval.extend(rec.render(values, config)?);
                    cur = retval.pop().unwrap_or_default();
                }
                Token::Placeholder { name, formatter } => {
                    let value = values
                        .get(name.as_str())
                        .ok_or_else(|| FormatError::PlaceholderNotFound(name.into()))?;
                    let formatter = formatter
                        .as_ref()
                        .map(Box::as_ref)
                        .unwrap_or_else(|| value.default_formatter());
                    let formatted = formatter.format(&value.inner, config)?;
                    if value.metadata == cur.metadata {
                        cur.text.push_str(&formatted);
                    } else {
                        if !cur.text.is_empty() {
                            retval.push(cur);
                        }
                        cur = Fragment {
                            text: formatted,
                            metadata: value.metadata,
                        };
                    }
                }
                Token::Icon { name } => {
                    let icon = config.get_icon(name, None)?;
                    if cur.metadata.is_default() {
                        cur.text.push_str(&icon);
                    } else {
                        if !cur.text.is_empty() {
                            retval.push(cur);
                        }
                        cur = icon.into();
                    }
                }
            }
        }

        if !cur.text.is_empty() {
            retval.push(cur);
        }

        Ok(retval)
    }
}

impl FromStr for FormatTemplate {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        parse::parse_full(s)
            .and_then(TryInto::try_into)
            .error("Incorrect format template")
    }
}

impl TryFrom<parse::FormatTemplate<'_>> for FormatTemplate {
    type Error = Error;

    fn try_from(value: parse::FormatTemplate) -> Result<Self, Self::Error> {
        value
            .0
            .into_iter()
            .map(TryInto::try_into)
            .collect::<Result<Arc<[_]>>>()
            .map(Self)
    }
}

impl TryFrom<parse::TokenList<'_>> for TokenList {
    type Error = Error;

    fn try_from(value: parse::TokenList) -> Result<Self, Self::Error> {
        value
            .0
            .into_iter()
            .map(TryInto::try_into)
            .collect::<Result<Vec<_>>>()
            .map(Self)
    }
}

impl TryFrom<parse::Token<'_>> for Token {
    type Error = Error;

    fn try_from(value: parse::Token) -> Result<Self, Self::Error> {
        Ok(match value {
            parse::Token::Text(text) => Self::Text(text),
            parse::Token::Placeholder(placeholder) => Self::Placeholder {
                name: placeholder.name.to_owned(),
                formatter: placeholder
                    .formatter
                    .map(|fmt| new_formatter(fmt.name, &fmt.args))
                    .transpose()?,
            },
            parse::Token::Icon(icon) => Self::Icon {
                name: icon.to_owned(),
            },
            parse::Token::Recursive(rec) => Self::Recursive(rec.try_into()?),
        })
    }
}