From 75be6e22a0f5ff0f3b5100027af61a9bca8f3e20 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Sun, 19 Nov 2023 01:44:27 +0100 Subject: [PATCH] implement debug and display for Command, Trait and Type --- src/namespace/command.rs | 27 ++++++++++++++++++ src/namespace/mod.rs | 6 ++-- src/namespace/trait.rs | 16 +++++++++++ src/namespace/type.rs | 16 +++++++++++ src/namespace/typedef.rs | 61 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 3 deletions(-) diff --git a/src/namespace/command.rs b/src/namespace/command.rs index d835fd8..4e8be09 100644 --- a/src/namespace/command.rs +++ b/src/namespace/command.rs @@ -1,3 +1,6 @@ +use std::fmt::Debug; +use std::fmt::Display; + use super::typedef::{InternalTypeDef, TypeDef}; use super::GlobalNamespace; @@ -23,6 +26,30 @@ impl<'a> Command<'a> { } } +impl Display for Command<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let name = &self.namespace.commands.borrow()[self.id].name; + f.write_fmt(format_args!( + "{name} {} -> {}", + self.get_input_types() + .map_or("!".to_string(), |v| v.to_string()), + self.get_output_types() + .map_or("!".to_string(), |v| v.to_string()) + )) + } +} + +impl Debug for Command<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let name = &self.namespace.commands.borrow()[self.id].name; + f.write_fmt(format_args!( + "{name:?} {:?} -> {:?}", + self.get_input_types(), + self.get_output_types() + )) + } +} + pub(super) struct InternalCommand { pub(super) name: String, // gosh this is hacky diff --git a/src/namespace/mod.rs b/src/namespace/mod.rs index 3edce2f..1a95363 100644 --- a/src/namespace/mod.rs +++ b/src/namespace/mod.rs @@ -10,9 +10,9 @@ use self::{ typedef::TypeDef, }; -mod command; -mod r#trait; -mod r#type; +pub mod command; +pub mod r#trait; +pub mod r#type; pub mod typedef; pub struct GlobalNamespace { diff --git a/src/namespace/trait.rs b/src/namespace/trait.rs index 47b8921..fe84732 100644 --- a/src/namespace/trait.rs +++ b/src/namespace/trait.rs @@ -1,6 +1,8 @@ use std::collections::HashSet; use std::cell::RefCell; +use std::fmt::Debug; +use std::fmt::Display; use super::GlobalNamespace; @@ -10,6 +12,20 @@ pub struct Trait<'a> { pub(super) namespace: &'a GlobalNamespace, } +impl Display for Trait<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let name = &self.namespace.traits.borrow()[self.id].name; + + f.write_fmt(format_args!("{name}")) + } +} + +impl Debug for Trait<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Trait({self})") + } +} + pub(super) struct InternalTrait { // make resolution easier pub(super) types: RefCell>, diff --git a/src/namespace/type.rs b/src/namespace/type.rs index 52df2de..010e6e7 100644 --- a/src/namespace/type.rs +++ b/src/namespace/type.rs @@ -1,6 +1,8 @@ use std::collections::HashSet; use std::cell::RefCell; +use std::fmt::Debug; +use std::fmt::Display; use super::Trait; @@ -31,6 +33,20 @@ impl<'a> Type<'a> { } } +impl Display for Type<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let name = &self.namespace.types.borrow()[self.id].name; + + f.write_fmt(format_args!("{name}")) + } +} + +impl Debug for Type<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Type({self})") + } +} + pub(super) struct InternalType { pub(super) traits: RefCell>, pub(super) name: String, diff --git a/src/namespace/typedef.rs b/src/namespace/typedef.rs index 81c9332..6c468d7 100644 --- a/src/namespace/typedef.rs +++ b/src/namespace/typedef.rs @@ -1,3 +1,6 @@ +use std::fmt::Debug; +use std::fmt::Display; + use super::TypeNamespaceId; use super::GlobalNamespace; @@ -34,6 +37,64 @@ impl<'a> TypeDef<'a> { } } +impl Display for TypeDef<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + TypeDef::Type(t) => Display::fmt(&t, f), + TypeDef::Trait(t) => Display::fmt(&t, f), + TypeDef::List(l) => { + f.write_str("[ ")?; + for (i, item) in l.iter().enumerate() { + if i != 0 { + f.write_str(", ")?; + } + Display::fmt(&item, f)?; + } + f.write_str(" ]") + } + TypeDef::Record(rec) => { + f.write_str("{ ")?; + for (i, item) in rec.iter().enumerate() { + if i != 0 { + f.write_str(", ")?; + } + f.write_fmt(format_args!("{}: {}", item.0, item.1))?; + } + f.write_str(" }") + } + } + } +} + +impl Debug for TypeDef<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + TypeDef::Type(t) => Debug::fmt(&t, f), + TypeDef::Trait(t) => Debug::fmt(&t, f), + TypeDef::List(l) => { + f.write_str("[ ")?; + for (i, item) in l.iter().enumerate() { + if i != 0 { + f.write_str(", ")?; + } + Debug::fmt(&item, f)?; + } + f.write_str(" ]") + } + TypeDef::Record(rec) => { + f.write_str("{ ")?; + for (i, item) in rec.iter().enumerate() { + if i != 0 { + f.write_str(", ")?; + } + f.write_fmt(format_args!("{:?}: {:?}", item.0, item.1))?; + } + f.write_str(" }") + } + } + } +} + pub(super) enum InternalTypeDef { Single(TypeNamespaceId), List(Vec),