fast-yaml-cli 0.6.5

Fast YAML command-line processor with validation and linting
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use anyhow::{Context, Result};
use fast_yaml_core::{Emitter, Parser, Value};
use serde_json;

use crate::cli::ConvertFormat;
use crate::config::CommonConfig;
use crate::io::{InputSource, OutputWriter};

/// Convert command implementation
pub struct ConvertCommand {
    config: CommonConfig,
    target_format: ConvertFormat,
    pretty: bool,
}

impl ConvertCommand {
    pub const fn new(config: CommonConfig, target_format: ConvertFormat, pretty: bool) -> Self {
        Self {
            config,
            target_format,
            pretty,
        }
    }

    /// Execute convert command
    pub fn execute(&self, input: &InputSource, output: &OutputWriter) -> Result<()> {
        match self.target_format {
            ConvertFormat::Json => self.yaml_to_json(input, output),
            ConvertFormat::Yaml => self.json_to_yaml(input, output),
        }
    }

    /// Convert YAML to JSON
    fn yaml_to_json(&self, input: &InputSource, output: &OutputWriter) -> Result<()> {
        // Parse all YAML documents to support multi-document streams
        let docs = Parser::parse_all(input.as_str()).context("Failed to parse YAML")?;

        if docs.is_empty() {
            return Err(anyhow::anyhow!("Empty YAML document"));
        }

        let json_value = if docs.len() == 1 {
            // Single document: preserve existing behaviour (plain object/value)
            value_to_json(&docs[0])?
        } else {
            // Multi-document stream: output a JSON array
            let arr: Result<Vec<_>> = docs.iter().map(value_to_json).collect();
            serde_json::Value::Array(arr?)
        };

        // Serialize to JSON
        let mut json_string = if self.pretty {
            serde_json::to_string_pretty(&json_value).context("Failed to serialize JSON")?
        } else {
            serde_json::to_string(&json_value).context("Failed to serialize JSON")?
        };

        // Add trailing newline for JSON
        json_string.push('\n');

        // Write output
        output.write(&json_string)?;

        Ok(())
    }

    /// Convert JSON to YAML
    #[allow(clippy::unused_self)]
    fn json_to_yaml(&self, input: &InputSource, output: &OutputWriter) -> Result<()> {
        // Parse JSON
        let json_value: serde_json::Value =
            serde_json::from_str(input.as_str()).context("Failed to parse JSON")?;

        // Convert to YAML Value
        let yaml_value = json_to_value(&json_value)?;

        // Emit YAML
        let yaml_string = Emitter::emit_str(&yaml_value).context("Failed to emit YAML")?;

        // Write output
        output.write(&yaml_string)?;

        Ok(())
    }
}

/// Coerce a YAML scalar key to its string representation for JSON output.
///
/// JSON only supports string keys. Scalar YAML keys are converted to their
/// canonical string form: null -> "null", bool -> "true"/"false",
/// integers and floats -> their decimal string representation.
///
/// # Errors
///
/// Returns an error for non-scalar key types (mappings, sequences, aliases)
/// that have no meaningful string representation.
fn yaml_key_to_string(key: &Value) -> Result<String> {
    use Value as YValue;
    use fast_yaml_core::value::ScalarOwned;

    match key {
        YValue::Value(scalar) => Ok(match scalar {
            ScalarOwned::Null => "null".to_string(),
            ScalarOwned::Boolean(b) => b.to_string(),
            ScalarOwned::Integer(i) => i.to_string(),
            ScalarOwned::FloatingPoint(f) => f.to_string(),
            ScalarOwned::String(s) => s.clone(),
        }),
        _ => Err(anyhow::anyhow!(
            "Unsupported YAML map key type: only scalar keys (string, number, boolean, null) \
             can be converted to JSON"
        )),
    }
}

