implement debug and display for Command, Trait and Type

This commit is contained in:
Schrottkatze 2023-11-19 01:44:27 +01:00
parent a07a031e0c
commit 75be6e22a0
5 changed files with 123 additions and 3 deletions

View file

@ -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

View file

@ -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 {

View file

@ -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<HashSet<usize>>,

View file

@ -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<HashSet<usize>>,
pub(super) name: String,

View file

@ -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<InternalTypeDef>),