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
use super::*;
use core::fmt::Debug;

mod display;

use crate::{ArgumentTerm, ExpressionNode, StringTextNode};

#[doc = include_str!("readme.md")]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FormatterNode {
    /// The raw string of the number.
    pub terms: Vec<FormatterTerm>,
    /// The range of the node
    pub span: Range<u32>,
}

/// `\r, \u{00FF}, {{, }}`
#[derive(Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FormatterTerm {
    /// `abc \r\u{12}`
    Text {
        /// The unescaped string of the number.
        unescaped: StringTextNode,
    },
    /// `{ datetime, 'yyyy-MM-dd h:mm tt', culture: 'en-us' }`
    ///
    /// equivalent to
    ///
    /// `{ datetime.show(fmt: 'yyyy-MM-dd h:mm tt', culture: 'en-us')}`
    Simple {
        /// The raw string of the number.
        argument: ExpressionKind,
        /// The format arguments
        formatter: Option<StringTextNode>,
    },
    /// `{ datetime, fmt:yyyy-MM-dd h:mm tt, culture:en-us }`
    ///
    /// equivalent to
    ///
    /// `{ datetime.show(fmt: 'yyyy-MM-dd h:mm tt', culture: 'en-us')}`
    Complex {
        /// The raw string of the number.
        argument: ExpressionKind,
        /// The format arguments
        formatters: Vec<ArgumentTerm>,
    },
}

impl ValkyrieNode for FormatterNode {
    fn get_range(&self) -> Range<usize> {
        Range { start: self.span.start as usize, end: self.span.end as usize }
    }
}

impl FormatterNode {
    /// Create a string formatter
    pub fn new(capacity: usize, span: &Range<u32>) -> Self {
        Self { terms: Vec::with_capacity(capacity), span: span.clone() }
    }
}