Skip to content

Commit

Permalink
JDebug
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 12, 2024
1 parent 8d4f8e4 commit 3826840
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
3 changes: 1 addition & 2 deletions v0.5/fastn-section/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ homepage.workspace = true

[dependencies]
serde.workspace = true

[dev-dependencies]
serde_json.workspace = true


44 changes: 21 additions & 23 deletions v0.5/fastn-section/src/debug.rs
Original file line number Diff line number Diff line change
@@ -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() { "<empty>" } else { t }.into()
}
}

impl<T: JDebug> JDebug for fastn_section::Spanned<T> {
impl<T: fastn_section::JDebug> fastn_section::JDebug for fastn_section::Spanned<T> {
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<T: JDebug> JDebug for Vec<T> {
impl<T: fastn_section::JDebug> fastn_section::JDebug for Vec<T> {
fn debug(&self, source: &str) -> serde_json::Value {
serde_json::Value::Array(self.iter().map(|v| v.debug(source)).collect())
}
}

impl<T: JDebug> JDebug for std::collections::HashMap<fastn_section::Identifier, T> {
impl<T: fastn_section::JDebug> fastn_section::JDebug
for std::collections::HashMap<fastn_section::Identifier, T>
{
fn debug(&self, source: &str) -> serde_json::Value {
let mut o = serde_json::Map::new();
for (k, v) in self {
Expand All @@ -44,21 +42,21 @@ impl<T: JDebug> JDebug for std::collections::HashMap<fastn_section::Identifier,
}
}

impl<T: JDebug> JDebug for Option<T> {
impl<T: fastn_section::JDebug> fastn_section::JDebug for Option<T> {
fn debug(&self, source: &str) -> serde_json::Value {
self.as_ref()
.map(|v| v.debug(source))
.unwrap_or(serde_json::Value::Null)
}
}

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() {
Expand All @@ -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();
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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),
Expand All @@ -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 {}",
Expand All @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions v0.5/fastn-section/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand Down
22 changes: 22 additions & 0 deletions v0.5/fastn-unresolved/src/debug.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions v0.5/fastn-unresolved/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

extern crate self as fastn_unresolved;

#[cfg(test)]
mod debug;
mod parser;
mod utils;

Expand Down

0 comments on commit 3826840

Please sign in to comment.