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 {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!("impl Display for Impl")
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
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())
}
}
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),*$(,)?})*) {$($(
impl From<$from> for $T {

View File

@ -205,14 +205,14 @@ pub enum VariantKind {
/// Sub-[items](Item) (associated functions, etc.) for a [Ty]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Impl {
pub target: Ty,
pub body: Vec<Item>,
pub target: ImplKind,
pub body: File,
}
// TODO: `impl` Trait for <Target> { }
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ImplKind {
Type(Box<Ty>),
Type(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);
}
}
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 {
fn yaml(&self, y: &mut Yamler) {
let Self { stmts } = self;