From 2365e179161edd2541981e9a12d8ca0a4e5aa8cb Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Mon, 4 Nov 2024 17:01:48 +0530 Subject: [PATCH] Added fastn_section::parser::section --- v0.5/fastn-section/src/debug.rs | 15 +++++---- v0.5/fastn-section/src/lib.rs | 2 ++ v0.5/fastn-section/src/parser/mod.rs | 1 + v0.5/fastn-section/src/parser/section.rs | 32 +++++++++++++++++++ v0.5/fastn-section/src/parser/section_init.rs | 10 +++--- 5 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 v0.5/fastn-section/src/parser/section.rs diff --git a/v0.5/fastn-section/src/debug.rs b/v0.5/fastn-section/src/debug.rs index fca86ac47..1bdcbf00f 100644 --- a/v0.5/fastn-section/src/debug.rs +++ b/v0.5/fastn-section/src/debug.rs @@ -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) } } diff --git a/v0.5/fastn-section/src/lib.rs b/v0.5/fastn-section/src/lib.rs index b2f76a9b0..97ccb45ff 100644 --- a/v0.5/fastn-section/src/lib.rs +++ b/v0.5/fastn-section/src/lib.rs @@ -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}; diff --git a/v0.5/fastn-section/src/parser/mod.rs b/v0.5/fastn-section/src/parser/mod.rs index f369436ba..61788a0e5 100644 --- a/v0.5/fastn-section/src/parser/mod.rs +++ b/v0.5/fastn-section/src/parser/mod.rs @@ -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; diff --git a/v0.5/fastn-section/src/parser/section.rs b/v0.5/fastn-section/src/parser/section.rs new file mode 100644 index 000000000..0d84d4583 --- /dev/null +++ b/v0.5/fastn-section/src/parser/section.rs @@ -0,0 +1,32 @@ +#[allow(dead_code)] +pub fn section( + scanner: &mut fastn_section::Scanner, +) -> Option { + 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"]}); + } +} diff --git a/v0.5/fastn-section/src/parser/section_init.rs b/v0.5/fastn-section/src/parser/section_init.rs index 972d3a695..ceaae2a2e 100644 --- a/v0.5/fastn-section/src/parser/section_init.rs +++ b/v0.5/fastn-section/src/parser/section_init.rs @@ -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 foo:", {"name": {"name": "foo", "kind": "integer"}}, ""); } }