traits
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use <std::fmt>;
|
||||
|
||||
public trait TestLogging: Display {}
|
||||
public trait TestLogging<T: fmt::Display>: fmt::Display {}
|
||||
|
||||
public enum LogLevel {
|
||||
Info,
|
||||
|
||||
+2
-8
@@ -80,7 +80,7 @@ pub enum TopLevelKind {
|
||||
name: Identifier,
|
||||
generics: Generics,
|
||||
requirements: Vec<TypeExpr>,
|
||||
items: Vec<TraitItem>,
|
||||
items: Vec<FunctionDecl>,
|
||||
},
|
||||
EnumDecl {
|
||||
visibility: Visibility,
|
||||
@@ -105,12 +105,6 @@ pub enum TopLevelKind {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum TraitItem {
|
||||
WithBody(FunctionDecl),
|
||||
NoBody(FunctionDecl),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum ClassItem {
|
||||
Method(FunctionDecl),
|
||||
@@ -158,7 +152,7 @@ pub struct FunctionDecl {
|
||||
pub generics: Generics,
|
||||
pub params: ParamList,
|
||||
pub return_type: TypeExpr,
|
||||
pub body: Block,
|
||||
pub body: Option<Block>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
||||
+2
-15
@@ -314,7 +314,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
requirements: consume_rule(&mut inner, Rule::trait_requirements)
|
||||
.map(|pair| pair.into_inner().map(TypeExpr::from).collect())
|
||||
.unwrap_or_default(),
|
||||
items: inner.map(TraitItem::from).collect(),
|
||||
items: inner.map(FunctionDecl::from).collect(),
|
||||
},
|
||||
|
||||
_ => 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 {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
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()));
|
||||
|
||||
let body = Block::from(inner.next().unwrap());
|
||||
let body = inner.next().map(Block::from);
|
||||
|
||||
Self {
|
||||
visibility,
|
||||
|
||||
+37
-1
@@ -358,6 +358,38 @@ impl ToRust for TopLevelKind {
|
||||
cg.indent -= 1;
|
||||
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 {
|
||||
visibility,
|
||||
name,
|
||||
@@ -627,9 +659,13 @@ impl ToRust for FunctionDecl {
|
||||
params_str,
|
||||
self.return_type.get_rust()
|
||||
));
|
||||
if let Some(body) = &self.body {
|
||||
cg.indent += 1;
|
||||
self.body.to_rust(cg);
|
||||
body.to_rust(cg);
|
||||
cg.indent -= 1;
|
||||
} else {
|
||||
cg.add(";");
|
||||
}
|
||||
cg.add_indentedln("}\n");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user