/// Set 'compact inline notation' on or off, as described for block /// [sequences](http://www.yaml.org/spec/1.2/spec.html#id2797382) /// and /// [mappings](http://www.yaml.org/spec/1.2/spec.html#id2798057). /// /// In this form, blocks cannot have any properties (such as anchors /// or tags), which should be OK, because this emitter doesn't /// (currently) emit those anyways. pubfn compact(&mutself, compact: bool) { self.compact = compact;
}
/// Determine if this emitter is using 'compact inline notation'. #[must_use] pubfn is_compact(&self) -> bool { self.compact
}
/// Determine if this emitter will emit multiline strings when appropriate. #[must_use] pubfn is_multiline_strings(&self) -> bool { self.multiline_strings
}
/// Dump Yaml to an output stream. /// # Errors /// Returns `EmitError` when an error occurs. pubfn dump(&mutself, doc: &Yaml) -> EmitResult { // write DocumentStart
writeln!(self.writer, "---")?; self.level = -1; self.emit_node(doc)
}
fn emit_literal_block(&mutself, v: &str) -> EmitResult { let ends_with_newline = v.ends_with('\n'); if ends_with_newline { self.writer.write_str("|")?;
} else { self.writer.write_str("|-")?;
}
self.level += 1; // lines() will omit the last line if it is empty. for line in v.lines() {
writeln!(self.writer)?; self.write_indent()?; // It's literal text, so don't escape special chars. self.writer.write_str(line)?;
} self.level -= 1;
Ok(())
}
/// Emit a yaml as a hash or array value: i.e., which should appear /// following a ":" or "-", either after a space, or on a new line. /// If `inline` is true, then the preceding characters are distinct /// and short enough to respect the compact flag. fn emit_val(&mutself, inline: bool, val: &Yaml) -> EmitResult { match *val {
Yaml::Array(ref v) => { if (inline && self.compact) || v.is_empty() {
write!(self.writer, " ")?;
} else {
writeln!(self.writer)?; self.level += 1; self.write_indent()?; self.level -= 1;
} self.emit_array(v)
}
Yaml::Hash(ref h) => { if (inline && self.compact) || h.is_empty() {
write!(self.writer, " ")?;
} else {
writeln!(self.writer)?; self.level += 1; self.write_indent()?; self.level -= 1;
} self.emit_hash(h)
}
_ => {
write!(self.writer, " ")?; self.emit_node(val)
}
}
}
}
/// Check if the string requires quoting. /// Strings starting with any of the following characters must be quoted. /// :, &, *, ?, |, -, <, >, =, !, %, @ /// Strings containing any of the following characters must be quoted. /// {, }, \[, t \], ,, #, ` /// /// If the string contains any of the following control characters, it must be escaped with double quotes: /// \0, \x01, \x02, \x03, \x04, \x05, \x06, \a, \b, \t, \n, \v, \f, \r, \x0e, \x0f, \x10, \x11, \x12, \x13, \x14, \x15, \x16, \x17, \x18, \x19, \x1a, \e, \x1c, \x1d, \x1e, \x1f, \N, \_, \L, \P /// /// Finally, there are other cases when the strings must be quoted, no matter if you're using single or double quotes: /// * When the string is true or false (otherwise, it would be treated as a boolean value); /// * When the string is null or ~ (otherwise, it would be considered as a null value); /// * When the string looks like a number, such as integers (e.g. 2, 14, etc.), floats (e.g. 2.6, 14.9) and exponential numbers (e.g. 12e7, etc.) (otherwise, it would be treated as a numeric value); /// * When the string looks like a date (e.g. 2014-12-31) (otherwise it would be automatically converted into a Unix timestamp). #[allow(clippy::doc_markdown)] fn need_quotes(string: &str) -> bool { fn need_quotes_spaces(string: &str) -> bool {
string.starts_with(' ') || string.ends_with(' ')
}
string.is_empty()
|| need_quotes_spaces(string)
|| string.starts_with(|character: char| {
matches!(
character, '&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@'
)
})
|| string.contains(|character: char| {
matches!(character, ':'
| '{'
| '}'
| '['
| ']'
| ','
| '#'
| '`'
| '\"'
| '\''
| '\\'
| '\0'..='\x06'
| '\t'
| '\n'
| '\r'
| '\x0e'..='\x1a'
| '\x1c'..='\x1f')
})
|| [ // Canonical forms of the boolean values in the Core schema. "true", "false", "True", "False", "TRUE", "FALSE", // Canonical forms of the null value in the Core schema. "null", "Null", "NULL", "~", // These can be quoted when emitting so that YAML 1.1 parsers do not parse them as // booleans. This doesn't cause any issue with YAML 1.2 parsers. "y", "Y", "n", "N", "yes", "Yes", "YES", "no", "No", "NO", "True", "TRUE", "False", "FALSE", "on", "On", "ON", "off", "Off", "OFF",
]
.contains(&string)
|| string.starts_with('.')
|| string.starts_with("0x")
|| string.parse::<i64>().is_ok()
|| string.parse::<f64>().is_ok()
}
#[cfg(test)] mod test { usesuper::YamlEmitter; usecrate::YamlLoader;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.