cs2-dumper/src/builder/yaml_file_builder.rs

49 lines
1.2 KiB
Rust

use super::FileBuilder;
use std::io::{Result, Write};
/// A structure representing a builder for Yaml files.
/// The builder implements the `FileBuilder` trait.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct YamlFileBuilder;
impl FileBuilder for YamlFileBuilder {
fn extension(&mut self) -> &str {
"yaml"
}
fn write_top_level(&mut self, output: &mut dyn Write) -> Result<()> {
write!(output, "---\n")
}
fn write_namespace(
&mut self,
output: &mut dyn Write,
name: &str,
comment: Option<&str>,
) -> Result<()> {
let comment = comment.map_or(String::new(), |c| format!(" # {}", c));
write!(output, "{}:{}\n", name, comment)
}
fn write_variable(
&mut self,
output: &mut dyn Write,
name: &str,
value: usize,
comment: Option<&str>,
indentation: Option<usize>,
) -> Result<()> {
let indentation = " ".repeat(indentation.unwrap_or(4));
let comment = comment.map_or(String::new(), |c| format!(" # {}", c));
write!(output, "{}{}: {}{}\n", indentation, name, value, comment)
}
fn write_closure(&mut self, _output: &mut dyn Write, _eof: bool) -> Result<()> {
Ok(())
}
}