Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Oct 30, 2024
1 parent c599ff3 commit fd6aade
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions v0.5/fastn-p1/src/wiggin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ fn inner_ender<T: SectionProxy>(
) -> Vec<T> {
let mut stack = Vec::new();
'outer: for mut section in sections {
match section.name(source).unwrap() {
match section.mark(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() {
match candidate.mark(source).unwrap() {
Mark::Start(name) => {
if name == e_name {
section.add_children(children);
Expand All @@ -107,16 +107,16 @@ fn inner_ender<T: SectionProxy>(
stack
}

pub enum Mark<'input> {
enum Mark<'input> {
Start(&'input str),
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
pub trait SectionProxy: Sized {
trait SectionProxy: Sized {
/// returns the name of the section, and if it starts or ends the section
fn name<'input>(
fn mark<'input>(
&'input self,
source: &'input str,
) -> Result<Mark<'input>, fastn_p1::SingleError>;
Expand All @@ -125,7 +125,7 @@ pub trait SectionProxy: Sized {
}

impl SectionProxy for fastn_p1::Section {
fn name<'input>(
fn mark<'input>(
&'input self,
source: &'input str,
) -> Result<Mark<'input>, fastn_p1::SingleError> {
Expand Down Expand Up @@ -174,7 +174,7 @@ mod test {
}

impl super::SectionProxy for DummySection {
fn name<'input>(
fn mark<'input>(
&'input self,
_source: &'input str,
) -> Result<super::Mark<'input>, fastn_p1::SingleError> {
Expand Down Expand Up @@ -217,6 +217,7 @@ mod test {
// foo containing bar and baz will look like this: foo [bar [], baz []]
fn to_str(sections: &[DummySection]) -> String {
fn to_str_(s: &mut String, sections: &[DummySection]) {
// we are using peekable iterator so we can check if we are at the end
let mut iterator = sections.iter().peekable();
while let Some(section) = iterator.next() {
s.push_str(&section.name);
Expand Down

0 comments on commit fd6aade

Please sign in to comment.