-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfmt_yaml_stream.rs
43 lines (36 loc) · 1.36 KB
/
fmt_yaml_stream.rs
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
// RCL -- A reasonable configuration language.
// Copyright 2024 Ruud van Asseldonk
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// A copy of the License has been included in the root of the repository.
//! Formatter that prints list elements prefixed by `---` YAML document separators.
use crate::error::{IntoError, PathElement, Result};
use crate::fmt_json::Formatter;
use crate::markup::Markup;
use crate::pprint::Doc;
use crate::runtime::Value;
use crate::source::Span;
/// Render a value in YAML stream format.
pub fn format_yaml_stream(caller: Span, v: &Value) -> Result<Doc> {
let elements = match v {
Value::List(xs) => xs,
_ => {
return caller
.error("To format as YAML stream, the top-level value must be a list.")
.err()
}
};
let mut formatter = Formatter::new(caller);
let mut parts = Vec::new();
for (i, element) in elements.iter().enumerate() {
if !parts.is_empty() {
parts.push(Doc::HardBreak)
}
parts.push(Doc::str("---").with_markup(Markup::Comment));
parts.push(Doc::HardBreak);
formatter.path.push(PathElement::Index(i));
parts.push(formatter.value(element)?);
formatter.path.pop();
}
Ok(Doc::Concat(parts))
}