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
use litl::Litl;
use unicode_segmentation::UnicodeSegmentation;
use crate::Value;

#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum Input {
    Plain(Litl),
    Value(Value),
    CollabText(String),
    CollabList(Vec<Input>),
    CollabMap(Vec<(Input, Input)>),
}

impl Input {
    pub fn collab_list<I: IntoIterator<Item = Input>>(iter: I) -> Self {
        Input::CollabList(iter.into_iter().collect())
    }

    pub fn collab_map<I: IntoIterator<Item = (Input, Input)>>(iter: I) -> Self {
        Input::CollabMap(
            iter.into_iter()
                .collect(),
        )
    }

    pub fn to_litl(&self) -> Litl {
        match self {
            Input::Plain(litl) => litl.clone(),
            Input::Value(value) => value.to_litl(),
            Input::CollabText(string) => Litl::dict([
                (Litl::string("ranges"), Litl::array([])),
                (
                    Litl::string("graphemes"),
                    Litl::array(
                        string
                            .graphemes(true)
                            .map(Litl::string)
                            .collect::<Vec<_>>(),
                    ),
                ),
            ]),
            Input::CollabList(items) => Litl::array(items.iter().map(|item| item.to_litl())),
            Input::CollabMap(map) => {
                Litl::dict(map.iter().map(|(k, v)| (k.to_litl(), v.to_litl())))
            }
        }
    }
}

impl From<Litl> for Input {
    fn from(val: Litl) -> Self {
        Input::Plain(val)
    }
}

impl From<&str> for Input {
    fn from(string: &str) -> Self {
        Input::Plain(Litl::string(string))
    }
}

impl From<usize> for Input {
    fn from(int: usize) -> Self {
        Input::Plain(Litl::integer(int as i64))
    }
}