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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use std::{cell::RefCell, collections::HashSet};

use crate::{impl_cache, ConfigError};

/// Config key, [ConfigSource](source/trait.ConfigSource.html) use this key to access config properties.
///
/// It's designed for better querying sources.
///
/// Config key has a normalized string representation, it is composed by
/// multiple partial keys, which are [`&str`] or [`usize`]. We use dot(`.`) and
/// square bracket `[]` to separate partial keys in a config key.
///
/// # Partial Key
///
/// ## String Partial Key
///
/// String partial key has regex pattern `[a-z][_a-z0-9]`, usually string partial keys are separated by dot(`.`).
/// For example: `cfg.k1`.
///
/// ## Index Partial Key
///
/// Index partial keys are [`usize`] values, which is around by a pair of square bracket.
/// For example: `[0]`, `[1]`.
///
/// # Config Key
///
/// Config key is composed by partial keys. String partial key can be followed by index partial key for zero or more times.
/// If the string config key is not in head, then it should after a dot(`.`).
/// For example:
///
///   * `cfg.v1`
///   * `cfg.v2[0]`
///   * `cfg.v3[0][1]`
///   * `cfg.v4.key`
///   * `cfg.v5.arr[0]`
///   * `[0]`
///
/// Please notice that `cfg.[0]` is invalid key.
///
pub type ConfigKey<'a> = CacheKey<'a>;

#[derive(Debug)]
pub(crate) struct CacheString {
    current: String,
    mark: usize,
}
thread_local! {
    static BUG: RefCell<CacheString> = RefCell::new(CacheString::new());
}
impl CacheString {
    pub(crate) fn new() -> CacheString {
        Self {
            current: String::with_capacity(10),
            mark: 0,
        }
    }

    #[inline]
    #[allow(single_use_lifetimes)]
    fn push<'a, I: IntoIterator<Item = PartialKey<'a>>>(&mut self, iter: I) -> usize {
        let mark = self.mark;
        self.mark = self.current.len();
        for i in iter {
            i.update_string(&mut self.current);
        }
        mark
    }

    #[inline]
    fn pop(&mut self, mark: usize) {
        self.current.truncate(self.mark);
        self.mark = mark;
    }

    #[inline]
    fn clear(&mut self) {
        self.current.clear();
        self.mark = 0;
    }

    #[inline]
    pub(crate) fn new_key(&mut self) -> CacheKey<'_> {
        CacheKey { cache: self }
    }

    #[inline]
    pub(crate) fn with_key_place<T, F: FnMut(&mut Self) -> Result<T, ConfigError>>(
        f: F,
    ) -> Result<T, ConfigError> {
        BUG.with(move |buf| Self::with_key_buf(buf, f))
    }
}

impl_cache!(CacheString);

/// The implementation of [`ConfigKey`].
#[derive(Debug)]
pub struct CacheKey<'a> {
    cache: &'a mut CacheString,
}

impl Drop for CacheKey<'_> {
    fn drop(&mut self) {
        self.cache.clear();
    }
}

impl<'a> CacheKey<'a> {
    pub(crate) fn push<I: Into<PartialKeyIter<'a>>>(&mut self, iter: I) -> usize {
        self.cache.push(iter.into())
    }
    pub(crate) fn pop(&mut self, mark: usize) {
        self.cache.pop(mark);
    }

    /// As string
    pub(crate) fn as_str(&self) -> &str {
        &self.cache.current
    }
}
impl std::fmt::Display for CacheKey<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}
/// Partial key, plese refer to [`ConfigKey`].
#[allow(single_use_lifetimes)]
#[derive(Debug, PartialEq, Eq)]
pub enum PartialKey<'a> {
    /// String Partial Key.
    Str(&'a str),
    /// Index Partial Key.
    Int(usize),
}

impl PartialKey<'_> {
    #[inline]
    pub(crate) fn update_string(&self, key_long: &mut String) {
        match self {
            PartialKey::Int(i) => {
                key_long.push('[');
                key_long.push_str(&i.to_string());
                key_long.push(']');
            }
            PartialKey::Str(v) => {
                if !key_long.is_empty() {
                    key_long.push('.');
                }
                key_long.push_str(v);
            }
        }
    }
}

