From 0766d8058f38dba3f4bf6d4547cffe29c7b49927 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Thu, 30 May 2019 18:01:05 -0500 Subject: [PATCH] DEV: Stop using nested tuples in flag output. Now that we have string manipulation facilities in the stdlib that is a less useful and more confusing feature. This code removes it. --- docsite/site/content/reference/converters.md | 8 +-- examples/test_flags.txt | 1 - examples/test_flags.ucg | 6 +- src/convert/flags.rs | 67 +++++++++----------- src/convert/flags_help.txt | 12 ++-- 5 files changed, 39 insertions(+), 55 deletions(-) delete mode 100644 examples/test_flags.txt diff --git a/docsite/site/content/reference/converters.md b/docsite/site/content/reference/converters.md index ed8f4b2..d096e90 100644 --- a/docsite/site/content/reference/converters.md +++ b/docsite/site/content/reference/converters.md @@ -32,7 +32,7 @@ rules for each type. * Boolean values are translated to "true" and "false" repsectively. * Integers and Floats are rendered as numbers. * Tuples are rendered as `--field value` pairs for each field. - * Nested tuples concatenate the field names to create the field. + * Nested tuples are ignored. * Nested Lists generate a new `--field listitem` pair for each item in the list. * For fields that are just one character in length use a single `-`. Use double dashes `--` for fields that are longer than one character. @@ -48,10 +48,8 @@ let flags = { "some/dir", "some/other/dir", ], - log = { - debug = true, - format = "json", - }, + "log.debug" = true, + "log.format" = "json", } ``` diff --git a/examples/test_flags.txt b/examples/test_flags.txt deleted file mode 100644 index dc41662..0000000 --- a/examples/test_flags.txt +++ /dev/null @@ -1 +0,0 @@ ---port 8080 --listen '0.0.0.0' --verbose --dir 'some/dir' --dir 'some/other/dir' --log.debug true --log.format 'json' \ No newline at end of file diff --git a/examples/test_flags.ucg b/examples/test_flags.ucg index 2b3d221..52f8467 100644 --- a/examples/test_flags.ucg +++ b/examples/test_flags.ucg @@ -6,10 +6,8 @@ let args = { "some/dir", "some/other/dir", ], - log = { - debug = true, - format = "json", - }, + "log.debug" = true, + "log.format" = "json", }; out flags args; \ No newline at end of file diff --git a/src/convert/flags.rs b/src/convert/flags.rs index 7358055..db3a7b0 100644 --- a/src/convert/flags.rs +++ b/src/convert/flags.rs @@ -55,20 +55,20 @@ impl FlagConverter { // 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(); - if vref.is_list() || vref.is_tuple() || vref.is_func() { + if vref.is_list() || vref.is_tuple() || vref.is_func() || vref.is_module() { eprintln!( "Skipping non primitive val in list for flag {}{}", pfx, name ); } else { self.write_flag_name(pfx, name, w)?; - self.write(pfx, vref, w)?; + self.write_simple_value(vref, w)?; } } return Ok(()); } - fn write(&self, pfx: &str, v: &Val, w: &mut Write) -> ConvertResult { + fn write_simple_value(&self, v: &Val, w: &mut Write) -> ConvertResult { match v { &Val::Empty => { // Empty is a noop. @@ -86,41 +86,31 @@ impl FlagConverter { &Val::Str(ref s) => { write!(w, "'{}' ", s)?; } - &Val::List(ref _def) => { - eprintln!("Skipping List..."); + &Val::List(_) | &Val::Tuple(_) | &Val::Func(_) | &Val::Env(_) | &Val::Module(_) => { + // This is ignored + eprintln!("Skipping {}...", v.type_name()); } - &Val::Tuple(ref flds) => { - for &(ref name, ref val) in flds.iter() { - if let &Val::Empty = val.as_ref() { - self.write_flag_name(pfx, name, w)?; - continue; - } - match val.as_ref() { - &Val::Tuple(_) => { - let new_pfx = format!("{}{}{}", pfx, name, self.sep); - self.write(&new_pfx, val, w)?; - } - &Val::List(ref def) => { - self.write_list_flag(pfx, name, def, w)?; - } - _ => { - self.write_flag_name(pfx, name, w)?; - self.write(pfx, &val, w)?; - } - } + } + Ok(()) + } + + fn write(&self, pfx: &str, flds: &Vec<(String, Rc)>, w: &mut Write) -> ConvertResult { + for &(ref name, ref val) in flds.iter() { + if let &Val::Empty = val.as_ref() { + self.write_flag_name(pfx, name, w)?; + continue; + } + match val.as_ref() { + &Val::Tuple(_) | &Val::Module(_) | &Val::Func(_) | &Val::Env(_) => { + eprintln!("Skipping {} in flag output tuple.", val.type_name()); + } + &Val::List(ref def) => { + self.write_list_flag(pfx, name, def, w)?; + } + &Val::Boolean(_) | &Val::Empty | &Val::Float(_) | &Val::Int(_) | &Val::Str(_) => { + self.write_flag_name(pfx, name, w)?; + self.write_simple_value(val, w)?; } - } - &Val::Func(ref _def) => { - // This is ignored - eprintln!("Skipping macro..."); - } - &Val::Env(ref _fs) => { - // This is ignored - eprintln!("Skipping env..."); - } - &Val::Module(ref _def) => { - // This is ignored - eprintln!("Skipping module..."); } } Ok(()) @@ -129,13 +119,14 @@ impl FlagConverter { impl Converter for FlagConverter { fn convert(&self, v: Rc, mut w: &mut Write) -> ConvertResult { - if !v.is_tuple() { + if let &Val::Tuple(ref flds) = v.as_ref() { + self.write("", flds, &mut w) + } else { return Err(Box::new(BuildError::new( "Flag outputs must be a tuple", ErrorType::ConvertError, ))); } - self.write("", &v, &mut w) } fn file_ext(&self) -> String { diff --git a/src/convert/flags_help.txt b/src/convert/flags_help.txt index 9dd638a..c6ba3f4 100644 --- a/src/convert/flags_help.txt +++ b/src/convert/flags_help.txt @@ -3,10 +3,8 @@ Flags converts a tuple into a set of command line arguments for command line app The flags are converted using the following rules: - keys in a tuple are converted into the argument name. -- values in a tuple are converted into the argument value. -- NULL values are not emitted. -- lists expand out into an argument for each item in the list. - e.g. {{foo = [1, 2]}} becomes --foo=1 --foo=2 -- tuples expand out into an argument with the key as a prefix separated by a `.`. - e.g. {{foo = {{bar = 1, baz = 2}}}} becomes --foo.bar=1 --foo.baz=2 -- Functions and Modules are ignored. \ No newline at end of file +- values in a tuple are converted into the argument value with following exceptions: + - NULL values print out only the key name for the flag. + - lists expand out into an argument for each item in the list. + e.g. {{foo = [1, 2]}} becomes --foo=1 --foo=2 + - Tuples, Functions and Modules are ignored. \ No newline at end of file