Skip to content

Commit

Permalink
Added fastn_section::parser::section
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Nov 4, 2024
1 parent fa22360 commit 2365e17
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
15 changes: 9 additions & 6 deletions v0.5/fastn-section/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,20 @@ impl JDebug for fastn_section::Document {
impl JDebug for fastn_section::Section {
fn debug(&self, source: &str) -> serde_json::Value {
// todo: add headers etc (only if they are not null)
serde_json::json! ({
"init": self.init.debug(source),
})
let mut o = serde_json::Map::new();
o.insert("init".to_string(), self.init.debug(source));

if let Some(c) = &self.caption {
o.insert("caption".to_string(), c.debug(source));
}

serde_json::Value::Object(o)
}
}

impl JDebug for fastn_section::SectionInit {
fn debug(&self, source: &str) -> serde_json::Value {
serde_json::json! ({
"name": self.name.debug(source)
})
self.name.debug(source)
}
}

Expand Down
2 changes: 2 additions & 0 deletions v0.5/fastn-section/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub use fastn_section::parser::kinded_name::kinded_name;
pub use fastn_section::parser::module_name::module_name;
pub use fastn_section::parser::package_name::package_name;
pub use fastn_section::parser::qualified_identifier::qualified_identifier;
pub use fastn_section::parser::section_init::section_init;
pub use fastn_section::parser::tes::tes;
pub use fastn_section::warning::Warning;
pub use scanner::{Scannable, Scanner};

Expand Down
1 change: 1 addition & 0 deletions v0.5/fastn-section/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(super) mod kinded_name;
pub(super) mod module_name;
pub(super) mod package_name;
pub(super) mod qualified_identifier;
mod section;
pub(super) mod section_init;
pub(super) mod tes;
pub(super) mod visibility;
Expand Down
32 changes: 32 additions & 0 deletions v0.5/fastn-section/src/parser/section.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#[allow(dead_code)]
pub fn section(
scanner: &mut fastn_section::Scanner<fastn_section::Document>,
) -> Option<fastn_section::Section> {
let section_init = fastn_section::section_init(scanner)?;

scanner.skip_spaces();
let caption = fastn_section::tes(scanner);

// TODO: implement headers, body, children, sub-sections
Some(fastn_section::Section {
init: section_init,
caption,
headers: vec![],
body: None,
children: vec![],
sub_sections: vec![],
function_marker: None,
is_commented: false,
has_end: false,
})
}

#[cfg(test)]
mod test {
fastn_section::tt!(super::section);

#[test]
fn section() {
t!("-- foo: Hello World", {"init": {"name": "foo"}, "caption": ["Hello World"]});
}
}
10 changes: 5 additions & 5 deletions v0.5/fastn-section/src/parser/section_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ mod test {

#[test]
fn section_init() {
t!("-- foo:", {"name": {"name": "foo"}});
t!("-- foo: ", {"name": {"name": "foo"}}, " ");
t!("-- foo: hello", {"name": {"name": "foo"}}, " hello");
t!("-- integer foo: hello", {"name": {"name": "foo", "kind": "integer"}}, " hello");
t!("-- integer héllo: foo", {"name": {"name": "héllo", "kind": "integer"}}, " foo");
t!("-- foo:", {"name": "foo"});
t!("-- foo: ", {"name": "foo"}, " ");
t!("-- foo: hello", {"name": "foo"}, " hello");
t!("-- integer foo: hello", {"name": "foo", "kind": "integer"}, " hello");
t!("-- integer héllo: foo", {"name": "héllo", "kind": "integer"}, " foo");
// t!("-- list<integer> foo:", {"name": {"name": "foo", "kind": "integer"}}, "");
}
}

0 comments on commit 2365e17

Please sign in to comment.