ucglib/convert/
env.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//! Contains code for converting a UCG Val into the environment variable output target.
16use std::io::Write as IOWrite;
17use std::rc::Rc;
18
19use crate::build::Val;
20use crate::convert::traits::{ConvertResult, Converter};
21
22/// EnvConverter implements the conversion logic for converting a Val into a
23/// set of environment variables.
24pub struct EnvConverter {}
25
26impl EnvConverter {
27    pub fn new() -> Self {
28        EnvConverter {}
29    }
30
31    fn convert_tuple(&self, flds: &Vec<(String, Rc<Val>)>, w: &mut dyn IOWrite) -> ConvertResult {
32        for &(ref name, ref val) in flds.iter() {
33            if val.is_tuple() {
34                eprintln!("Skipping embedded tuple...");
35                return Ok(());
36            }
37            if let &Val::Empty = val.as_ref() {
38                eprintln!("Skipping empty variable: {}", name);
39                return Ok(());
40            }
41            write!(w, "{}=", name)?;
42            self.write(&val, w)?;
43        }
44        Ok(())
45    }
46
47    fn convert_list(&self, _items: &Vec<Rc<Val>>, _w: &mut dyn IOWrite) -> ConvertResult {
48        eprintln!("Skipping List...");
49        Ok(())
50    }
51
52    fn write(&self, v: &Val, w: &mut dyn IOWrite) -> ConvertResult {
53        match v {
54            &Val::Empty => {
55                // Empty is a noop.
56                return Ok(());
57            }
58            &Val::Boolean(b) => {
59                write!(w, "{}\n", if b { "true" } else { "false" })?;
60            }
61            &Val::Float(ref f) => {
62                write!(w, "{}\n", f)?;
63            }
64            &Val::Int(ref i) => {
65                write!(w, "{}\n", i)?;
66            }
67            &Val::Str(ref s) => {
68                write!(w, "'{}'\n", s)?;
69            }
70            &Val::List(ref items) => {
71                self.convert_list(items, w)?;
72            }
73            &Val::Tuple(ref flds) => {
74                self.convert_tuple(flds, w)?;
75            }
76            &Val::Env(ref _fs) => {
77                // This is ignored
78                eprintln!("Skipping env...");
79            }
80        }
81        Ok(())
82    }
83}
84
85impl Converter for EnvConverter {
86    fn convert(&self, v: Rc<Val>, mut w: &mut dyn IOWrite) -> ConvertResult {
87        self.write(&v, &mut w)
88    }
89
90    fn file_ext(&self) -> String {
91        String::from("env")
92    }
93
94    fn description(&self) -> String {
95        "Convert ucg Vals into environment variables.".to_string()
96    }
97
98    #[allow(unused_must_use)]
99    fn help(&self) -> String {
100        include_str!("env_help.txt").to_string()
101    }
102}