/// Convert `fast_yaml_core::Value` to `serde_json::Value`
fn value_to_json(value: &Value) -> Result<serde_json::Value> {
    use Value as YValue;
    use fast_yaml_core::value::ScalarOwned;
    use serde_json::Value as JValue;

    Ok(match value {
        YValue::Value(scalar) => match scalar {
            ScalarOwned::Null => JValue::Null,
            ScalarOwned::Boolean(b) => JValue::Bool(*b),
            ScalarOwned::Integer(i) => JValue::Number((*i).into()),
            ScalarOwned::FloatingPoint(f) => serde_json::Number::from_f64(f.0)
                .map(JValue::Number)
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "YAML value '{f}' cannot be represented in JSON \
                     (JSON does not support infinity/NaN). \
                     Consider replacing with a numeric sentinel value."
                    )
                })?,
            ScalarOwned::String(s) => JValue::String(s.clone()),
        },
        YValue::Sequence(arr) => {
            let json_arr: Result<Vec<_>> = arr.iter().map(value_to_json).collect();
            JValue::Array(json_arr?)
        }
        YValue::Mapping(map) => {
            let mut json_map = serde_json::Map::new();
            for (k, v) in map {
                let key = yaml_key_to_string(k)?;
                json_map.insert(key, value_to_json(v)?);
            }
            JValue::Object(json_map)
        }
        YValue::Alias(_) => {
            anyhow::bail!("YAML aliases are not supported in JSON conversion");
        }
        YValue::BadValue => {
            anyhow::bail!("Invalid YAML value encountered");
        }
        YValue::Representation(s, _, _) => {
            // Try to convert the representation string to appropriate JSON type
            JValue::String(s.clone())
        }
        YValue::Tagged(_, inner) => {
            // Ignore the tag and convert the inner value
            value_to_json(inner)?
        }
    })
}

