From 3826840bf5287111d5493791ab8f019c0aa1096b Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Tue, 12 Nov 2024 13:38:33 +0530 Subject: [PATCH] JDebug --- v0.5/fastn-section/Cargo.toml | 3 +- v0.5/fastn-section/src/debug.rs | 44 ++++++++++++++---------------- v0.5/fastn-section/src/lib.rs | 6 ++-- v0.5/fastn-unresolved/src/debug.rs | 22 +++++++++++++++ v0.5/fastn-unresolved/src/lib.rs | 2 ++ 5 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 v0.5/fastn-unresolved/src/debug.rs diff --git a/v0.5/fastn-section/Cargo.toml b/v0.5/fastn-section/Cargo.toml index e9c1c0b92..4eff273dd 100644 --- a/v0.5/fastn-section/Cargo.toml +++ b/v0.5/fastn-section/Cargo.toml @@ -10,7 +10,6 @@ homepage.workspace = true [dependencies] serde.workspace = true - -[dev-dependencies] serde_json.workspace = true + diff --git a/v0.5/fastn-section/src/debug.rs b/v0.5/fastn-section/src/debug.rs index b095f149e..6ce1f1dd4 100644 --- a/v0.5/fastn-section/src/debug.rs +++ b/v0.5/fastn-section/src/debug.rs @@ -1,37 +1,35 @@ -pub trait JDebug { - fn debug(&self, source: &str) -> serde_json::Value; -} - fn span(s: &fastn_section::Span, key: &str, source: &str) -> serde_json::Value { serde_json::json!({ key: (source[s.start..s.end]).to_string()}) } -impl JDebug for fastn_section::Span { +impl fastn_section::JDebug for fastn_section::Span { fn debug(&self, source: &str) -> serde_json::Value { let t = &source[self.start..self.end]; if t.is_empty() { "" } else { t }.into() } } -impl JDebug for fastn_section::Spanned { +impl fastn_section::JDebug for fastn_section::Spanned { fn debug(&self, source: &str) -> serde_json::Value { self.value.debug(source) } } -impl JDebug for fastn_section::Spanned<()> { +impl fastn_section::JDebug for fastn_section::Spanned<()> { fn debug(&self, source: &str) -> serde_json::Value { span(&self.span, "spanned", source) } } -impl JDebug for Vec { +impl fastn_section::JDebug for Vec { fn debug(&self, source: &str) -> serde_json::Value { serde_json::Value::Array(self.iter().map(|v| v.debug(source)).collect()) } } -impl JDebug for std::collections::HashMap { +impl fastn_section::JDebug + for std::collections::HashMap +{ fn debug(&self, source: &str) -> serde_json::Value { let mut o = serde_json::Map::new(); for (k, v) in self { @@ -44,7 +42,7 @@ impl JDebug for std::collections::HashMap JDebug for Option { +impl fastn_section::JDebug for Option { fn debug(&self, source: &str) -> serde_json::Value { self.as_ref() .map(|v| v.debug(source)) @@ -52,13 +50,13 @@ impl JDebug for Option { } } -impl JDebug for fastn_section::Visibility { +impl fastn_section::JDebug for fastn_section::Visibility { fn debug(&self, _source: &str) -> serde_json::Value { format!("{self:?}").into() } } -impl JDebug for fastn_section::Document { +impl fastn_section::JDebug for fastn_section::Document { fn debug(&self, source: &str) -> serde_json::Value { let mut o = serde_json::Map::new(); if self.module_doc.is_some() { @@ -82,7 +80,7 @@ impl JDebug for fastn_section::Document { } } -impl JDebug for fastn_section::Section { +impl fastn_section::JDebug for fastn_section::Section { fn debug(&self, source: &str) -> serde_json::Value { // todo: add headers etc (only if they are not null) let mut o = serde_json::Map::new(); @@ -96,13 +94,13 @@ impl JDebug for fastn_section::Section { } } -impl JDebug for fastn_section::SectionInit { +impl fastn_section::JDebug for fastn_section::SectionInit { fn debug(&self, source: &str) -> serde_json::Value { self.name.debug(source) } } -impl JDebug for fastn_section::KindedName { +impl fastn_section::JDebug for fastn_section::KindedName { fn debug(&self, source: &str) -> serde_json::Value { let mut o = serde_json::Map::new(); if let Some(kind) = &self.kind { @@ -113,7 +111,7 @@ impl JDebug for fastn_section::KindedName { } } -impl JDebug for fastn_section::Kind { +impl fastn_section::JDebug for fastn_section::Kind { fn debug(&self, source: &str) -> serde_json::Value { if let Some(v) = self.to_identifier() { return v.debug(source); @@ -134,7 +132,7 @@ impl JDebug for fastn_section::Kind { } } -impl JDebug for fastn_section::QualifiedIdentifier { +impl fastn_section::JDebug for fastn_section::QualifiedIdentifier { fn debug(&self, source: &str) -> serde_json::Value { if self.terms.is_empty() { return self.module.debug(source); @@ -147,7 +145,7 @@ impl JDebug for fastn_section::QualifiedIdentifier { } } -impl JDebug for fastn_section::Tes { +impl fastn_section::JDebug for fastn_section::Tes { fn debug(&self, source: &str) -> serde_json::Value { match self { fastn_section::Tes::Text(e) => e.debug(source), @@ -157,13 +155,13 @@ impl JDebug for fastn_section::Tes { } } -impl JDebug for fastn_section::Identifier { +impl fastn_section::JDebug for fastn_section::Identifier { fn debug(&self, source: &str) -> serde_json::Value { self.name.debug(source) } } -impl JDebug for fastn_section::PackageName { +impl fastn_section::JDebug for fastn_section::PackageName { fn debug(&self, source: &str) -> serde_json::Value { format!( "{} as {}", @@ -174,7 +172,7 @@ impl JDebug for fastn_section::PackageName { } } -impl JDebug for fastn_section::AliasableIdentifier { +impl fastn_section::JDebug for fastn_section::AliasableIdentifier { fn debug(&self, source: &str) -> serde_json::Value { if self.alias.is_none() { return self.name.debug(source); @@ -187,7 +185,7 @@ impl JDebug for fastn_section::AliasableIdentifier { } } -impl JDebug for fastn_section::ModuleName { +impl fastn_section::JDebug for fastn_section::ModuleName { fn debug(&self, source: &str) -> serde_json::Value { if self.path.is_empty() && self.name.alias.is_none() @@ -216,7 +214,7 @@ impl JDebug for fastn_section::ModuleName { } } -impl JDebug for fastn_section::Error { +impl fastn_section::JDebug for fastn_section::Error { fn debug(&self, source: &str) -> serde_json::Value { error(self, &Default::default(), source) } diff --git a/v0.5/fastn-section/src/lib.rs b/v0.5/fastn-section/src/lib.rs index db54f978a..faf9d0165 100644 --- a/v0.5/fastn-section/src/lib.rs +++ b/v0.5/fastn-section/src/lib.rs @@ -13,8 +13,6 @@ mod utils; mod warning; mod wiggin; -#[cfg(test)] -pub(crate) use debug::JDebug; pub use error::Error; pub use fastn_section::parser::identifier::identifier; pub use fastn_section::parser::kind::kind; @@ -28,6 +26,10 @@ pub use fastn_section::parser::tes::tes; pub use fastn_section::warning::Warning; pub use scanner::{Scannable, Scanner}; +pub trait JDebug { + fn debug(&self, source: &str) -> serde_json::Value; +} + pub enum Diagnostic { Error(Error), Warning(Warning), diff --git a/v0.5/fastn-unresolved/src/debug.rs b/v0.5/fastn-unresolved/src/debug.rs new file mode 100644 index 000000000..32ea0f120 --- /dev/null +++ b/v0.5/fastn-unresolved/src/debug.rs @@ -0,0 +1,22 @@ +impl fastn_section::JDebug for fastn_unresolved::Import { + fn debug(&self, _source: &str) -> serde_json::Value { + let mut o = serde_json::Map::new(); + + o.insert( + "import".into(), + match self.alias { + Some(ref v) => format!("{}{}=>{}", self.package.0, self.module.0, v.0), + None => format!("{}{}", self.package.0, self.module.0), + } + .into(), + ); + + // if let Some(ref v) = self.exports { + // o.insert("exports".into(), v.debug(source)); + // } + // if let Some(ref v) = self.exposing { + // o.insert("exposing".into(), v.debug(source)); + // } + serde_json::Value::Object(o) + } +} diff --git a/v0.5/fastn-unresolved/src/lib.rs b/v0.5/fastn-unresolved/src/lib.rs index 81ead8a30..3e123a02f 100644 --- a/v0.5/fastn-unresolved/src/lib.rs +++ b/v0.5/fastn-unresolved/src/lib.rs @@ -4,6 +4,8 @@ extern crate self as fastn_unresolved; +#[cfg(test)] +mod debug; mod parser; mod utils;