Skip to content

Commit

Permalink
add nested sub-tables
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderARodin committed Feb 8, 2024
1 parent db5062b commit 1163dde
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 53 deletions.
3 changes: 3 additions & 0 deletions src/call_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) => {
Expand Down
47 changes: 39 additions & 8 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) => {
Expand All @@ -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 )
Expand All @@ -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!( "<construct_table_callitem>: 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!( "<construct_table_callitem>: block must be unreachable" );
return Err(Box::from( msg ));
},
_ => {
let msg = format!( "<construct_table_callitem>: unsupported table item <{}>", value );
return Err(Box::from( msg ));
},
}
}

82 changes: 37 additions & 45 deletions tests/new_feature.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,48 @@


#[allow(dead_code)]
#[derive(Debug)]
pub enum CallItem {
Item( Box<str>, Option< Box<CallItem> > ),
}
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::<Table>().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} <<");
}

0 comments on commit 1163dde

Please sign in to comment.