cl-ast: Give Impl/ImplKind some love

This commit is contained in:
John 2024-04-14 18:01:30 -05:00
parent 3bebac6798
commit 89cd1393ed
3 changed files with 36 additions and 5 deletions

View File

@ -236,8 +236,20 @@ mod display {
} }
} }
impl Display for Impl { impl Display for Impl {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!("impl Display for Impl") let Self { target, body } = self;
write!(f, "impl {target} ")?;
delimit(|f| body.fmt(f), BRACES)(f)
}
}
impl Display for ImplKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ImplKind::Type(t) => t.fmt(f),
ImplKind::Trait { impl_trait, for_type } => {
write!(f, "{impl_trait} for {for_type}")
}
}
} }
} }
@ -551,6 +563,15 @@ mod convert {
Identifier(value.as_ref().into()) Identifier(value.as_ref().into())
} }
} }
impl<T: AsRef<str>> From<T> for PathPart {
fn from(value: T) -> Self {
match value.as_ref() {
"Self" => PathPart::SelfKw,
"super" => PathPart::SuperKw,
ident => PathPart::Ident(ident.into()),
}
}
}
macro impl_from ($(impl From for $T:ty {$($from:ty => $to:expr),*$(,)?})*) {$($( macro impl_from ($(impl From for $T:ty {$($from:ty => $to:expr),*$(,)?})*) {$($(
impl From<$from> for $T { impl From<$from> for $T {

View File

@ -205,14 +205,14 @@ pub enum VariantKind {
/// Sub-[items](Item) (associated functions, etc.) for a [Ty] /// Sub-[items](Item) (associated functions, etc.) for a [Ty]
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Impl { pub struct Impl {
pub target: Ty, pub target: ImplKind,
pub body: Vec<Item>, pub body: File,
} }
// TODO: `impl` Trait for <Target> { } // TODO: `impl` Trait for <Target> { }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum ImplKind { pub enum ImplKind {
Type(Box<Ty>), Type(Ty),
Trait { impl_trait: Path, for_type: Box<Ty> }, Trait { impl_trait: Path, for_type: Box<Ty> },
} }

View File

@ -312,6 +312,16 @@ pub mod yamlify {
y.key("Impl").pair("target", target).pair("body", body); y.key("Impl").pair("target", target).pair("body", body);
} }
} }
impl Yamlify for ImplKind {
fn yaml(&self, y: &mut Yamler) {
match self {
ImplKind::Type(t) => y.value(t),
ImplKind::Trait { impl_trait, for_type } => {
y.pair("trait", impl_trait).pair("for_type", for_type)
}
};
}
}
impl Yamlify for Block { impl Yamlify for Block {
fn yaml(&self, y: &mut Yamler) { fn yaml(&self, y: &mut Yamler) {
let Self { stmts } = self; let Self { stmts } = self;