ucglib/convert/
yamlmulti.rs

1// Copyright 2020 Jeremy Wall
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std;
16use std::io::Write;
17use std::rc::Rc;
18
19use crate::build::Val;
20use crate::convert::traits::{ConvertResult, Converter};
21use crate::convert::yaml::YamlConverter;
22
23pub struct MultiYamlConverter(YamlConverter);
24
25impl MultiYamlConverter {
26    pub fn new() -> Self {
27        MultiYamlConverter(YamlConverter::new())
28    }
29
30    pub fn convert_list(&self, vals: &Vec<Rc<Val>>, mut w: &mut dyn Write) -> ConvertResult {
31        for val in vals {
32            self.0.write(val.as_ref(), &mut w)?;
33        }
34        Ok(())
35    }
36}
37
38impl Converter for MultiYamlConverter {
39    fn convert(&self, v: Rc<Val>, mut w: &mut dyn Write) -> ConvertResult {
40        if let Val::List(ref vals) = v.as_ref() {
41            self.convert_list(vals, &mut w)
42        } else {
43            let list = vec![v];
44            self.convert_list(&list, &mut w)
45        }
46    }
47
48    fn file_ext(&self) -> String {
49        "yaml".to_owned()
50    }
51
52    fn description(&self) -> String {
53        "Convert ucg vals into valid multi document yaml.".to_owned()
54    }
55
56    #[allow(unused_must_use)]
57    fn help(&self) -> String {
58        include_str!("yaml_help.txt").to_owned()
59    }
60}