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
use serde::Serialize;
use std::{
    fmt::{Display, Formatter, Result},
    ops::Index,
};

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct InputPosition {
    inner: Vec<InputPositionType>,
}

impl InputPosition {
    pub fn new() -> Self {
        Self { inner: Vec::new() }
    }

    pub fn new_with_index(&self, index: usize) -> Self {
        let mut new = self.clone();
        new.add_index(index);
        new
    }

    pub fn new_with_key(&self, key: &str) -> Self {
        let mut new = self.clone();
        new.add_key(key);
        new
    }

    pub fn add_index(&mut self, index: usize) {
        self.inner.push(InputPositionType::Index(index))
    }

    pub fn add_key(&mut self, key: &str) {
        self.inner.push(InputPositionType::Key(key.to_string()))
    }

    pub fn add<T: Into<InputPositionType>>(&mut self, key_or_index: T) {
        self.inner.push(key_or_index.into())
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
}

pub fn new() -> InputPosition {
    InputPosition::new()
}

impl Default for InputPosition {
    fn default() -> Self {
        Self::new()
    }
}

impl Display for InputPosition {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.write_str(
            if self.inner.len() == 1 {
                self.inner[0].to_string()
            } else {
                self.inner
                    .iter()
                    .map(|position_type| format!("[{position_type}]"))
                    .collect::<String>()
            }
            .as_str(),
        )
    }
}

impl Index<usize> for InputPosition {
    type Output = InputPositionType;

    fn index(&self, index: usize) -> &Self::Output {
        self.inner.index(index)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum InputPositionType {
    Key(String),
    Index(usize),
}

impl InputPositionType {
    pub fn new<T: Into<Self>>(value: T) -> Self {
        value.into()
    }
}

impl From<usize> for InputPositionType {
    fn from(index: usize) -> Self {
        Self::Index(index)
    }
}

impl From<&str> for InputPositionType {
    fn from(key: &str) -> Self {
        Self::Key(key.to_string())
    }
}

impl From<String> for InputPositionType {
    fn from(key: String) -> Self {
        Self::Key(key)
    }
}

impl Display for InputPositionType {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.write_str(
            match self {
                Self::Key(key) => key.to_string(),
                Self::Index(index) => format!("{index}"),
            }
            .as_str(),
        )
    }
}

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

    #[test]
    fn done() {
        let mut pos = new();
        pos.add_index(0);
        assert_eq!(pos[0], InputPositionType::new(0));
        assert_eq!(format!("{pos}"), "0".to_string());
        pos.add_key("foo");
        assert_eq!(pos[0], InputPositionType::new(0));
        assert_eq!(pos[1], InputPositionType::new("foo"));
        assert_eq!(format!("{pos}"), "[0][foo]".to_string());
    }
}