Skip to content

Commit

Permalink
minor fixes amend
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust committed Jan 18, 2024
1 parent f97d097 commit 5ddbaf6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
10 changes: 5 additions & 5 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ pub mod script_document;

/// Purely utility trait to not repeat code when implementing Debug trait for printing regular debug string or pretty debug string
trait DebugMaybeAlternate {
fn debug_maybe_alternate(&mut self, value: &dyn core::fmt::Debug) -> core::fmt::Result;
fn debug_maybe_alternate_named(&mut self, name: &str, value: &dyn core::fmt::Debug) -> core::fmt::Result;
fn debug_maybe_alternate(&mut self, value: &dyn std::fmt::Debug) -> std::fmt::Result;
fn debug_maybe_alternate_named(&mut self, name: &str, value: &dyn std::fmt::Debug) -> std::fmt::Result;
}

impl DebugMaybeAlternate for core::fmt::Formatter<'_> {
fn debug_maybe_alternate(&mut self, value: &dyn core::fmt::Debug) -> core::fmt::Result {
impl DebugMaybeAlternate for std::fmt::Formatter<'_> {
fn debug_maybe_alternate(&mut self, value: &dyn std::fmt::Debug) -> std::fmt::Result {
if self.alternate() {
write!(self, "{value:#?}")
} else {
write!(self, "{value:?}")
}
}

fn debug_maybe_alternate_named(&mut self, name: &str, value: &dyn core::fmt::Debug) -> core::fmt::Result {
fn debug_maybe_alternate_named(&mut self, name: &str, value: &dyn std::fmt::Debug) -> std::fmt::Result {
if self.alternate() {
write!(self, "{name} {value:#?}")
} else {
Expand Down
22 changes: 16 additions & 6 deletions crates/core/src/tokens/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::Debug;
use std::str::ParseBoolError;
use shrinkwraprs::Shrinkwrap;
use thiserror::Error;
use crate::DebugMaybeAlternate;
use crate::script_document::ScriptDocument;
use crate::{AnyNode, NamedSyntaxNode, SyntaxNode, ast::{ExpressionTraversal, ExpressionVisitor}};

Expand Down Expand Up @@ -246,7 +247,7 @@ impl<'script> TryFrom<AnyNode<'script>> for LiteralNullNode<'script> {
}


#[derive(Debug, Clone, PartialEq)]
#[derive(Clone, PartialEq)]
pub enum Literal<'script> {
Int(LiteralIntNode<'script>),
Float(LiteralFloatNode<'script>),
Expand All @@ -256,6 +257,19 @@ pub enum Literal<'script> {
Null(LiteralNullNode<'script>)
}

impl Debug for Literal<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Int(n) => f.debug_maybe_alternate(n),
Self::Float(n) => f.debug_maybe_alternate(n),
Self::Bool(n) => f.debug_maybe_alternate(n),
Self::String(n) => f.debug_maybe_alternate(n),
Self::Name(n) => f.debug_maybe_alternate(n),
Self::Null(n) => f.debug_maybe_alternate(n),
}
}
}

pub type LiteralNode<'script> = SyntaxNode<'script, Literal<'script>>;

impl NamedSyntaxNode for LiteralNode<'_> {
Expand All @@ -279,11 +293,7 @@ impl LiteralNode<'_> {

impl Debug for LiteralNode<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
write!(f, "{:#?}", self.value())
} else {
write!(f, "{:?}", self.value())
}
f.debug_maybe_alternate(&self.value())
}
}

Expand Down
9 changes: 8 additions & 1 deletion crates/core/src/tokens/unnamed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::str::FromStr;
use crate::{tokens::Keyword, SyntaxNode, AnyNode};
use std::fmt::Debug;
use crate::{tokens::Keyword, SyntaxNode, AnyNode, DebugMaybeAlternate};


#[derive(Debug, Clone)]
Expand All @@ -21,6 +22,12 @@ impl UnnamedNode<'_> {
}
}

impl Debug for UnnamedNode<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_maybe_alternate(&self.value())
}
}

impl<'script> TryFrom<AnyNode<'script>> for UnnamedNode<'script> {
type Error = ();

Expand Down

0 comments on commit 5ddbaf6

Please sign in to comment.