Class impl
This commit is contained in:
@@ -58,4 +58,10 @@ public class Logger {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display {
|
||||
std::fmt::Result fmt(self*, std::fmt::Formatter<'_> mut* f) {
|
||||
return write!(f, "logger ({})", self.prefix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -94,10 +94,16 @@ pub enum TopLevelKind {
|
||||
generics: Generics,
|
||||
fields: Vec<FieldDeclStmt>,
|
||||
constructor: ClassConstructor,
|
||||
items: Vec<FunctionDecl>,
|
||||
items: Vec<ClassItem>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum ClassItem {
|
||||
Method(FunctionDecl),
|
||||
ImplDecl(ImplDecl),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Default)]
|
||||
pub struct Generics(pub Vec<Generic>);
|
||||
|
||||
|
||||
+15
-1
@@ -289,7 +289,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
.map(FieldDeclStmt::from)
|
||||
.collect(),
|
||||
constructor: ClassConstructor::from(inner.next().unwrap()),
|
||||
items: inner.into_iter().map(FunctionDecl::from).collect(),
|
||||
items: inner.into_iter().map(ClassItem::from).collect(),
|
||||
},
|
||||
|
||||
Rule::enum_decl => TopLevelKind::EnumDecl {
|
||||
@@ -310,6 +310,20 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for ClassItem {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let rule = pair.as_rule();
|
||||
|
||||
match rule {
|
||||
Rule::impl_decl | Rule::impl_for_decl => ClassItem::ImplDecl(ImplDecl::from(pair)),
|
||||
|
||||
Rule::method => ClassItem::Method(FunctionDecl::from(pair)),
|
||||
|
||||
_ => unimplemented!("{rule:#?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for ImplDecl {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let rule = pair.as_rule();
|
||||
|
||||
+25
-7
@@ -1,8 +1,8 @@
|
||||
use parser::ast::{
|
||||
Attribute, BinaryOp, Block, EnumItem, Expression, FieldDecl, FunctionDecl, Generic, Generics,
|
||||
Identifier, ImplDecl, Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch,
|
||||
TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl,
|
||||
VarDeclStmt, Visibility,
|
||||
Attribute, BinaryOp, Block, ClassItem, EnumItem, Expression, FieldDecl, FunctionDecl, Generic,
|
||||
Generics, Identifier, ImplDecl, Literal, Path, Pattern, Postfix, Prefix, Statement,
|
||||
StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt,
|
||||
VarDecl, VarDeclStmt, Visibility,
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -364,7 +364,7 @@ impl ToRust for TopLevelKind {
|
||||
generics,
|
||||
fields,
|
||||
constructor,
|
||||
items: methods,
|
||||
items,
|
||||
} => {
|
||||
// Struct decl
|
||||
cg.addln(&format!(
|
||||
@@ -458,12 +458,30 @@ impl ToRust for TopLevelKind {
|
||||
cg.indent -= 1;
|
||||
cg.add_indentedln("}\n");
|
||||
|
||||
for method in methods {
|
||||
method.to_rust(cg);
|
||||
for item in items {
|
||||
match item {
|
||||
ClassItem::ImplDecl(_) => {}
|
||||
ClassItem::Method(method) => method.to_rust(cg),
|
||||
}
|
||||
}
|
||||
|
||||
cg.indent -= 1;
|
||||
cg.addln("}\n");
|
||||
|
||||
for item in items {
|
||||
match item {
|
||||
ClassItem::ImplDecl(impl_) => {
|
||||
let mut impl_ = impl_.clone();
|
||||
|
||||
impl_.trait_ = Some(impl_.target);
|
||||
impl_.target =
|
||||
TypeExpr(TypeExprKind::Path(Path(vec![name.clone()])), Vec::new());
|
||||
|
||||
impl_.to_rust(cg);
|
||||
}
|
||||
ClassItem::Method(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user