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
use nom::{error::VerboseError, Finish};
use std::collections::HashMap;
mod parser;
#[derive(Debug, PartialEq)]
pub enum Value {
Bool(bool),
Int(i64),
Float(f64),
String(String),
Array(Vec<Value>),
Object(HashMap<String, Value>),
}
pub fn from_str(input: &str) -> Result<Value, VerboseError<&str>> {
parser::root::<VerboseError<&str>>(input)
.finish()
.map(|(_, o)| o)
}
#[cfg(test)]
mod tests {
use super::Value;
use std::collections::HashMap;
#[test]
fn test_empty() {
let config = "config : {};";
let res = super::from_str(config).unwrap();
assert_eq!(res, Value::Object(HashMap::new()))
}
#[test]
fn test_bool() {
let config = "config : true;";
let res = super::from_str(config).unwrap();
assert_eq!(res, Value::Bool(true))
}
#[test]
fn test_int() {
let config = "config : 123;";
let res = super::from_str(config).unwrap();
assert_eq!(res, Value::Int(123))
}
#[test]
fn test_float() {
let config = "config : 123.1;";
let res = dbg!(super::from_str(config).unwrap());
assert_eq!(res, Value::Float(123.1))
}
#[test]
fn test_string() {
let config = "config : \"Test\";";
let res = dbg!(super::from_str(config).unwrap());
assert_eq!(res, Value::String(String::from("Test")))
}
#[test]
fn test_object() {
let config = "config : { test : 123; };";
let res = dbg!(super::from_str(config).unwrap());
let mut inner = HashMap::new();
inner.insert("test".into(), Value::Int(123));
assert_eq!(res, Value::Object(inner))
}
#[test]
fn test_object_string() {
let config = "config : { test : \"Test\"; };";
let res = dbg!(super::from_str(config).unwrap());
let mut inner = HashMap::new();
inner.insert("test".into(), Value::String(String::from("Test")));
assert_eq!(res, Value::Object(inner))
}
#[test]
fn test_list() {
let config = "config : { test : (1, 2, 3); };";
let res = dbg!(super::from_str(config).unwrap());
let mut inner = HashMap::new();
inner.insert(
"test".into(),
Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)]),
);
assert_eq!(res, Value::Object(inner))
}
#[test]
fn test_array() {
let config = "config : { test : [1, 2, 3]; };";
let res = dbg!(super::from_str(config).unwrap());
let mut inner = HashMap::new();
inner.insert(
"test".into(),
Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)]),
);
assert_eq!(res, Value::Object(inner))
}
#[test]
fn test_vproj_1() {
let config = include_str!("../test/1.vproj");
let res = super::from_str(config).unwrap();
assert!(matches!(res, Value::Object(_)))
}
#[test]
fn test_vproj_2() {
let config = include_str!("../test/2.vproj");
let res = super::from_str(config).unwrap();
assert!(matches!(res, Value::Object(_)))
}
}