Skip to content

Commit

Permalink
untested inner_ender implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Oct 30, 2024
1 parent 4fb23fd commit a1bb52e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions v0.5/fastn-p1/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,6 @@ fn error(e: &fastn_p1::SingleError, _s: &fastn_p1::Span, _source: &str) -> serde
fastn_p1::SingleError::KindedNameNotFound => "kinded_name_not_found",
fastn_p1::SingleError::SectionNameNotFoundForEnd => "section_name_not_found_for_end",
fastn_p1::SingleError::EndContainsData => "end_contains_data",
fastn_p1::SingleError::EndWithoutStart => "end_without_start",
}})
}
1 change: 1 addition & 0 deletions v0.5/fastn-p1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ pub enum SingleError {
ColonNotFound,
SectionNameNotFoundForEnd,
EndContainsData,
EndWithoutStart,
// SectionNotFound(&'a str),
// MoreThanOneCaption,
// ParseError,
Expand Down
53 changes: 45 additions & 8 deletions v0.5/fastn-p1/src/wiggin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ fn section_ender(
if let Some(caption) = section.caption {
section.caption = Some(header_value_ender(source, o, caption));
}

section.headers = section
.headers
.into_iter()
Expand Down Expand Up @@ -69,30 +68,60 @@ fn header_value_ender(
/// example:
/// [{section: "foo"}, {section: "bar"}, "-- end: foo"] -> [{section: "foo", children: [{section: "bar"}]}]
fn inner_ender<T: SectionProxy>(
_source: &str,
_o: &mut fastn_p1::ParseOutput,
_sections: Vec<T>,
source: &str,
o: &mut fastn_p1::ParseOutput,
sections: Vec<T>,
) -> Vec<T> {
todo!()
let mut stack = Vec::new();
'outer: for mut section in sections {
match section.name(source).unwrap() {
Mark::Start(_name) => {
stack.push(section);
}
Mark::End(e_name) => {
let mut children = Vec::new();
while let Some(candidate) = stack.pop() {
match candidate.name(source).unwrap() {
Mark::Start(name) => {
if name == e_name {
section.add_children(children);
stack.push(section);
continue 'outer;
} else {
children.push(candidate);
}
}
Mark::End(_name) => unreachable!("we never put section end on the stack"),
}
}
// we have run out of sections, and we have not found the section end, return
// error, put the children back on the stack
o.items.push(fastn_p1::Spanned {
span: section.span(),
value: fastn_p1::Item::Error(fastn_p1::SingleError::EndWithoutStart),
});
stack.extend(children.into_iter().rev());
}
}
}
stack
}

pub enum Mark<'input> {
#[expect(dead_code)]
Start(&'input str),
#[expect(dead_code)]
End(&'input str),
}

/// we are using a proxy trait so we can write tests against a fake type, and then implement the
/// trait for the real Section type
#[expect(unused)]
pub trait SectionProxy: Sized {
/// returns the name of the section, and if it starts or ends the section
fn name<'input>(
&'input self,
source: &'input str,
) -> Result<Mark<'input>, fastn_p1::SingleError>;
fn add_children(&mut self, children: Vec<Self>);
fn span(&self) -> fastn_p1::Span;
}

impl SectionProxy for fastn_p1::Section {
Expand Down Expand Up @@ -129,6 +158,10 @@ impl SectionProxy for fastn_p1::Section {
fn add_children(&mut self, children: Vec<Self>) {
self.children = children;
}

fn span(&self) -> fastn_p1::Span {
self.init.dashdash.clone()
}
}

#[allow(dead_code)] // #[expect(dead_code)] is not working
Expand All @@ -153,4 +186,8 @@ impl SectionProxy for DummySection {
fn add_children(&mut self, children: Vec<Self>) {
self.children = children;
}

fn span(&self) -> fastn_p1::Span {
Default::default()
}
}

0 comments on commit a1bb52e

Please sign in to comment.