mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-21 18:10:42 -04:00
parent
3f3368f496
commit
72de604384
@ -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;
|
||||
out yamlmulti [server_config, server_config];
|
@ -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
|
||||
|
@ -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, "")?;
|
||||
|
@ -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.
|
||||
- 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.
|
60
src/convert/yamlmulti.rs
Normal file
60
src/convert/yamlmulti.rs
Normal file
@ -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<Rc<Val>>, 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<Val>, 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()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user