/// Convert `serde_json::Value` to `fast_yaml_core::Value`
fn json_to_value(json: &serde_json::Value) -> Result<Value> {
    use Value as YValue;
    use fast_yaml_core::Map;
    use fast_yaml_core::value::ScalarOwned;
    use serde_json::Value as JValue;

    Ok(match json {
        JValue::Null => YValue::Value(ScalarOwned::Null),
        JValue::Bool(b) => YValue::Value(ScalarOwned::Boolean(*b)),
        JValue::Number(n) => {
            use ordered_float::OrderedFloat;
            use saphyr_parser::ScalarStyle;
            // With the `arbitrary_precision` serde_json feature, `as_str()` returns the
            // original JSON token (e.g. "1.0", "1.23e10", "42"). Use it to distinguish
            // floats (contain '.' or 'e'/'E') from integers so that `1.0` is preserved
            // as a floating-point YAML scalar rather than being coerced to integer `1`.
            let raw = n.as_str();
            let is_float = raw.contains('.') || raw.contains('e') || raw.contains('E');
            if is_float {
                // Validate the value is representable, then store the original JSON token
                // as a plain scalar so the YAML output preserves the float notation.
                let _ = n.as_f64().ok_or_else(|| {
                    anyhow::anyhow!("Float value out of representable range: {n}")
                })?;
                YValue::Representation(raw.to_string(), ScalarStyle::Plain, None)
            } else if let Some(i) = n.as_i64() {
                YValue::Value(ScalarOwned::Integer(i))
            } else if let Some(f) = n.as_f64() {
                YValue::Value(ScalarOwned::FloatingPoint(OrderedFloat(f)))
            } else {
                anyhow::bail!("Unsupported number type: {n}");
            }
        }
        JValue::String(s) => YValue::Value(ScalarOwned::String(s.clone())),
        JValue::Array(arr) => {
            let yaml_arr: Result<Vec<_>> = arr.iter().map(json_to_value).collect();
            YValue::Sequence(yaml_arr?)
        }
        JValue::Object(map) => {
            let mut yaml_map = Map::new();
            for (k, v) in map {
                yaml_map.insert(
                    YValue::Value(ScalarOwned::String(k.clone())),
                    json_to_value(v)?,
                );
            }
            YValue::Mapping(yaml_map)
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::CommonConfig;
    use crate::io::input::InputOrigin;

    #[test]
    fn test_yaml_to_json() {
        let input = InputSource {
            content: "name: test\nvalue: 123".to_string(),
            origin: InputOrigin::Stdin,
        };

        let temp_dir = tempfile::tempdir().unwrap();
        let temp_path = temp_dir.path().join("output.json");
        let output = OutputWriter::from_args(Some(temp_path.clone()), false, None).unwrap();

        let config = CommonConfig::new();
        let cmd = ConvertCommand::new(config, ConvertFormat::Json, true);
        let result = cmd.execute(&input, &output);
        if let Err(e) = &result {
            eprintln!("Execute error: {e}");
        }
        assert!(result.is_ok());

        let json_str = std::fs::read_to_string(&temp_path)
            .unwrap_or_else(|e| panic!("Failed to read {temp_path:?}: {e}"));
        assert!(!json_str.is_empty(), "Output file is empty!");
        let json: serde_json::Value = serde_json::from_str(&json_str)
            .unwrap_or_else(|e| panic!("Failed to parse JSON from '{json_str}': {e}"));
        assert_eq!(json["name"], "test");
        assert_eq!(json["value"], 123);
    }

    #[test]
    fn test_json_to_yaml() {
        let input = InputSource {
            content: r#"{"name": "test", "value": 123}"#.to_string(),
            origin: InputOrigin::Stdin,
        };

        let temp_dir = tempfile::tempdir().unwrap();
        let temp_path = temp_dir.path().join("output.yaml");
        let output = OutputWriter::from_args(Some(temp_path.clone()), false, None).unwrap();

        let config = CommonConfig::new();
        let cmd = ConvertCommand::new(config, ConvertFormat::Yaml, true);
        assert!(cmd.execute(&input, &output).is_ok());

        let yaml_str = std::fs::read_to_string(&temp_path).unwrap();
        assert!(yaml_str.contains("name:"));
        assert!(yaml_str.contains("value:"));
    }

    #[test]
    fn test_value_to_json_simple() {
        let yaml = "name: test";
        let value = Parser::parse_str(yaml).unwrap().unwrap();
        let json = value_to_json(&value).unwrap();

        assert_eq!(json["name"], "test");
    }

    #[test]
    fn test_json_to_value_simple() {
        let json_str = r#"{"name": "test"}"#;
        let json: serde_json::Value = serde_json::from_str(json_str).unwrap();
        let yaml = json_to_value(&json).unwrap();

        match yaml {
            Value::Mapping(map) => {
                assert_eq!(map.len(), 1);
            }
            _ => panic!("Expected Mapping"),
        }
    }

    #[test]
    fn test_invalid_yaml_to_json() {
        let input = InputSource {
            content: "invalid: [".to_string(),
            origin: InputOrigin::Stdin,
        };

        let output = OutputWriter::stdout();

        let config = CommonConfig::new();
        let cmd = ConvertCommand::new(config, ConvertFormat::Json, true);
        assert!(cmd.execute(&input, &output).is_err());
    }

    #[test]
    fn test_invalid_json_to_yaml() {
        let input = InputSource {
            content: "{invalid json}".to_string(),
            origin: InputOrigin::Stdin,
        };

        let output = OutputWriter::stdout();

        let config = CommonConfig::new();
        let cmd = ConvertCommand::new(config, ConvertFormat::Yaml, true);
        assert!(cmd.execute(&input, &output).is_err());
    }

    #[test]
    fn test_multi_document_yaml_to_json() {
        let input = InputSource {
            content: "---\nfoo: 1\n---\nbar: 2\n---\nbaz: 3\n".to_string(),
            origin: InputOrigin::Stdin,
        };

        let temp_dir = tempfile::tempdir().unwrap();
        let temp_path = temp_dir.path().join("output.json");
        let output = OutputWriter::from_args(Some(temp_path.clone()), false, None).unwrap();

        let config = CommonConfig::new();
        let cmd = ConvertCommand::new(config, ConvertFormat::Json, false);
        assert!(cmd.execute(&input, &output).is_ok());

        let json_str = std::fs::read_to_string(&temp_path).unwrap();
        let json: serde_json::Value = serde_json::from_str(json_str.trim()).unwrap();
        assert!(
            json.is_array(),
            "Expected JSON array for multi-document stream"
        );
        let arr = json.as_array().unwrap();
        assert_eq!(arr.len(), 3);
        assert_eq!(arr[0]["foo"], 1);
        assert_eq!(arr[1]["bar"], 2);
        assert_eq!(arr[2]["baz"], 3);
    }

    #[test]
    fn test_yaml_inf_nan_to_json_gives_clear_error() {
        for yaml in &["val: .inf", "val: -.inf", "val: .nan"] {
            let input = InputSource {
                content: (*yaml).to_string(),
                origin: InputOrigin::Stdin,
            };
            let output = OutputWriter::stdout();
            let config = CommonConfig::new();
            let cmd = ConvertCommand::new(config, ConvertFormat::Json, false);
            let err = cmd.execute(&input, &output).unwrap_err();
            let msg = err.to_string();
            assert!(
                msg.contains("cannot be represented in JSON"),
                "expected descriptive error, got: {msg}"
            );
        }
    }

    #[test]
    fn test_json_float_preserves_type() {
        let input = InputSource {
            content: r#"{"whole_float": 1.0, "sci": 1.23e10, "integer": 42}"#.to_string(),
            origin: InputOrigin::Stdin,
        };
        let temp_dir = tempfile::tempdir().unwrap();
        let temp_path = temp_dir.path().join("output.yaml");
        let output = OutputWriter::from_args(Some(temp_path.clone()), false, None).unwrap();
        let config = CommonConfig::new();
        let cmd = ConvertCommand::new(config, ConvertFormat::Yaml, false);
        assert!(cmd.execute(&input, &output).is_ok());

        let yaml_str = std::fs::read_to_string(&temp_path).unwrap();
        // 1.0 must not become bare integer "1"
        assert!(
            yaml_str.contains("whole_float: 1.0"),
            "expected 'whole_float: 1.0' in: {yaml_str}"
        );
        // integer stays integer
        assert!(
            yaml_str.contains("integer: 42"),
            "expected 'integer: 42' in: {yaml_str}"
        );
    }

    #[test]
    fn test_explicit_int_tag_float_to_json() {
        let input = InputSource {
            content: "val: !!int 3.14".to_string(),
            origin: InputOrigin::Stdin,
        };
        let temp_dir = tempfile::tempdir().unwrap();
        let temp_path = temp_dir.path().join("output.json");
        let output = OutputWriter::from_args(Some(temp_path.clone()), false, None).unwrap();
        let config = CommonConfig::new();
        let cmd = ConvertCommand::new(config, ConvertFormat::Json, false);
        assert!(cmd.execute(&input, &output).is_ok());
        let json_str = std::fs::read_to_string(&temp_path).unwrap();
        let json: serde_json::Value = serde_json::from_str(json_str.trim()).unwrap();
        assert_eq!(json["val"], 3, "!!int 3.14 should truncate to integer 3");
    }

    #[test]
    fn test_value_to_json_null_key() {
        let yaml = "null: value";
        let value = Parser::parse_str(yaml).unwrap().unwrap();
        let json = value_to_json(&value).unwrap();
        assert_eq!(json["null"], "value");
    }

    #[test]
    fn test_value_to_json_bool_key() {
        let yaml = "true: yes_value";
        let value = Parser::parse_str(yaml).unwrap().unwrap();
        let json = value_to_json(&value).unwrap();
        assert_eq!(json["true"], "yes_value");
    }

    #[test]
    fn test_value_to_json_integer_key() {
        let yaml = "42: answer";
        let value = Parser::parse_str(yaml).unwrap().unwrap();
        let json = value_to_json(&value).unwrap();
        assert_eq!(json["42"], "answer");
    }
}