2017-11-15 22:43:29 -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-02-12 22:47:42 -06:00
|
|
|
//! Contains code for converting a UCG Val into the command line flag output target.
|
2017-11-15 22:43:29 -06:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::io::Result;
|
|
|
|
|
|
|
|
use build::Val;
|
|
|
|
use convert::traits::Converter;
|
|
|
|
|
2018-02-07 19:49:13 -06:00
|
|
|
/// FlagConverter implements the conversion logic for converting a Val into a set of command line flags.
|
2017-11-15 22:43:29 -06:00
|
|
|
pub struct FlagConverter {}
|
|
|
|
|
|
|
|
impl FlagConverter {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
FlagConverter {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&self, v: &Val, w: &mut Write) -> Result<()> {
|
|
|
|
match v {
|
|
|
|
&Val::Float(ref f) => {
|
|
|
|
try!(write!(w, "{} ", f));
|
2017-11-26 12:22:58 -05:00
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
&Val::Int(ref i) => {
|
|
|
|
try!(write!(w, "{} ", i));
|
2017-11-26 12:22:58 -05:00
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
&Val::String(ref s) => {
|
|
|
|
try!(write!(w, "'{}' ", s));
|
2017-11-26 12:22:58 -05:00
|
|
|
}
|
|
|
|
&Val::List(ref _def) => {
|
|
|
|
// FIXME(jwall): Fill this in?
|
|
|
|
eprintln!("Skipping List...");
|
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
&Val::Tuple(ref flds) => {
|
|
|
|
for &(ref name, ref val) in flds.iter() {
|
2018-02-04 16:08:30 -06:00
|
|
|
if val.is_tuple() {
|
|
|
|
eprintln!("Skipping embedded tuple...");
|
|
|
|
return Ok(());
|
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
try!(write!(w, "--{} ", name.val));
|
|
|
|
// TODO(jwall): What if the value is a tuple?
|
|
|
|
try!(self.write(&val, w));
|
|
|
|
}
|
2017-11-26 12:22:58 -05:00
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
&Val::Macro(ref _def) => {
|
|
|
|
// This is ignored
|
|
|
|
eprintln!("Skipping macro...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Converter for FlagConverter {
|
|
|
|
fn convert(&self, v: Rc<Val>, mut w: Box<Write>) -> Result<()> {
|
2018-02-04 16:08:30 -06:00
|
|
|
self.write(&v, &mut w)
|
2017-11-15 22:43:29 -06:00
|
|
|
}
|
|
|
|
}
|