This commit is contained in:
2026-05-10 18:08:15 +02:00
parent e063d4c685
commit 380755bf83
4 changed files with 44 additions and 27 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
use <std::fmt>; use <std::fmt>;
public trait TestLogging: Display {} public trait TestLogging<T: fmt::Display>: fmt::Display {}
public enum LogLevel { public enum LogLevel {
Info, Info,
+2 -8
View File
@@ -80,7 +80,7 @@ pub enum TopLevelKind {
name: Identifier, name: Identifier,
generics: Generics, generics: Generics,
requirements: Vec<TypeExpr>, requirements: Vec<TypeExpr>,
items: Vec<TraitItem>, items: Vec<FunctionDecl>,
}, },
EnumDecl { EnumDecl {
visibility: Visibility, visibility: Visibility,
@@ -105,12 +105,6 @@ pub enum TopLevelKind {
}, },
} }
#[derive(Debug, Clone, Serialize)]
pub enum TraitItem {
WithBody(FunctionDecl),
NoBody(FunctionDecl),
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum ClassItem { pub enum ClassItem {
Method(FunctionDecl), Method(FunctionDecl),
@@ -158,7 +152,7 @@ pub struct FunctionDecl {
pub generics: Generics, pub generics: Generics,
pub params: ParamList, pub params: ParamList,
pub return_type: TypeExpr, pub return_type: TypeExpr,
pub body: Block, pub body: Option<Block>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+2 -15
View File
@@ -314,7 +314,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
requirements: consume_rule(&mut inner, Rule::trait_requirements) requirements: consume_rule(&mut inner, Rule::trait_requirements)
.map(|pair| pair.into_inner().map(TypeExpr::from).collect()) .map(|pair| pair.into_inner().map(TypeExpr::from).collect())
.unwrap_or_default(), .unwrap_or_default(),
items: inner.map(TraitItem::from).collect(), items: inner.map(FunctionDecl::from).collect(),
}, },
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
@@ -322,19 +322,6 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for TraitItem {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule();
match rule {
Rule::method => TraitItem::WithBody(FunctionDecl::from(pair)),
Rule::method_no_body => TraitItem::NoBody(FunctionDecl::from(pair)),
_ => unimplemented!("{rule:#?}"),
}
}
}
impl From<pest::iterators::Pair<'_, Rule>> for ClassItem { impl From<pest::iterators::Pair<'_, Rule>> for ClassItem {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule(); let rule = pair.as_rule();
@@ -789,7 +776,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
}) })
.unwrap_or_else(|| ParamList(self_param.into_iter().collect())); .unwrap_or_else(|| ParamList(self_param.into_iter().collect()));
let body = Block::from(inner.next().unwrap()); let body = inner.next().map(Block::from);
Self { Self {
visibility, visibility,
+37 -1
View File
@@ -358,6 +358,38 @@ impl ToRust for TopLevelKind {
cg.indent -= 1; cg.indent -= 1;
cg.addln("}\n"); cg.addln("}\n");
} }
Self::TraitDecl {
visibility,
name,
generics,
requirements,
items,
} => {
cg.addln(&format!(
"{}trait {}{}{} {{",
visibility.get_rust(),
name.get_rust(),
generics.get_rust(),
if requirements.len() != 0 {
String::from(": ")
+ &requirements
.iter()
.map(TypeExpr::get_rust)
.collect::<Vec<_>>()
.join("+")
} else {
String::new()
},
));
cg.indent += 1;
for item in items {
item.to_rust(cg);
}
cg.indent -= 1;
cg.addln("}\n");
}
Self::ClassDecl { Self::ClassDecl {
visibility, visibility,
name, name,
@@ -627,9 +659,13 @@ impl ToRust for FunctionDecl {
params_str, params_str,
self.return_type.get_rust() self.return_type.get_rust()
)); ));
if let Some(body) = &self.body {
cg.indent += 1; cg.indent += 1;
self.body.to_rust(cg); body.to_rust(cg);
cg.indent -= 1; cg.indent -= 1;
} else {
cg.add(";");
}
cg.add_indentedln("}\n"); cg.add_indentedln("}\n");
} }
} }