diff --git a/examples/test_yaml.ucg b/examples/test_yaml.ucg index 4d0e136..05937d2 100644 --- a/examples/test_yaml.ucg +++ b/examples/test_yaml.ucg @@ -19,11 +19,9 @@ let db_conn2 = mk_db_conn(dbhost2, shared.port, dbname); // We have two database connections in a list let db_conn_list = [db_conn1, db_conn2]; -let connstr_mapper = func (item) => { - str = item.conn_string -}; +let connstr_mapper = func (item) => item.conn_string; -let db_conns = map connstr_mapper.str [db_conn1, db_conn2]; +let db_conns = map(connstr_mapper, [db_conn1, db_conn2]); let cplx_list = [ {foo = 1}, @@ -41,4 +39,4 @@ let server_config = { l = ["foo", "bar"] }; -out yaml server_config; \ No newline at end of file +out yamlmulti [server_config, server_config]; \ No newline at end of file diff --git a/src/convert/mod.rs b/src/convert/mod.rs index 975757e..534e310 100644 --- a/src/convert/mod.rs +++ b/src/convert/mod.rs @@ -22,6 +22,7 @@ pub mod toml; pub mod traits; pub mod xml; pub mod yaml; +pub mod yamlmulti; use std::collections::HashMap; @@ -50,6 +51,7 @@ impl ConverterRegistry { registry.register("flags", Box::new(flags::FlagConverter::new())); registry.register("exec", Box::new(exec::ExecConverter::new())); registry.register("yaml", Box::new(yaml::YamlConverter::new())); + registry.register("yamlmulti", Box::new(yamlmulti::MultiYamlConverter::new())); registry.register("toml", Box::new(toml::TomlConverter::new())); registry.register("xml", Box::new(xml::XmlConverter {})); registry diff --git a/src/convert/yaml.rs b/src/convert/yaml.rs index eb3d526..a38d6ea 100644 --- a/src/convert/yaml.rs +++ b/src/convert/yaml.rs @@ -130,7 +130,7 @@ impl YamlConverter { }) } - fn write(&self, v: &Val, mut w: &mut dyn Write) -> ConvertResult { + pub fn write(&self, v: &Val, mut w: &mut dyn Write) -> ConvertResult { let jsn_val = self.convert_value(v)?; serde_yaml::to_writer(&mut w, &jsn_val)?; writeln!(w, "")?; diff --git a/src/convert/yaml_help.txt b/src/convert/yaml_help.txt index b8fcabc..c032611 100644 --- a/src/convert/yaml_help.txt +++ b/src/convert/yaml_help.txt @@ -7,4 +7,8 @@ They are transformed into toml using the following rules: - Int becomes an Int - Float becomes a Float - Strings become Strings. -- Functions and Modules are ignored. \ No newline at end of file +- Functions and Modules are ignored. + +If you are using the yamlmulti conversion type then a list will get transformed +into a yaml document per list item. All other types will get treated as a single +document. \ No newline at end of file diff --git a/src/convert/yamlmulti.rs b/src/convert/yamlmulti.rs new file mode 100644 index 0000000..fd54068 --- /dev/null +++ b/src/convert/yamlmulti.rs @@ -0,0 +1,60 @@ +// Copyright 2020 Jeremy Wall +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std; +use std::io::Write; +use std::rc::Rc; + +use crate::convert::yaml::YamlConverter; +use crate::convert::traits::{ConvertResult, Converter}; +use crate::build::Val; + +pub struct MultiYamlConverter (YamlConverter); + +impl MultiYamlConverter { + pub fn new() -> Self { + MultiYamlConverter (YamlConverter::new()) + } + + pub fn convert_list(&self, vals: &Vec>, mut w: &mut dyn Write) -> ConvertResult { + for val in vals { + self.0.write(val.as_ref(), &mut w)?; + } + Ok(()) + } +} + +impl Converter for MultiYamlConverter { + fn convert(&self, v: Rc, mut w: &mut dyn Write) -> ConvertResult { + if let Val::List(ref vals) = v.as_ref() { + self.convert_list(vals, &mut w) + } else { + let list = vec![v]; + self.convert_list(&list, &mut w) + } + } + + fn file_ext(&self) -> String { + "yaml".to_owned() + } + + fn description(&self) -> String { + "Convert ucg vals into valid multi document yaml.".to_owned() + } + + #[allow(unused_must_use)] + fn help(&self) -> String { + include_str!("yaml_help.txt").to_owned() + } +} \ No newline at end of file