From b31e95567ddf07a9760058a21a3c9bd584feb572 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Tue, 6 Mar 2018 19:39:10 -0600 Subject: [PATCH] Add embedded struct output support for flags. We use a dotted syntax for now but we might want to support other separators in a future commit. --- Cargo.toml | 2 +- TODO.md | 2 ++ examples/env.txt | 1 - examples/flags.txt | 1 - examples/test.ucg | 6 ++++-- src/ast.rs | 6 ++++++ src/convert/flags.rs | 19 ++++++++++++------- 7 files changed, 25 insertions(+), 12 deletions(-) delete mode 100644 examples/env.txt delete mode 100644 examples/flags.txt diff --git a/Cargo.toml b/Cargo.toml index 166bf11..4a7eb49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ucg" -version = "0.0.1" +version = "0.0.2" authors = ["Jeremy Wall "] description = "A configuration generation grammar." repository = "https://github.com/zaphar/ucg" diff --git a/TODO.md b/TODO.md index 10dc2ce..f824b2a 100644 --- a/TODO.md +++ b/TODO.md @@ -17,4 +17,6 @@ organiztion for a given configuration structure. Some options here could be # Minor Fixes and Polish +* Flags should allow different seperators for prefixed flags. +* None value types * YAML export \ No newline at end of file diff --git a/examples/env.txt b/examples/env.txt deleted file mode 100644 index bea1788..0000000 --- a/examples/env.txt +++ /dev/null @@ -1 +0,0 @@ -dbconn_list=db_conn1='db1.prod.net:3306/testdb' db_conn2='db2.prod.net:3306/testdb' tmpldir='./templates' \ No newline at end of file diff --git a/examples/flags.txt b/examples/flags.txt deleted file mode 100644 index 1e97b10..0000000 --- a/examples/flags.txt +++ /dev/null @@ -1 +0,0 @@ ---db_conn1 'db1.prod.net:3306/testdb' --db_conn2 'db2.prod.net:3306/testdb' --dbconn_list --tmpldir './templates' \ No newline at end of file diff --git a/examples/test.ucg b/examples/test.ucg index f5c1921..79f8933 100644 --- a/examples/test.ucg +++ b/examples/test.ucg @@ -19,8 +19,10 @@ let db_conns = [db_conn1.conn_string, db_conn2.conn_string]; // Our server configuration. let server_config = { - dbconn_list = db_conns, db_conn1 = db_conns.0, // connection one db_conn2 = db_conns.1, - tmpldir = "./templates" + tmpldir = "./templates", + prefix = { + foo = "bar" + } }; \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index 4c14503..c17e6d7 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -437,6 +437,12 @@ pub struct Positioned { pub val: T, } +impl std::fmt::Display for Positioned { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "{}", self.val) + } +} + impl Positioned { /// Constructs a new Positioned with a value, line, and column information. pub fn new(v: T, l: usize, c: usize) -> Self { diff --git a/src/convert/flags.rs b/src/convert/flags.rs index d628a00..a7037e4 100644 --- a/src/convert/flags.rs +++ b/src/convert/flags.rs @@ -28,7 +28,7 @@ impl FlagConverter { FlagConverter {} } - fn write(&self, v: &Val, w: &mut Write) -> Result<()> { + fn write(&self, pfx: &str, v: &Val, w: &mut Write) -> Result<()> { match v { &Val::Float(ref f) => { try!(write!(w, "{} ", f)); @@ -46,12 +46,17 @@ impl FlagConverter { &Val::Tuple(ref flds) => { for &(ref name, ref val) in flds.iter() { if val.is_tuple() { - eprintln!("Skipping embedded tuple..."); - return Ok(()); + let new_pfx = format!("{}{}.", pfx, name); + try!(self.write(&new_pfx, val, w)); + } else { + if name.val.chars().count() > 1 || pfx.chars().count() > 0 { + try!(write!(w, "--{}{} ", pfx, name.val)); + } else { + try!(write!(w, "-{} ", name.val)); + } + // TODO(jwall): What if the value is a tuple? + try!(self.write(pfx, &val, w)); } - try!(write!(w, "--{} ", name.val)); - // TODO(jwall): What if the value is a tuple? - try!(self.write(&val, w)); } } &Val::Macro(ref _def) => { @@ -65,6 +70,6 @@ impl FlagConverter { impl Converter for FlagConverter { fn convert(&self, v: Rc, mut w: Box) -> Result<()> { - self.write(&v, &mut w) + self.write("", &v, &mut w) } }