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.
|
2018-05-14 21:34:38 -05:00
|
|
|
use std::io::Write;
|
|
|
|
use std::rc::Rc;
|
2017-11-15 22:43:29 -06:00
|
|
|
|
2018-12-06 12:23:52 -06:00
|
|
|
use crate::build::Val;
|
2019-02-19 14:50:55 -06:00
|
|
|
use crate::convert::traits::{ConvertResult, Converter};
|
2017-11-15 22:43:29 -06:00
|
|
|
|
2018-07-14 22:56:47 -05:00
|
|
|
/// FlagConverter implements the conversion logic for converting a Val into a set
|
|
|
|
/// of command line flags.
|
2019-04-08 22:13:29 -05:00
|
|
|
pub struct FlagConverter {
|
|
|
|
sep: &'static str,
|
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
|
|
|
|
impl FlagConverter {
|
|
|
|
pub fn new() -> Self {
|
2019-04-08 22:13:29 -05:00
|
|
|
FlagConverter { sep: "." }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_sep(mut self, sep: &'static str) -> Self {
|
|
|
|
self.sep = sep;
|
|
|
|
self
|
2017-11-15 22:43:29 -06:00
|
|
|
}
|
|
|
|
|
2019-02-19 14:50:55 -06:00
|
|
|
fn write_flag_name(&self, pfx: &str, name: &str, w: &mut Write) -> ConvertResult {
|
2018-03-12 20:29:31 -05:00
|
|
|
if name.chars().count() > 1 || pfx.chars().count() > 0 {
|
2018-12-06 12:46:47 -06:00
|
|
|
write!(w, "--{}{} ", pfx, name)?;
|
2018-03-12 20:29:31 -05:00
|
|
|
} else {
|
2018-12-06 12:46:47 -06:00
|
|
|
write!(w, "-{} ", name)?;
|
2018-03-12 20:29:31 -05:00
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:50:55 -06:00
|
|
|
fn write_list_flag(
|
|
|
|
&self,
|
|
|
|
pfx: &str,
|
|
|
|
name: &str,
|
|
|
|
def: &Vec<Rc<Val>>,
|
|
|
|
w: &mut Write,
|
|
|
|
) -> ConvertResult {
|
2018-03-17 09:15:13 -05:00
|
|
|
// first of all we need to make sure that each &Val is only a primitive type.
|
|
|
|
for v in def.iter() {
|
|
|
|
let vref = v.as_ref();
|
2019-01-24 20:04:40 -06:00
|
|
|
if vref.is_list() || vref.is_tuple() || vref.is_func() {
|
2018-03-17 09:15:13 -05:00
|
|
|
eprintln!(
|
|
|
|
"Skipping non primitive val in list for flag {}{}",
|
|
|
|
pfx, name
|
|
|
|
);
|
|
|
|
} else {
|
2018-12-06 12:46:47 -06:00
|
|
|
self.write_flag_name(pfx, name, w)?;
|
|
|
|
self.write(pfx, vref, w)?;
|
2018-03-15 19:31:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:50:55 -06:00
|
|
|
fn write(&self, pfx: &str, v: &Val, w: &mut Write) -> ConvertResult {
|
2017-11-15 22:43:29 -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-12-06 12:46:47 -06:00
|
|
|
write!(w, "{} ", if b { "true" } else { "false" })?;
|
2018-03-22 20:09:38 -05:00
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
&Val::Float(ref f) => {
|
2018-12-06 12:46:47 -06:00
|
|
|
write!(w, "{} ", f)?;
|
2017-11-26 12:22:58 -05:00
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
&Val::Int(ref i) => {
|
2018-12-06 12:46:47 -06:00
|
|
|
write!(w, "{} ", i)?;
|
2017-11-26 12:22:58 -05:00
|
|
|
}
|
2018-06-10 14:13:08 -05:00
|
|
|
&Val::Str(ref s) => {
|
2018-12-06 12:46:47 -06:00
|
|
|
write!(w, "'{}' ", s)?;
|
2017-11-26 12:22:58 -05:00
|
|
|
}
|
|
|
|
&Val::List(ref _def) => {
|
|
|
|
eprintln!("Skipping List...");
|
|
|
|
}
|
2018-12-10 21:27:44 -06:00
|
|
|
&Val::Tuple(ref flds) => {
|
|
|
|
for &(ref name, ref val) in flds.iter() {
|
|
|
|
if let &Val::Empty = val.as_ref() {
|
2019-02-19 16:36:19 -06:00
|
|
|
self.write_flag_name(pfx, name, w)?;
|
2018-12-10 21:27:44 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
match val.as_ref() {
|
|
|
|
&Val::Tuple(_) => {
|
2019-04-08 22:13:29 -05:00
|
|
|
let new_pfx = format!("{}{}{}", pfx, name, self.sep);
|
2018-12-10 21:27:44 -06:00
|
|
|
self.write(&new_pfx, val, w)?;
|
|
|
|
}
|
|
|
|
&Val::List(ref def) => {
|
2019-02-19 16:36:19 -06:00
|
|
|
self.write_list_flag(pfx, name, def, w)?;
|
2018-12-10 21:27:44 -06:00
|
|
|
}
|
|
|
|
_ => {
|
2019-02-19 16:36:19 -06:00
|
|
|
self.write_flag_name(pfx, name, w)?;
|
2018-12-10 21:27:44 -06:00
|
|
|
self.write(pfx, &val, w)?;
|
|
|
|
}
|
2018-03-17 09:15:13 -05:00
|
|
|
}
|
2018-03-15 19:31:02 -05:00
|
|
|
}
|
2018-12-10 21:27:44 -06:00
|
|
|
}
|
2019-01-24 20:04:40 -06:00
|
|
|
&Val::Func(ref _def) => {
|
2017-11-15 22:43:29 -06:00
|
|
|
// 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...");
|
|
|
|
}
|
2017-11-15 22:43:29 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Converter for FlagConverter {
|
2019-02-19 14:50:55 -06:00
|
|
|
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
|
2018-03-06 19:39:10 -06:00
|
|
|
self.write("", &v, &mut w)
|
2017-11-15 22:43:29 -06:00
|
|
|
}
|
2018-08-15 18:22:05 -05:00
|
|
|
|
|
|
|
fn file_ext(&self) -> String {
|
|
|
|
String::from("txt")
|
|
|
|
}
|
2018-08-22 00:13:11 -05:00
|
|
|
|
|
|
|
fn description(&self) -> String {
|
|
|
|
"Convert ucg Vals into command line flags.".to_string()
|
|
|
|
}
|
2018-03-11 15:11:27 -05:00
|
|
|
|
2019-03-25 20:23:50 -04:00
|
|
|
#[allow(unused_must_use)]
|
|
|
|
fn help(&self) -> String {
|
2019-04-08 22:13:29 -05:00
|
|
|
include_str!("flags_help.txt").to_string()
|
2019-03-25 20:23:50 -04:00
|
|
|
}
|
|
|
|
}
|