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
pub mod traits;
use std::collections::HashMap;

use pest::Parser;

#[derive(Parser)]
#[grammar = "parser/value.pest"]
struct JSONParser;

use crate::{Error, Number, StringB, Value};
use pest::iterators::Pair;

impl Value {
    /// Parses a string to a `Value`.
    ///
    /// # Arguments
    ///
    /// * `str` - A string slice to parse.
    ///
    /// # Errors
    ///
    /// Returns an `Error` if the string is not parseable.
    pub fn str_to_value(str: &str) -> Result<Value, Error> {
        let value = match JSONParser::parse(Rule::json, str) {
            Ok(mut pairs) => match pairs.next() {
                Some(pair) => Self::parse_value(pair),
                None => return Err(Error::NonParseble),
            },
            Err(msg) => return Err(Error::NonParsebleMsg(msg.to_string())),
        };
        Ok(value)
    }

    /// Parses a `Pair` from `pest` to a `Value`.
    fn parse_value(pair: Pair<Rule>) -> Self {
        match pair.as_rule() {
            Rule::object => {
                let map = pair
                    .into_inner()
                    .map(|pair| {
                        let mut inner_rules = pair.into_inner();
                        let name = inner_rules
                            .next()
                            .unwrap()
                            .into_inner()
                            .next()
                            .unwrap()
                            .as_str()
                            .to_string();
                        let value = Self::parse_value(inner_rules.next().unwrap());
                        (name, value)
                    })
                    .collect::<HashMap<String, Value>>();

                Self::from(map)
            }
            Rule::array => {
                Self::from(pair.into_inner().map(Self::parse_value).collect::<Vec<_>>())
            }
            Rule::string => {
                Self::from(StringB::from(pair.into_inner().next().unwrap().as_str()))
            }
            Rule::number => Self::from(Number::try_from(pair.as_str()).unwrap()),
            Rule::boolean => Self::Boolean(pair.as_str().parse().unwrap()),
            Rule::null => Self::Null,
            Rule::json
            | Rule::EOI
            | Rule::pair
            | Rule::value
            | Rule::inner
            | Rule::char
            | Rule::WHITESPACE => Self::Undefined,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::Object;

    use super::*;

    #[test]
    fn json() {
        let raw = "{
        \"test\": true,
        \"test2\": \"ok\",
        \"test3\": [0, 1]
       }";

        let compare = Value::Object({
            let mut map = HashMap::new();
            map.insert("test".to_string(), Value::Boolean(true));
            map.insert("test2".to_string(), Value::String(StringB::from("ok")));
            map.insert(
                "test3".to_string(),
                Value::from(vec![
                    Value::from(0),
                    Value::from(1),
                ]),
            );
            Object::HashMap(map)
        });

        assert_eq!(Value::str_to_value(raw), Ok(compare));
    }

    #[test]
    fn array() {
        let raw = "[0, true, null, \"ok\"]";

        let compare = {
            let mut list = Vec::new();
            list.push(Value::Number(Number::from(0)));
            list.push(Value::Boolean(true));
            list.push(Value::Null);
            list.push(Value::String(StringB::from("ok")));
            Value::from(list)
        };

        assert_eq!(Value::str_to_value(raw), Ok(compare));
    }

    #[test]
    fn number() {
        let int = "0";
        let float = "1.0";

        assert_eq!(Value::str_to_value(int), Ok(Value::Number(Number::from(0))));
        assert_eq!(Value::str_to_value(float), Ok(Value::Number(Number::from(1.0))));
    }

    #[test]
    fn string() {
        let string = r#""string""#;

        assert_eq!(
            Value::str_to_value(string),
            Ok(Value::String(StringB::from("string")))
        );
    }

    #[test]
    fn null() {
        let null = "null";

        assert_eq!(Value::str_to_value(null), Ok(Value::Null));
    }
    #[test]
    fn boolean() {
        let boolean = "true";

        assert_eq!(Value::str_to_value(boolean), Ok(Value::Boolean(true)));
    }
}