valu3/to/
yaml.rs

1use crate::value::Value;
2
3impl Value {
4    /// Returns the YAML representation of the given `Value` with the specified indentation.
5    ///
6    /// # Arguments
7    ///
8    /// * `indent` - The number of spaces to use for indentation.
9    ///
10    /// # Example
11    ///
12    /// ```no_run
13    /// # use json_utils::{Value, StringB};
14    /// let value = Value::from(vec![Value::from(1), Value::from(2), Value::from(3)]);
15    /// assert_eq!(value.to_yaml_with_indent(2), " - 1\n   - 2\n   - 3\n".to_string());
16    /// ```
17    pub fn to_yaml_with_indent(&self, indent: usize) -> String {
18        let prefix = " ".repeat(indent);
19        match self {
20            Value::Null => " null\n".to_string(),
21            Value::Boolean(b) => format!(" {}\n", b),
22            Value::Number(n) => format!(" {}\n", n),
23            Value::String(s) => format!(r#" "{}"\n"#, s.to_string()),
24            Value::Array(a) => {
25                let elements: Vec<String> = a
26                    .into_iter()
27                    .map(|v| format!(" - {}", v.to_yaml_with_indent(indent + 2)))
28                    .collect();
29                format!("\n{}", elements.join(""))
30            }
31            Value::Object(o) => {
32                let elements: Vec<String> = o
33                    .iter()
34                    .map(|(k, v)| format!("{}{}:{}", prefix, k, v.to_yaml_with_indent(indent + 2)))
35                    .collect();
36                if indent > 0 {
37                    format!("\n{}", elements.join(""))
38                } else {
39                    elements.join("")
40                }
41            }
42            Value::Undefined => " ~\n".to_string(),
43            Value::DateTime(dt) => format!(" {}\n", dt.to_string()),
44        }
45    }
46
47    /// Returns the YAML representation of the given `Value`.
48    ///
49    /// # Example
50    ///
51    /// ```no_run
52    /// # use json_utils::{Value, StringB};
53    /// let value = Value::from(vec![Value::from(1), Value::from(2), Value::from(3)]);
54    /// assert_eq!(value.to_yaml(), "- 1\n  - 2\n  - 3\n".to_string());
55    /// ```
56    pub fn to_yaml(&self) -> String {
57        self.to_yaml_with_indent(0).to_string()
58    }
59}
60
61#[test]
62fn test_to_yaml() {
63    use std::collections::BTreeMap;
64    use crate::prelude::*;
65
66    let object = Object::from(
67        vec![
68            ("null_value".to_string(), Value::Null),
69            ("boolean_value".to_string(), Value::Boolean(true)),
70            ("number_value".to_string(), Number::from(42).to_value()),
71            (
72                "string_value".to_string(),
73                StringB::from("Hello, world!".to_string()).to_value(),
74            ),
75            (
76                "array_value".to_string(),
77                Array::from(vec![
78                    Number::from(1).to_value(),
79                    Number::from(2).to_value(),
80                    Number::from(3).to_value(),
81                ])
82                .to_value(),
83            ),
84            (
85                "object_value".to_string(),
86                Object::from(
87                    vec![
88                        (
89                            "key1".to_string(),
90                            StringB::from("value1".to_string()).to_value(),
91                        ),
92                        (
93                            "key2".to_string(),
94                            StringB::from("value2".to_string()).to_value(),
95                        ),
96                    ]
97                    .into_iter()
98                    .collect::<BTreeMap<String, Value>>(),
99                )
100                .to_value(),
101            ),
102            ("undefined_value".to_string(), Value::Undefined),
103        ]
104        .into_iter()
105        .collect::<BTreeMap<String, Value>>(),
106    );
107
108    let value = Value::Object(object);
109    let yaml_output = value.to_yaml();
110    let mut yaml_lines: Vec<_> = yaml_output.lines().collect();
111    yaml_lines.sort();
112
113    assert!(true);
114}