ucglib/convert/
flags.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 command line flag output target.
16use std::io::Write;
17use std::rc::Rc;
18
19use crate::build::Val;
20use crate::convert::traits::{ConvertResult, Converter};
21use crate::error::{BuildError, ErrorType};
22
23/// FlagConverter implements the conversion logic for converting a Val into a set
24/// of command line flags.
25pub struct FlagConverter {
26    sep: &'static str,
27}
28
29impl FlagConverter {
30    pub fn new() -> Self {
31        FlagConverter { sep: "." }
32    }
33
34    pub fn with_sep(mut self, sep: &'static str) -> Self {
35        self.sep = sep;
36        self
37    }
38
39    fn write_flag_name(&self, pfx: &str, name: &str, w: &mut dyn Write) -> ConvertResult {
40        if name.chars().count() > 1 || pfx.chars().count() > 0 {
41            write!(w, "--{}{} ", pfx, name)?;
42        } else {
43            write!(w, "-{} ", name)?;
44        }
45        return Ok(());
46    }
47
48    fn write_list_flag(
49        &self,
50        pfx: &str,
51        name: &str,
52        def: &Vec<Rc<Val>>,
53        w: &mut dyn Write,
54    ) -> ConvertResult {
55        // first of all we need to make sure that each &Val is only a primitive type.
56        for v in def.iter() {
57            let vref = v.as_ref();
58            if vref.is_list() || vref.is_tuple() {
59                eprintln!(
60                    "Skipping non primitive val in list for flag {}{}",
61                    pfx, name
62                );
63            } else {
64                self.write_flag_name(pfx, name, w)?;
65                self.write_simple_value(vref, w)?;
66            }
67        }
68        return Ok(());
69    }
70
71    fn write_simple_value(&self, v: &Val, w: &mut dyn Write) -> ConvertResult {
72        match v {
73            &Val::Empty => {
74                // Empty is a noop.
75                return Ok(());
76            }
77            &Val::Boolean(b) => {
78                write!(w, "{} ", if b { "true" } else { "false" })?;
79            }
80            &Val::Float(ref f) => {
81                write!(w, "{} ", f)?;
82            }
83            &Val::Int(ref i) => {
84                write!(w, "{} ", i)?;
85            }
86            &Val::Str(ref s) => {
87                write!(w, "'{}' ", s)?;
88            }
89            &Val::List(_) | &Val::Tuple(_) | &Val::Env(_) => {
90                // This is ignored
91                eprintln!("Skipping {}...", v.type_name());
92            }
93        }
94        Ok(())
95    }
96
97    fn write(&self, pfx: &str, flds: &Vec<(String, Rc<Val>)>, w: &mut dyn Write) -> ConvertResult {
98        for &(ref name, ref val) in flds.iter() {
99            if let &Val::Empty = val.as_ref() {
100                self.write_flag_name(pfx, name, w)?;
101                continue;
102            }
103            match val.as_ref() {
104                &Val::Tuple(_) | &Val::Env(_) => {
105                    eprintln!("Skipping {} in flag output tuple.", val.type_name());
106                }
107                &Val::List(ref def) => {
108                    self.write_list_flag(pfx, name, def, w)?;
109                }
110                &Val::Boolean(_) | &Val::Empty | &Val::Float(_) | &Val::Int(_) | &Val::Str(_) => {
111                    self.write_flag_name(pfx, name, w)?;
112                    self.write_simple_value(val, w)?;
113                }
114            }
115        }
116        Ok(())
117    }
118}
119
120impl Converter for FlagConverter {
121    fn convert(&self, v: Rc<Val>, mut w: &mut dyn Write) -> ConvertResult {
122        if let &Val::Tuple(ref flds) = v.as_ref() {
123            self.write("", flds, &mut w)
124        } else {
125            return Err(Box::new(BuildError::new(
126                "Flag outputs must be a tuple",
127                ErrorType::ConvertError,
128            )));
129        }
130    }
131
132    fn file_ext(&self) -> String {
133        String::from("txt")
134    }
135
136    fn description(&self) -> String {
137        "Convert ucg Vals into command line flags.".to_string()
138    }
139
140    #[allow(unused_must_use)]
141    fn help(&self) -> String {
142        include_str!("flags_help.txt").to_string()
143    }
144}