diff --git a/src/call_item.rs b/src/call_item.rs index 95a85bf..6bc2145 100644 --- a/src/call_item.rs +++ b/src/call_item.rs @@ -13,6 +13,9 @@ impl CallItem { pub fn new( s: &str ) -> Self { Self::Item( s.into(), None ) } + pub fn new_pair( s: &str, nested: CallItem ) -> Self { + Self::Item( s.into(), Some(Box::new(nested)) ) + } pub fn append(&self, s2: &str) -> Self { match &self { Self::Item( a, None ) => { diff --git a/src/parser.rs b/src/parser.rs index 5b6e42e..e27a941 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -83,12 +83,6 @@ impl Parser<'_> { } Ok(()) } - fn iterate_table( &mut self, tbl: &toml::Table ) -> ResultOf< () > { - for (key, value) in tbl { - self.process_one_pair( key, value )?; - } - Ok(()) - } fn follow_subscript_link( &mut self, link: &Value ) -> ResultOf< () > { match link { @@ -102,6 +96,13 @@ impl Parser<'_> { } } + fn iterate_table( &mut self, tbl: &toml::Table ) -> ResultOf< () > { + for (key, value) in tbl { + self.push_table_item( key, value )?; + //self.process_one_pair( key, value )?; + } + Ok(()) + } fn process_one_pair( &mut self, key: &str, value: &Value ) -> ResultOf< () > { match value { Value::String(param) => { @@ -114,8 +115,14 @@ impl Parser<'_> { } } - // // // // // // // + fn push_table_item( &mut self, key: &str, value: &Value ) -> ResultOf< () > { + let item: CallItem = construct_table_callitem( key, value )?; + self.list.push( + item + ); + Ok(()) + } fn push_with_param( &mut self, key: &str, param: &str ) -> ResultOf< () > { self.list.push( CallItem::new( key ) @@ -130,5 +137,29 @@ impl Parser<'_> { Ok(()) } } - + // // // // // // // +fn construct_table_callitem( key: &str, value: &Value ) -> ResultOf< CallItem > { + match value { + Value::String( s ) => { + return Ok( CallItem::new( key ).append( s ) ); + }, + Value::Table( sub_tbl ) => { + if sub_tbl.len() > 1 { + let msg = format!( ": subtable must have single item" ); + return Err(Box::from( msg )); + } + for (sub_key,sub_value) in sub_tbl.iter() { + return Ok( CallItem::new_pair( key, + construct_table_callitem(sub_key, sub_value)? + ) ); + } + let msg = format!( ": block must be unreachable" ); + return Err(Box::from( msg )); + }, + _ => { + let msg = format!( ": unsupported table item <{}>", value ); + return Err(Box::from( msg )); + }, + } +} diff --git a/tests/new_feature.rs b/tests/new_feature.rs index ac0f029..ca05b16 100644 --- a/tests/new_feature.rs +++ b/tests/new_feature.rs @@ -1,56 +1,48 @@ -#[allow(dead_code)] -#[derive(Debug)] -pub enum CallItem { - Item( Box, Option< Box > ), -} -impl CallItem { - pub fn new( s: &str ) -> Self { - Self::Item( s.into(), None ) - } - pub fn append(&self, s2: &str) -> Self { - match &self { - Self::Item( a, None ) => { - Self::Item( - a.clone(), - Some( Self::new( &s2 ).into() ) - ) - }, - Self::Item( a, Some(b) ) => { - let new_b = b.append(s2); - Self::Item( - a.clone(), - Some( new_b.into() ) - ) - }, - } - } -} +use toml::Table; +use raalog::log; + // // // // // // // // // TESTs // // // // // // // // +use call_list::{ from_toml_table, CallItem }; #[test] -fn NestedItems() { - let t = CallItem - ::new( "a" ) - .append( "b" ) - .append( "c" ) - .append( "d" ); - pr( 0, &t ); -} - -fn pr( n: i32, item: &CallItem ) { - match item { - CallItem::Item( a, None ) => { - println!( "{} --> ({}, None)", n, a ); - } - CallItem::Item( a, Some(b) ) => { - println!( "{} --> ({}, -- )", n, a ); - pr( n+1, &b ); - } +fn nested_tables() { + let tml = r#" + [workflows] + sc2 = [ + { a = 'A', b = 'B' }, + 'branch', + { some-thing = 'some-think' }, + 'beforing', + { root = { lvl2 = 'lvl3' } }, + { root = { lvl2 = { lvl3 = 'lvl4' } } }, + ] + "# + .parse::().unwrap(); + let validator = vec![ + CallItem::new("a").append("A"), + CallItem::new("b").append("B"), + CallItem::new("branch"), + CallItem::new("some-thing").append("some-think"), + CallItem::new("beforing"), + CallItem::new("root").append("lvl2").append("lvl3"), + CallItem::new("root").append("lvl2").append("lvl3").append("lvl4"), + ]; + let mist; + match from_toml_table( &tml, "workflows.sc2" ) { + Err(e) => { + mist = "must NOT be Errors"; + log::error(&e.to_string()); + }, + Ok(list) => { + mist = ""; + assert_eq!( list, validator, "list are NOT identical {:?} - {:?}", list, validator ); + }, } + assert!( mist == "", ">> {mist} <<"); }