2018-02-12 22:47:42 -06:00
|
|
|
// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-07-14 22:56:47 -05:00
|
|
|
//! Contains code for converting a UCG Val into the environment variable output target.
|
2018-05-14 21:34:38 -05:00
|
|
|
use std::io::Write;
|
|
|
|
use std::rc::Rc;
|
2018-02-12 22:47:42 -06:00
|
|
|
|
2018-11-05 21:34:12 -06:00
|
|
|
use ast::PositionedItem;
|
2018-02-12 22:47:42 -06:00
|
|
|
use build::Val;
|
2018-06-18 22:07:18 -05:00
|
|
|
use convert::traits::{Converter, Result};
|
2018-02-12 22:47:42 -06:00
|
|
|
|
2018-07-14 22:56:47 -05:00
|
|
|
/// EnvConverter implements the conversion logic for converting a Val into a
|
|
|
|
/// set of environment variables.
|
2018-02-12 22:47:42 -06:00
|
|
|
pub struct EnvConverter {}
|
|
|
|
|
|
|
|
impl EnvConverter {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
EnvConverter {}
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:12 -06:00
|
|
|
fn convert_tuple(
|
|
|
|
&self,
|
|
|
|
flds: &Vec<(PositionedItem<String>, Rc<Val>)>,
|
|
|
|
w: &mut Write,
|
|
|
|
) -> Result {
|
2018-02-12 22:47:42 -06:00
|
|
|
for &(ref name, ref val) in flds.iter() {
|
|
|
|
if val.is_tuple() {
|
|
|
|
eprintln!("Skipping embedded tuple...");
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-03-12 20:29:31 -05:00
|
|
|
if let &Val::Empty = val.as_ref() {
|
|
|
|
eprintln!("Skipping empty variable: {}", name);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-02-12 22:47:42 -06:00
|
|
|
try!(write!(w, "{}=", name.val));
|
|
|
|
try!(self.write(&val, w));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-06-18 22:07:18 -05:00
|
|
|
fn convert_list(&self, _items: &Vec<Rc<Val>>, _w: &mut Write) -> Result {
|
2018-02-12 22:47:42 -06:00
|
|
|
eprintln!("Skipping List...");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-06-18 22:07:18 -05:00
|
|
|
fn write(&self, v: &Val, w: &mut Write) -> Result {
|
2018-02-12 22:47:42 -06:00
|
|
|
match v {
|
2018-03-12 20:29:31 -05:00
|
|
|
&Val::Empty => {
|
|
|
|
// Empty is a noop.
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-03-22 20:09:38 -05:00
|
|
|
&Val::Boolean(b) => {
|
2018-11-17 21:49:56 -06:00
|
|
|
try!(write!(w, "{}\n", if b { "true" } else { "false" }));
|
2018-03-22 20:09:38 -05:00
|
|
|
}
|
2018-02-12 22:47:42 -06:00
|
|
|
&Val::Float(ref f) => {
|
2018-11-17 21:49:56 -06:00
|
|
|
try!(write!(w, "{}\n", f));
|
2018-02-12 22:47:42 -06:00
|
|
|
}
|
|
|
|
&Val::Int(ref i) => {
|
2018-11-17 21:49:56 -06:00
|
|
|
try!(write!(w, "{}\n", i));
|
2018-02-12 22:47:42 -06:00
|
|
|
}
|
2018-06-10 14:13:08 -05:00
|
|
|
&Val::Str(ref s) => {
|
2018-11-17 21:49:56 -06:00
|
|
|
try!(write!(w, "'{}'\n", s));
|
2018-02-12 22:47:42 -06:00
|
|
|
}
|
|
|
|
&Val::List(ref items) => {
|
|
|
|
try!(self.convert_list(items, w));
|
|
|
|
}
|
|
|
|
&Val::Tuple(ref flds) => {
|
|
|
|
try!(self.convert_tuple(flds, w));
|
|
|
|
}
|
|
|
|
&Val::Macro(ref _def) => {
|
|
|
|
// This is ignored
|
|
|
|
eprintln!("Skipping macro...");
|
|
|
|
}
|
2018-11-26 21:36:50 -06:00
|
|
|
&Val::Env(ref _fs) => {
|
|
|
|
// This is ignored
|
|
|
|
eprintln!("Skipping env...");
|
|
|
|
}
|
2018-11-23 12:50:47 -06:00
|
|
|
&Val::Module(ref _def) => {
|
|
|
|
// This is ignored
|
|
|
|
eprintln!("Skipping module...");
|
|
|
|
}
|
2018-02-12 22:47:42 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Converter for EnvConverter {
|
2018-06-20 17:19:17 -04:00
|
|
|
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
|
2018-02-12 22:47:42 -06:00
|
|
|
self.write(&v, &mut w)
|
|
|
|
}
|
2018-08-15 18:22:05 -05:00
|
|
|
|
|
|
|
fn file_ext(&self) -> String {
|
2018-08-17 10:28:26 -05:00
|
|
|
String::from("env")
|
2018-08-15 18:22:05 -05:00
|
|
|
}
|
2018-08-22 00:13:11 -05:00
|
|
|
|
|
|
|
fn description(&self) -> String {
|
|
|
|
"Convert ucg Vals into environment variables.".to_string()
|
|
|
|
}
|
2018-02-12 22:47:42 -06:00
|
|
|
}
|