#[derive(Debug)]
#[allow(variant_size_differences)]
pub enum PartialKeyIter<'a> {
    Str(std::str::Split<'a, &'a [char]>),
    Int(Option<usize>),
}

impl<'a> Iterator for PartialKeyIter<'a> {
    type Item = PartialKey<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            PartialKeyIter::Str(s) => {
                for v in s {
                    if v.is_empty() {
                        continue;
                    }
                    return Some(if let Ok(i) = v.parse() {
                        PartialKey::Int(i)
                    } else {
                        PartialKey::Str(v)
                    });
                }
                None
            }
            PartialKeyIter::Int(x) => x.take().map(|x| x.into()),
        }
    }
}

/// Partial key collector.
#[derive(Debug)]
pub struct PartialKeyCollector<'a> {
    pub(crate) str_key: HashSet<&'a str>,
    pub(crate) int_key: Option<usize>,
}

#[allow(single_use_lifetimes)]
impl<'a> PartialKeyCollector<'a> {
    pub(crate) fn new() -> Self {
        Self {
            str_key: HashSet::new(),
            int_key: None,
        }
    }

    /// Add index of array.
    pub(crate) fn insert_int(&mut self, key: usize) {
        if let Some(u) = self.int_key {
            if u > key {
                return;
            }
        }
        self.int_key = Some(key + 1);
    }
}

impl<'a> From<&'a str> for PartialKeyIter<'a> {
    fn from(s: &'a str) -> Self {
        PartialKeyIter::Str(s.split(&['.', '[', ']'][..]))
    }
}

impl From<usize> for PartialKey<'_> {
    fn from(s: usize) -> Self {
        PartialKey::Int(s)
    }
}

impl From<usize> for PartialKeyIter<'_> {
    fn from(s: usize) -> Self {
        PartialKeyIter::Int(Some(s))
    }
}

impl<'a> From<&'a String> for PartialKeyIter<'a> {
    fn from(s: &'a String) -> Self {
        s.as_str().into()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    macro_rules! should_eq {
        ($origin:expr => $norm:expr) => {
            let mut che = CacheString::new();
            let mut key = che.new_key();
            key.push($origin);
            assert_eq!(&key.to_string(), $norm);
            let mut chd = CacheString::new();
            let mut kez = chd.new_key();
            kez.push($norm);
            assert_eq!(true, key.as_str() == kez.as_str());
        };
    }

    #[test]
    fn key_test() {
        should_eq!("" => "");
        should_eq!("." => "");
        should_eq!(".." => "");
        should_eq!("[" => "");
        should_eq!("[1]" => "[1]");
        should_eq!("1" => "[1]");
        should_eq!("1[1]" => "[1][1]");
        should_eq!("prefix.prop" => "prefix.prop");
        should_eq!(".prefix.prop"=> "prefix.prop");
        should_eq!("[]prefix.prop"=> "prefix.prop");
        should_eq!("[0]prefix.prop"=> "[0].prefix.prop");
        should_eq!("prefix[0].prop"=> "prefix[0].prop");
        should_eq!("prefix.0.prop"=> "prefix[0].prop");
        should_eq!("hello" => "hello");
    }

    macro_rules! should_ls {
        ($($origin:literal => $norm:literal,)+) => {
            let mut che = CacheString::new();
            let mut key = che.new_key();
            let mut vec = vec![];
            let mut xxx = vec![];
            $(
                xxx.push(key.push($origin));
                assert_eq!($norm, key.as_str());
                vec.push(key.to_string());
            )+
            while let Some(v) = vec.pop() {
                assert_eq!(&v, key.as_str());
                key.pop(xxx.pop().unwrap());
            }
        };
    }

    #[test]
    fn key_push_test() {
        should_ls!(
            "a" => "a",
            "" => "a",
            "b" => "a.b",
            "1" => "a.b[1]",
            "1" => "a.b[1][1]",
            "a.1" => "a.b[1][1].a[1]",
        );
    }
}