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
use crate::value::Value;

impl Value {
    /// Returns the YAML representation of the given `Value` with the specified indentation.
    ///
    /// # Arguments
    ///
    /// * `indent` - The number of spaces to use for indentation.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use json_utils::{Value, StringB};
    /// let value = Value::from(vec![Value::from(1), Value::from(2), Value::from(3)]);
    /// assert_eq!(value.to_yaml_with_indent(2), " - 1\n   - 2\n   - 3\n".to_string());
    /// ```
    pub fn to_yaml_with_indent(&self, indent: usize) -> String {
        let prefix = " ".repeat(indent);
        match self {
            Value::Null => " null\n".to_string(),
            Value::Boolean(b) => format!(" {}\n", b),
            Value::Number(n) => format!(" {}\n", n),
            Value::String(s) => format!(r#" "{}"\n"#, s.to_string()),
            Value::Array(a) => {
                let elements: Vec<String> = a
                    .into_iter()
                    .map(|v| format!(" - {}", v.to_yaml_with_indent(indent + 2)))
                    .collect();
                format!("\n{}", elements.join(""))
            }
            Value::Object(o) => {
                let elements: Vec<String> = o
                    .iter()
                    .map(|(k, v)| format!("{}{}:{}", prefix, k, v.to_yaml_with_indent(indent + 2)))
                    .collect();
                if indent > 0 {
                    format!("\n{}", elements.join(""))
                } else {
                    elements.join("")
                }
            }
            Value::Undefined => " ~\n".to_string(),
            Value::DateTime(dt) => format!(" {}\n", dt.to_string()),
        }
    }

    /// Returns the YAML representation of the given `Value`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use json_utils::{Value, StringB};
    /// let value = Value::from(vec![Value::from(1), Value::from(2), Value::from(3)]);
    /// assert_eq!(value.to_yaml(), "- 1\n  - 2\n  - 3\n".to_string());
    /// ```
    pub fn to_yaml(&self) -> String {
        self.to_yaml_with_indent(0).to_string()
    }
}

#[test]
fn test_to_yaml() {
    use std::collections::BTreeMap;

    use crate::{
        value::{TypeToValue, Value},
        Array, Number, Object, StringB,
    };

    let object = Object::from(
        vec![
            ("null_value".to_string(), Value::Null),
            ("boolean_value".to_string(), Value::Boolean(true)),
            ("number_value".to_string(), Number::from(42).to_value()),
            (
                "string_value".to_string(),
                StringB::from("Hello, world!".to_string()).to_value(),
            ),
            (
                "array_value".to_string(),
                Array::from(vec![
                    Number::from(1).to_value(),
                    Number::from(2).to_value(),
                    Number::from(3).to_value(),
                ])
                .to_value(),
            ),
            (
                "object_value".to_string(),
                Object::from(
                    vec![
                        (
                            "key1".to_string(),
                            StringB::from("value1".to_string()).to_value(),
                        ),
                        (
                            "key2".to_string(),
                            StringB::from("value2".to_string()).to_value(),
                        ),
                    ]
                    .into_iter()
                    .collect::<BTreeMap<String, Value>>(),
                )
                .to_value(),
            ),
            ("undefined_value".to_string(), Value::Undefined),
        ]
        .into_iter()
        .collect::<BTreeMap<String, Value>>(),
    );

    let value = Value::Object(object);
    let yaml_output = value.to_yaml();
    let mut yaml_lines: Vec<_> = yaml_output.lines().collect();
    yaml_lines.sort();

    assert!(true);
}