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
use icu_plurals::PluralCategory;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::ops::Deref;

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Hash)]
#[serde(tag = "type")]
#[serde(bound(
    serialize = "StrMaybeOwned: Serialize",
    deserialize = "StrMaybeOwned: Deserialize<'de>"
))]
pub enum Token<'a, 'b, StrMaybeOwned>
where
    StrMaybeOwned: Deref<Target = str> + Clone,
{
    #[serde(rename = "content")]
    Content { value: StrMaybeOwned },

    #[serde(rename = "argument")]
    PlainArg { arg: StrMaybeOwned },
    #[serde(rename = "function")]
    FunctionArg {
        arg: StrMaybeOwned,
        key: StrMaybeOwned,
        param: Option<Cow<'b, [Token<'a, 'b, StrMaybeOwned>]>>,
    },

    //   type: 'plural' | 'select' | 'selectordinal';
    #[serde(rename = "plural")]
    Plural {
        arg: StrMaybeOwned,
        cases: Cow<'b, [PluralCase<'a, 'b, StrMaybeOwned>]>,
        #[serde(default, rename = "pluralOffset")]
        plural_offset: Option<i32>,
    },
    #[serde(rename = "select")]
    Select {
        arg: StrMaybeOwned,
        cases: Cow<'b, [SelectCase<'a, 'b, StrMaybeOwned>]>,
        #[serde(default, rename = "pluralOffset")]
        plural_offset: Option<i32>, // TODO: Warn about this - it's unusable
    },
    #[serde(rename = "selectordinal")]
    SelectOrdinal {
        arg: StrMaybeOwned,
        cases: Cow<'b, [PluralCase<'a, 'b, StrMaybeOwned>]>,
        #[serde(default, rename = "pluralOffset")]
        plural_offset: Option<i32>,
    },

    #[serde(rename = "octothorpe")]
    Octothorpe {},
}

// #[serde_as]
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Hash)]
#[serde(bound(
    serialize = "StrMaybeOwned: Serialize",
    deserialize = "StrMaybeOwned: Deserialize<'de>"
))]
pub struct PluralCase<'a, 'b, StrMaybeOwned>
where
    StrMaybeOwned: Deref<Target = str> + Clone,
{
    pub key: PluralCategory,
    pub tokens: Cow<'b, [Token<'a, 'b, StrMaybeOwned>]>,
}

impl<'a, 'b, StrMaybeOwned> TryFrom<SelectCase<'a, 'b, StrMaybeOwned>>
    for PluralCase<'a, 'b, StrMaybeOwned>
where
    StrMaybeOwned: Deref<Target = str> + Clone,
{
    type Error = ();

    fn try_from(value: SelectCase<'a, 'b, StrMaybeOwned>) -> Result<Self, Self::Error> {
        Ok(PluralCase {
            key: PluralCategory::get_for_cldr_bytes(value.key.as_bytes()).ok_or(())?,
            tokens: value.tokens,
        })
    }
}

// #[serde_as]
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Hash)]
#[serde(bound(
    serialize = "StrMaybeOwned: Serialize",
    deserialize = "StrMaybeOwned: Deserialize<'de>"
))]
pub struct SelectCase<'a, 'b, StrMaybeOwned>
where
    StrMaybeOwned: 'a + Deref<Target = str> + Clone,
{
    pub key: StrMaybeOwned,
    pub tokens: Cow<'b, [Token<'a, 'b, StrMaybeOwned>]>,
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum ArgType {
    OrdinalArg,
    PlainArg,
    SelectArg,
    FunctionArg,
}

pub trait TokenSlice<'a, T> {
    fn get_args(&'a self) -> HashMap<&'a T, HashSet<ArgType>>
    where
        T: Deref<Target = str> + Clone,
    {
        let mut args = HashMap::new();

        self.get_args_into(&mut args);
        args
    }
    fn get_args_into(&'a self, args: &mut HashMap<&'a T, HashSet<ArgType>>)
    where
        T: Deref<Target = str> + Clone;
}

impl<'a, T> TokenSlice<'a, T> for [Token<'_, 'a, T>]
where
    T: Deref<Target = str>
        + Clone
        + for<'b> std::cmp::PartialEq<&'b str>
        + std::cmp::Eq
        + std::hash::Hash,
{
    fn get_args_into(&'a self, args: &mut HashMap<&'a T, HashSet<ArgType>>)
    where
        T: Deref<Target = str> + Clone,
    {
        for t in self.iter() {
            match t {
                Token::Content { value: _ } => {}
                Token::PlainArg { arg } => {
                    args.entry(arg).or_default().insert(ArgType::PlainArg);
                }
                Token::FunctionArg {
                    arg,
                    key: _,
                    param: _,
                } => {
                    args.entry(arg).or_default().insert(ArgType::FunctionArg);
                }
                Token::Plural {
                    arg,
                    cases,
                    plural_offset: _,
                }
                | Token::SelectOrdinal {
                    arg,
                    cases,
                    plural_offset: _,
                } => {
                    args.entry(arg).or_default().insert(ArgType::OrdinalArg);
                    for case in cases.iter() {
                        case.tokens.get_args_into(args)
                    }
                }
                Token::Select {
                    arg,
                    cases,
                    plural_offset: _,
                } => {
                    args.entry(arg).or_default().insert(ArgType::SelectArg);
                    for case in cases.iter() {
                        case.tokens.get_args_into(args)
                    }
                }
                Token::Octothorpe {} => {}
            };
        }
    }
}