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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! Permits to load YAML data into a [Doc](crate::ig::docs::Doc).
//!
//! Wraps the given YAML data and returns it as **Doc**.
//!
//! # Examples
//!
//! Loading an object into a doc:
//! ```
//! # use yaml_rust::YamlLoader;
//! # use jupiter::ig::yaml::hash_to_doc;
//! let input = "
//! a_string: 'Test'
//! a_map:
//! inner_key: 42
//! a_list:
//! - 1
//! - 2
//! - test: Plain String
//! a_bool: true
//! ";
//!
//! let yaml = &YamlLoader::load_from_str(input).unwrap()[0];
//! let doc = hash_to_doc(yaml.as_hash().unwrap(), |_| true).unwrap();
//!
//! assert_eq!(
//! yaml["a_string"].as_str(),
//! doc.root().query("a_string").as_str()
//! );
//! assert_eq!(
//! yaml["a_list"][0].as_i64(),
//! doc.root().query("a_list").at(0).as_int()
//! );
//! assert_eq!(
//! yaml["a_list"][1].as_i64(),
//! doc.root().query("a_list").at(1).as_int()
//! );
//! assert_eq!(
//! yaml["a_bool"].as_bool().unwrap(),
//! doc.root().query("a_bool").as_bool()
//! );
//! assert_eq!(
//! yaml["a_map"]["inner_key"].as_str(),
//! doc.root().query("a_map.inner_key").as_str()
//! );
//! ```
//!
//! Loading a list into a doc:
//! ```
//! # use yaml_rust::YamlLoader;
//! # use jupiter::ig::yaml::{hash_to_doc, list_to_doc};
//! let input = "
//! a_string: 'Test'
//! _skipped: true
//! ---
//! a_string: 'Test1'
//! ---
//! a_string: 'Test2'
//! ---
//! 42
//! ";
//!
//! let yaml = &YamlLoader::load_from_str(input).unwrap();
//! let doc = list_to_doc(yaml, | key | !key.starts_with("_")).unwrap();
//!
//! assert_eq!(doc.root().len(), 4);
//! assert_eq!(yaml[0]["a_string"].as_str(), doc.root().at(0).query("a_string").as_str());
//! // Ensure that keys starting with _ are skipped (as we use !key.starts_with("_") as filter)...
//! assert_eq!(false, doc.root().at(0).query("_skipped").as_bool());
//! assert_eq!(yaml[1]["a_string"].as_str(), doc.root().at(1).query("a_string").as_str());
//! assert_eq!(yaml[2]["a_string"].as_str(), doc.root().at(2).query("a_string").as_str());
//! assert_eq!(yaml[3].as_i64(), doc.root().at(3).as_int());
//! ```
//!
use LinkedHashMap;
use Yaml;
use crate;
use crateDoc;
/// Transforms a YAML hash (object) into a [Doc](crate::ig::docs::Doc).
///
/// The generated **Doc** will have an object as its root node. The filter lambda can be used
/// to skip (ignore) certain keys (e.g. all starting with an underscore). If `true` is returned,
/// the key is accepted, otherwise it is skipped.
///
/// # Errors
/// This will return an error if we're running out of symbols (if there are more than 2^31-1
/// distinct keys in the hash or one of its children).
///
/// # Example
///
/// ```
/// # use yaml_rust::YamlLoader;
/// # use jupiter::ig::yaml::hash_to_doc;
/// let input = "
/// a_string: 'Test'
/// a_map:
/// inner_key: 42
/// a_list:
/// - 1
/// - 2
/// - test: Plain String
/// a_bool: true
/// ";
///
/// let yaml = &YamlLoader::load_from_str(input).unwrap()[0];
/// let doc = hash_to_doc(yaml.as_hash().unwrap(), |_| true).unwrap();
///
/// assert_eq!(doc.root().query("a_string").as_str().unwrap(), "Test");
/// ```
/// Transforms the given list of YAML objects into a [Doc](crate::ig::docs::Doc).
///
/// The generated **Doc** will have a list as its root node. The filter lambda can be used
/// to skip (ignore) certain keys (e.g. all starting with an underscore). If `true` is returned,
/// the key is accepted, otherwise it is skipped.
///
/// # Errors
/// This will return an error if we're running out of symbols (if there are more than 2^31-1
/// distinct keys in the hash or one of its children).
///
/// # Example
///
/// ```
/// # use yaml_rust::YamlLoader;
/// # use jupiter::ig::yaml::{hash_to_doc, list_to_doc};
/// let input = "
/// a_string: 'Test'
/// ---
/// a_string: 'Test1'
/// ---
/// a_string: 'Test2'
/// ---
/// 42
/// ---
/// 'text'
/// ---
/// true
/// ";
///
/// let yaml = &YamlLoader::load_from_str(input).unwrap();
/// let doc = list_to_doc(yaml, |_| true).unwrap();
///
/// assert_eq!(doc.root().len(), 6);
/// assert_eq!(doc.root().at(1).query("a_string").as_str().unwrap(), "Test1");
/// assert_eq!(doc.root().at(3).as_int().unwrap(), 42);
/// assert_eq!(doc.root().at(4).as_str().unwrap(), "text");
/// assert_eq!(doc.root().at(5).as_bool(), true);
/// ```