Skip to content

Commit

Permalink
minimal replacment. needs nested tables implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderARodin committed Feb 7, 2024
1 parent 91231b0 commit db5062b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 23 deletions.
47 changes: 47 additions & 0 deletions src/call_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,52 @@
// CORE
// // // // // // // //

#[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() )
)
},
}
}
}

impl PartialEq for CallItem {
fn eq( &self, rh: &Self ) -> bool {
match (self, rh) {
(CallItem::Item( lh_a, None), CallItem::Item( rh_a, None )) => {
lh_a == rh_a
},
(CallItem::Item( lh_a, Some(lh_b) ), CallItem::Item( rh_a, Some( rh_b) )) => {
if lh_a != rh_a {
return false;
}
return lh_b == rh_b;
},
_ => false,
}
}
}

/*
#[allow(dead_code)]
#[derive(Debug)]
pub enum CallItem {
Expand All @@ -24,6 +70,7 @@ impl PartialEq for CallItem {
}
}
}
*/

// // // // // // // //
// TESTs
Expand Down
5 changes: 3 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ impl Parser<'_> {
// // // // // // //
fn push_with_param( &mut self, key: &str, param: &str ) -> ResultOf< () > {
self.list.push(
CallItem::WithParam(key.to_string(), param.to_string())
CallItem::new( key )
.append(param)
);
Ok(())
}
fn push_simple_item( &mut self, cmd: &str ) -> ResultOf< () > {
self.list.push(
CallItem::Simple(cmd.to_string())
CallItem::new(cmd)
);
Ok(())
}
Expand Down
42 changes: 21 additions & 21 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ fn simple_table() {
"#
.parse::<Table>().unwrap();
let validator = vec![
CallItem::WithParam("a".to_string(), "A".to_string()),
CallItem::WithParam("b".to_string(), "B".to_string()),
CallItem::Simple("branch".to_string()),
CallItem::WithParam("some-thing".to_string(), "some-think".to_string()),
CallItem::new("a").append("A"),
CallItem::new("b").append("B"),
CallItem::new("branch"),
CallItem::new("some-thing").append("some-think"),
];
let mist;
match from_toml_table( &tml, "sc2" ) {
Expand Down Expand Up @@ -51,13 +51,13 @@ fn subscripts_array() {
"#
.parse::<Table>().unwrap();
let validator = vec![
CallItem::Simple("the0".to_string()),
CallItem::Simple("the2".to_string()),
CallItem::Simple("branch".to_string()),
CallItem::Simple("the3".to_string()),
CallItem::Simple("the4".to_string()),
CallItem::Simple("the5".to_string()),
CallItem::Simple("fin".to_string()),
CallItem::new("the0"),
CallItem::new("the2"),
CallItem::new("branch"),
CallItem::new("the3"),
CallItem::new("the4"),
CallItem::new("the5"),
CallItem::new("fin"),
];
let mist;
match from_toml_table( &tml, "workflows.run0" ) {
Expand Down Expand Up @@ -91,14 +91,14 @@ fn ok_with_nesting_lvl5() {
"#
.parse::<Table>().unwrap();
let validator = vec![
CallItem::Simple("the0".to_string()),
CallItem::Simple("the1".to_string()),
CallItem::Simple("the2".to_string()),
CallItem::Simple("the3".to_string()),
CallItem::Simple("the4".to_string()),
CallItem::Simple("the5".to_string()),
CallItem::Simple("fin".to_string()),
CallItem::Simple("branch".to_string()),
CallItem::new("the0"),
CallItem::new("the1"),
CallItem::new("the2"),
CallItem::new("the3"),
CallItem::new("the4"),
CallItem::new("the5"),
CallItem::new("fin"),
CallItem::new("branch"),
];
let mist;
match from_toml_table( &tml, "workflows.run0" ) {
Expand Down Expand Up @@ -149,8 +149,8 @@ fn check_simple() {
"#
.parse::<Table>().unwrap();
let validator = vec![
CallItem::Simple("the".to_string()),
CallItem::Simple("script".to_string()),
CallItem::new("the"),
CallItem::new("script"),
];
let mist;
match from_toml_table( &tml, "workflows.script" ) {
Expand Down
56 changes: 56 additions & 0 deletions tests/new_feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@


#[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() )
)
},
}
}
}

// // // // // // // //
// TESTs
// // // // // // // //

#[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 );
}
}
}

0 comments on commit db5062b

Please sign in to comment.