1mod emitter;
51mod parser;
52
53use std::fmt;
54
55pub use parser::YamlError;
56
57#[derive(Clone, Debug, Default, PartialEq)]
64pub struct Mapping {
65 entries: Vec<(Value, Value)>,
66}
67
68impl Mapping {
69 #[must_use]
71 pub const fn new() -> Self {
72 Self {
73 entries: Vec::new(),
74 }
75 }
76
77 #[must_use]
79 pub const fn len(&self) -> usize {
80 self.entries.len()
81 }
82
83 #[must_use]
85 pub const fn is_empty(&self) -> bool {
86 self.entries.is_empty()
87 }
88
89 #[must_use]
91 pub fn get(&self, key: &str) -> Option<&Value> {
92 self.entries
93 .iter()
94 .find(|(k, _)| k.as_str() == Some(key))
95 .map(|(_, v)| v)
96 }
97
98 pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
100 self.entries
101 .iter_mut()
102 .find(|(k, _)| k.as_str() == Some(key))
103 .map(|(_, v)| v)
104 }
105
106 #[must_use]
108 pub fn contains_key(&self, key: &str) -> bool {
109 self.get(key).is_some()
110 }
111
112 pub fn insert(&mut self, key: impl Into<String>, value: Value) -> Option<Value> {
115 let key = key.into();
116 if let Some(slot) = self
117 .entries
118 .iter_mut()
119 .find(|(k, _)| k.as_str() == Some(&key))
120 {
121 return Some(std::mem::replace(&mut slot.1, value));
122 }
123 self.entries.push((Value::String(key), value));
124 None
125 }
126
127 pub fn remove(&mut self, key: &str) -> Option<Value> {
129 let idx = self
130 .entries
131 .iter()
132 .position(|(k, _)| k.as_str() == Some(key))?;
133 Some(self.entries.remove(idx).1)
134 }
135
136 pub(crate) fn push_raw(&mut self, key: Value, value: Value) {
138 self.entries.push((key, value));
139 }
140
141 pub fn iter(&self) -> impl Iterator<Item = (&Value, &Value)> {
143 self.entries.iter().map(|(k, v)| (k, v))
144 }
145
146 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&mut Value, &mut Value)> {
148 self.entries.iter_mut().map(|(k, v)| (k, v))
149 }
150
151 pub fn keys(&self) -> impl Iterator<Item = &str> {
153 self.entries.iter().filter_map(|(k, _)| k.as_str())
154 }
155
156 pub fn values(&self) -> impl Iterator<Item = &Value> {
158 self.entries.iter().map(|(_, v)| v)
159 }
160
161 #[must_use]
163 pub fn entries(&self) -> &[(Value, Value)] {
164 &self.entries
165 }
166}
167
168#[derive(Clone, Debug, PartialEq)]
170pub enum Value {
171 Null,
173 Bool(bool),
175 Int(i64),
177 Float(f64),
179 String(String),
181 Sequence(Vec<Self>),
183 Mapping(Mapping),
185}
186
187impl Value {
188 pub fn parse(text: &str) -> Result<Self, YamlError> {
195 parser::parse(text)
196 }
197
198 #[must_use]
200 pub fn to_yaml_string(&self) -> String {
201 emitter::emit(self)
202 }
203
204 #[must_use]
206 pub fn as_str(&self) -> Option<&str> {
207 match self {
208 Self::String(s) => Some(s),
209 _ => None,
210 }
211 }
212
213 #[must_use]
215 pub const fn as_bool(&self) -> Option<bool> {
216 match self {
217 Self::Bool(b) => Some(*b),
218 _ => None,
219 }
220 }
221
222 #[must_use]
224 pub const fn as_int(&self) -> Option<i64> {
225 match self {
226 Self::Int(i) => Some(*i),
227 _ => None,
228 }
229 }
230
231 #[must_use]
233 pub const fn as_float(&self) -> Option<f64> {
234 match self {
235 Self::Float(f) => Some(*f),
236 _ => None,
237 }
238 }
239
240 #[must_use]
242 pub fn as_sequence(&self) -> Option<&[Self]> {
243 match self {
244 Self::Sequence(s) => Some(s),
245 _ => None,
246 }
247 }
248
249 #[must_use]
251 pub const fn as_mapping(&self) -> Option<&Mapping> {
252 match self {
253 Self::Mapping(m) => Some(m),
254 _ => None,
255 }
256 }
257
258 pub const fn as_mapping_mut(&mut self) -> Option<&mut Mapping> {
260 match self {
261 Self::Mapping(m) => Some(m),
262 _ => None,
263 }
264 }
265
266 pub const fn as_sequence_mut(&mut self) -> Option<&mut Vec<Self>> {
268 match self {
269 Self::Sequence(s) => Some(s),
270 _ => None,
271 }
272 }
273
274 #[must_use]
278 pub const fn is_empty_value(&self) -> bool {
279 match self {
280 Self::Null | Self::Bool(false) | Self::Int(0) => true,
281 Self::String(s) => s.is_empty(),
282 Self::Sequence(s) => s.is_empty(),
283 Self::Mapping(m) => m.is_empty(),
284 _ => false,
285 }
286 }
287
288 #[must_use]
292 pub fn as_display_string(&self) -> Option<String> {
293 match self {
294 Self::String(s) => Some(s.clone()),
295 Self::Bool(b) => Some(b.to_string()),
296 Self::Int(i) => Some(i.to_string()),
297 Self::Float(f) => Some(format!("{f}")),
298 _ => None,
299 }
300 }
301
302 #[must_use]
312 pub fn as_display_str(&self) -> Option<std::borrow::Cow<'_, str>> {
313 match self {
314 Self::String(s) => Some(std::borrow::Cow::Borrowed(s)),
315 Self::Bool(b) => Some(std::borrow::Cow::Owned(b.to_string())),
316 Self::Int(i) => Some(std::borrow::Cow::Owned(i.to_string())),
317 Self::Float(f) => Some(std::borrow::Cow::Owned(format!("{f}"))),
318 _ => None,
319 }
320 }
321}
322
323impl fmt::Display for Value {
324 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
325 f.write_str(&self.to_yaml_string())
326 }
327}
328
329impl From<&str> for Value {
330 fn from(s: &str) -> Self {
331 Self::String(s.to_string())
332 }
333}
334
335impl From<String> for Value {
336 fn from(s: String) -> Self {
337 Self::String(s)
338 }
339}
340
341impl From<bool> for Value {
342 fn from(b: bool) -> Self {
343 Self::Bool(b)
344 }
345}
346
347impl From<i64> for Value {
348 fn from(i: i64) -> Self {
349 Self::Int(i)
350 }
351}
352
353impl<T: Into<Self>> From<Vec<T>> for Value {
354 fn from(v: Vec<T>) -> Self {
355 Self::Sequence(v.into_iter().map(Into::into).collect())
356 }
357}
358
359impl From<Mapping> for Value {
360 fn from(m: Mapping) -> Self {
361 Self::Mapping(m)
362 }
363}
364
365impl std::str::FromStr for Value {
366 type Err = YamlError;
367 fn from_str(s: &str) -> Result<Self, Self::Err> {
368 Self::parse(s)
369 }
370}
371
372impl From<i32> for Value {
373 fn from(i: i32) -> Self {
374 Self::Int(i64::from(i))
375 }
376}
377
378impl From<i16> for Value {
379 fn from(i: i16) -> Self {
380 Self::Int(i64::from(i))
381 }
382}
383
384impl From<i8> for Value {
385 fn from(i: i8) -> Self {
386 Self::Int(i64::from(i))
387 }
388}
389
390impl From<u32> for Value {
391 fn from(u: u32) -> Self {
392 Self::Int(i64::from(u))
393 }
394}
395
396impl From<u16> for Value {
397 fn from(u: u16) -> Self {
398 Self::Int(i64::from(u))
399 }
400}
401
402impl From<u8> for Value {
403 fn from(u: u8) -> Self {
404 Self::Int(i64::from(u))
405 }
406}
407
408impl From<u64> for Value {
409 fn from(u: u64) -> Self {
410 Self::Int(i64::try_from(u).unwrap_or(i64::MAX))
411 }
412}
413
414impl From<usize> for Value {
415 fn from(u: usize) -> Self {
416 Self::Int(i64::try_from(u).unwrap_or(i64::MAX))
417 }
418}
419
420impl From<f64> for Value {
421 fn from(f: f64) -> Self {
422 Self::Float(f)
423 }
424}
425
426impl From<f32> for Value {
427 fn from(f: f32) -> Self {
428 Self::Float(f64::from(f))
429 }
430}
431
432impl From<&String> for Value {
433 fn from(s: &String) -> Self {
434 Self::String(s.clone())
435 }
436}
437
438impl From<std::borrow::Cow<'_, str>> for Value {
439 fn from(s: std::borrow::Cow<'_, str>) -> Self {
440 Self::String(s.into_owned())
441 }
442}
443
444impl From<()> for Value {
445 fn from((): ()) -> Self {
446 Self::Null
447 }
448}
449
450impl<T: Into<Self>> From<Option<T>> for Value {
451 fn from(opt: Option<T>) -> Self {
452 opt.map_or(Self::Null, Into::into)
453 }
454}
455
456impl fmt::Display for Mapping {
457 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
458 f.write_str(&Value::Mapping(self.clone()).to_yaml_string())
459 }
460}
461
462impl IntoIterator for Mapping {
463 type Item = (Value, Value);
464 type IntoIter = std::vec::IntoIter<(Value, Value)>;
465 fn into_iter(self) -> Self::IntoIter {
466 self.entries.into_iter()
467 }
468}
469
470impl<'a> IntoIterator for &'a Mapping {
471 type Item = (&'a Value, &'a Value);
472 type IntoIter = std::iter::Map<
473 std::slice::Iter<'a, (Value, Value)>,
474 fn(&(Value, Value)) -> (&Value, &Value),
475 >;
476 fn into_iter(self) -> Self::IntoIter {
477 const fn map_ref(entry: &(Value, Value)) -> (&Value, &Value) {
478 (&entry.0, &entry.1)
479 }
480 self.entries.iter().map(map_ref)
481 }
482}
483
484impl<'a> IntoIterator for &'a mut Mapping {
485 type Item = (&'a mut Value, &'a mut Value);
486 type IntoIter = std::iter::Map<
487 std::slice::IterMut<'a, (Value, Value)>,
488 fn(&mut (Value, Value)) -> (&mut Value, &mut Value),
489 >;
490 fn into_iter(self) -> Self::IntoIter {
491 const fn map_mut(entry: &mut (Value, Value)) -> (&mut Value, &mut Value) {
492 (&mut entry.0, &mut entry.1)
493 }
494 self.entries.iter_mut().map(map_mut)
495 }
496}
497
498impl FromIterator<(Value, Value)> for Mapping {
499 fn from_iter<T: IntoIterator<Item = (Value, Value)>>(iter: T) -> Self {
500 Self {
501 entries: iter.into_iter().collect(),
502 }
503 }
504}
505
506impl FromIterator<(String, Value)> for Mapping {
507 fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
508 let mut map = Self::new();
509 for (k, v) in iter {
510 map.insert(k, v);
511 }
512 map
513 }
514}
515
516impl<'a> FromIterator<(&'a str, Value)> for Mapping {
517 fn from_iter<T: IntoIterator<Item = (&'a str, Value)>>(iter: T) -> Self {
518 let mut map = Self::new();
519 for (k, v) in iter {
520 map.insert(k, v);
521 }
522 map
523 }
524}
525
526impl Extend<(Value, Value)> for Mapping {
527 fn extend<T: IntoIterator<Item = (Value, Value)>>(&mut self, iter: T) {
528 self.entries.extend(iter);
529 }
530}
531
532impl Extend<(String, Value)> for Mapping {
533 fn extend<T: IntoIterator<Item = (String, Value)>>(&mut self, iter: T) {
534 for (k, v) in iter {
535 self.insert(k, v);
536 }
537 }
538}
539
540impl<'a> Extend<(&'a str, Value)> for Mapping {
541 fn extend<T: IntoIterator<Item = (&'a str, Value)>>(&mut self, iter: T) {
542 for (k, v) in iter {
543 self.insert(k, v);
544 }
545 }
546}