ucglib/convert/
mod.rs

1// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
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
15//! The conversion stage of the ucg compiler.
16pub mod b64;
17pub mod env;
18pub mod exec;
19pub mod flags;
20pub mod json;
21pub mod toml;
22pub mod traits;
23pub mod xml;
24pub mod yaml;
25pub mod yamlmulti;
26
27use std::collections::HashMap;
28
29/// ConverterRunner knows how to run a given converter on a Val.
30pub struct ConverterRegistry {
31    converters: HashMap<String, Box<dyn traits::Converter>>,
32}
33
34impl ConverterRegistry {
35    /// Creates a new ConverterRegistry.
36    ///
37    /// * flags
38    /// * json
39    /// * env
40    /// * exec
41    fn new() -> Self {
42        ConverterRegistry {
43            converters: HashMap::new(),
44        }
45    }
46
47    pub fn make_registry() -> Self {
48        let mut registry = Self::new();
49        registry.register("json", Box::new(json::JsonConverter::new()));
50        registry.register("env", Box::new(env::EnvConverter::new()));
51        registry.register("flags", Box::new(flags::FlagConverter::new()));
52        registry.register("exec", Box::new(exec::ExecConverter::new()));
53        registry.register("yaml", Box::new(yaml::YamlConverter::new()));
54        registry.register("yamlmulti", Box::new(yamlmulti::MultiYamlConverter::new()));
55        registry.register("toml", Box::new(toml::TomlConverter::new()));
56        registry.register("xml", Box::new(xml::XmlConverter {}));
57        registry
58    }
59
60    pub fn register<S: Into<String>>(&mut self, typ: S, converter: Box<dyn traits::Converter>) {
61        self.converters.insert(typ.into(), converter);
62    }
63
64    pub fn get_converter(&self, typ: &str) -> Option<&dyn traits::Converter> {
65        self.converters.get(typ).map(|c| c.as_ref())
66    }
67
68    pub fn get_converter_list(&self) -> Vec<(&String, &Box<dyn traits::Converter>)> {
69        self.converters.iter().collect()
70    }
71}
72
73pub struct ImporterRegistry {
74    importers: HashMap<String, Box<dyn traits::Importer>>,
75}
76
77impl ImporterRegistry {
78    /// Creates a new ImporterRegistry.
79    ///
80    /// * b64
81    /// * b64urlsafe
82    fn new() -> Self {
83        ImporterRegistry {
84            importers: HashMap::new(),
85        }
86    }
87
88    pub fn make_registry() -> Self {
89        let mut registry = Self::new();
90        registry.register("b64", Box::new(b64::Base64Importer { url_safe: false }));
91        registry.register(
92            "b64urlsafe",
93            Box::new(b64::Base64Importer { url_safe: true }),
94        );
95        registry.register("json", Box::new(json::JsonConverter {}));
96        registry.register("yaml", Box::new(yaml::YamlConverter {}));
97        registry.register("toml", Box::new(toml::TomlConverter {}));
98        registry
99    }
100
101    pub fn register<S: Into<String>>(&mut self, typ: S, importer: Box<dyn traits::Importer>) {
102        self.importers.insert(typ.into(), importer);
103    }
104
105    pub fn get_importer(&self, typ: &str) -> Option<&dyn traits::Importer> {
106        self.importers.get(typ).map(|c| c.as_ref())
107    }
108
109    pub fn get_importer_list(&self) -> Vec<(&String, &Box<dyn traits::Importer>)> {
110        self.importers.iter().collect()
111    }
112}