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
use crate::output::diagnostic;
use crate::output::path;
use crate::output::primitive_data;
use crate::output::tree;
use crate::parse::traversal;
use yaml_rust::yaml::Yaml;
pub type Value = serde_json::value::Value;
pub type Array = Vec<Value>;
pub type Map = serde_json::map::Map<String, Value>;
pub fn yaml_to_json(y: Yaml, path: &path::Path) -> diagnostic::DiagResult<Value> {
match y {
Yaml::Real(ref s) => Ok(Value::Number(
serde_json::value::Number::from_f64(y.as_f64().ok_or_else(|| {
diag!(
path.to_path_buf(),
Error,
YamlParseFailed,
"failed to parse {s} as float"
)
})?)
.ok_or_else(|| {
diag!(
path.to_path_buf(),
Error,
YamlParseFailed,
"{s} float is not supported"
)
})?,
)),
Yaml::Integer(i) => Ok(Value::Number(i.into())),
Yaml::String(s) => Ok(Value::String(s)),
Yaml::Boolean(b) => Ok(Value::Bool(b)),
Yaml::Array(a) => Ok(Value::Array(
a.into_iter()
.enumerate()
.map(|(index, value)| yaml_to_json(value, &path.with_index(index)))
.collect::<diagnostic::DiagResult<Vec<Value>>>()?,
)),
Yaml::Hash(m) => Ok(Value::Object(
m.into_iter()
.map(|(key, value)| {
let key = key
.as_str()
.ok_or_else(|| {
diag!(
path.to_path_buf(),
Error,
YamlParseFailed,
"non-string map keys are not supported"
)
})?
.to_string();
let path = path.with_field(&key);
let value = yaml_to_json(value, &path)?;
Ok((key, value))
})
.collect::<diagnostic::DiagResult<serde_json::value::Map<String, Value>>>()?,
)),
Yaml::Alias(_) => Err(diag!(
path.to_path_buf(),
Error,
YamlParseFailed,
"YAML aliases are not supported"
)),
Yaml::Null => Ok(Value::Null),
Yaml::BadValue => panic!("encountered Yaml::BadValue"),
}
}
impl crate::input::traits::InputNode for Value {
fn type_to_node() -> tree::Node {
tree::NodeType::YamlMap.into()
}
fn data_to_node(&self) -> tree::Node {
match self {
Value::Null => tree::NodeType::YamlPrimitive(primitive_data::PrimitiveData::Null),
Value::Bool(b) => {
tree::NodeType::YamlPrimitive(primitive_data::PrimitiveData::Bool(*b))
}
Value::Number(n) => tree::NodeType::YamlPrimitive(
n.as_u64()
.map(primitive_data::PrimitiveData::Unsigned)
.or_else(|| n.as_i64().map(primitive_data::PrimitiveData::Signed))
.or_else(|| n.as_f64().map(primitive_data::PrimitiveData::Float))
.unwrap(),
),
Value::String(s) => {
tree::NodeType::YamlPrimitive(primitive_data::PrimitiveData::String(s.clone()))
}
Value::Array(_) => tree::NodeType::YamlArray,
Value::Object(_) => tree::NodeType::YamlMap,
}
.into()
}
fn oneof_variant(&self) -> Option<&'static str> {
None
}
fn parse_unknown(&self, context: &mut crate::parse::context::Context<'_>) -> bool {
match self {
Value::Array(array) => {
let mut any = false;
for (index, _) in array.iter().enumerate() {
if !context.field_parsed(index.to_string()) {
traversal::push_yaml_element(array, context, index, true, |_, _| Ok(()));
any = true;
}
}
any
}
Value::Object(object) => {
let mut any = false;
let mut keys: Vec<_> = object.keys().collect();
keys.sort();
for field_name in keys {
if !context.field_parsed(field_name) {
traversal::push_yaml_field(self, context, field_name, true, |_, _| Ok(()))
.unwrap();
any = true;
}
}
any
}
_ => false,
}
}
}