Refactored fields
This commit is contained in:
+2
-5
@@ -3,9 +3,6 @@ use serde::Serialize;
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Identifier(pub String);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Default)]
|
||||
pub struct FieldList(pub Vec<(Identifier, Visibility, TypeExpr)>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Default)]
|
||||
pub struct ParamList(pub Vec<VarDecl>);
|
||||
|
||||
@@ -87,7 +84,7 @@ pub enum TopLevelKind {
|
||||
visibility: Visibility,
|
||||
generics: Generics,
|
||||
name: Identifier,
|
||||
fields: FieldList,
|
||||
fields: Vec<FieldDecl>,
|
||||
},
|
||||
FunctionDecl(FunctionDecl),
|
||||
ClassDecl {
|
||||
@@ -123,7 +120,7 @@ pub enum Pattern {
|
||||
pub enum EnumItem {
|
||||
Named(Identifier),
|
||||
Tuple(Identifier, Vec<TypeExpr>),
|
||||
Struct(Identifier, FieldList),
|
||||
Struct(Identifier, Vec<FieldDecl>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
||||
+8
-19
@@ -107,23 +107,6 @@ impl From<pest::iterators::Pair<'_, Rule>> for Path {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for FieldList {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
let params = pair
|
||||
.into_inner()
|
||||
.map(|p| {
|
||||
let mut param_inner = p.into_inner();
|
||||
let visibility = Visibility::from(&mut param_inner);
|
||||
let param_type = TypeExpr::from(param_inner.next().unwrap());
|
||||
let param_name = Identifier::from(param_inner.next().unwrap());
|
||||
(param_name, visibility, param_type)
|
||||
})
|
||||
.collect();
|
||||
|
||||
FieldList(params)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for ParamList {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
ParamList(pair.into_inner().map(VarDecl::from).collect())
|
||||
@@ -287,7 +270,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
generics: consume_rule(&mut inner, Rule::generics)
|
||||
.map(Generics::from)
|
||||
.unwrap_or_default(),
|
||||
fields: inner.next().map(FieldList::from).unwrap_or_default(),
|
||||
fields: inner
|
||||
.next()
|
||||
.map(|pair| pair.into_inner().map(FieldDecl::from).collect())
|
||||
.unwrap_or_default(),
|
||||
},
|
||||
|
||||
Rule::class_decl => TopLevelKind::ClassDecl {
|
||||
@@ -342,7 +328,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for EnumItem {
|
||||
|
||||
Rule::enum_struct => EnumItem::Struct(
|
||||
Identifier::from(inner.next().unwrap()),
|
||||
FieldList::from(inner.next().unwrap()),
|
||||
inner
|
||||
.next()
|
||||
.map(|pair| pair.into_inner().map(FieldDecl::from).collect())
|
||||
.unwrap_or_default(),
|
||||
),
|
||||
|
||||
_ => unimplemented!("{rule:#?}"),
|
||||
|
||||
+20
-10
@@ -1,7 +1,8 @@
|
||||
use parser::ast::{
|
||||
Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Generic, Generics, Identifier,
|
||||
Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind,
|
||||
TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, Visibility,
|
||||
Attribute, BinaryOp, Block, EnumItem, Expression, FieldDecl, FunctionDecl, Generic, Generics,
|
||||
Identifier, Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel,
|
||||
TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt,
|
||||
Visibility,
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -328,9 +329,8 @@ impl ToRust for TopLevelKind {
|
||||
));
|
||||
cg.indent += 1;
|
||||
|
||||
for (field_name, _, ty) in &fields.0 {
|
||||
let ty = ty.get_rust();
|
||||
cg.add_indentedln(&format!("pub {}: {},", field_name.get_rust(), ty));
|
||||
for field in fields {
|
||||
cg.add_indentedln(&field.get_rust());
|
||||
}
|
||||
|
||||
cg.indent -= 1;
|
||||
@@ -375,8 +375,7 @@ impl ToRust for TopLevelKind {
|
||||
cg.indent += 1;
|
||||
|
||||
for field in fields {
|
||||
let ty = field.decl.type_.clone().get_rust();
|
||||
cg.add_indentedln(&format!("pub {}: {},", field.decl.name.get_rust(), ty));
|
||||
cg.add_indentedln(&field.decl.get_rust());
|
||||
}
|
||||
|
||||
cg.indent -= 1;
|
||||
@@ -677,8 +676,8 @@ impl GetRust for EnumItem {
|
||||
Self::Struct(id, s) => format!(
|
||||
"{} {{{}}}",
|
||||
id.get_rust(),
|
||||
s.0.iter()
|
||||
.map(|(id, _, ty)| format!("{}: {}", id.get_rust(), ty.get_rust()))
|
||||
s.iter()
|
||||
.map(|field| format!("{}: {}", field.name.get_rust(), field.type_.get_rust()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
),
|
||||
@@ -769,6 +768,17 @@ impl GetRust for (bool, &Generic) {
|
||||
}
|
||||
}
|
||||
|
||||
impl GetRust for FieldDecl {
|
||||
fn get_rust(&self) -> String {
|
||||
format!(
|
||||
"{}{}: {},",
|
||||
self.visibility.get_rust(),
|
||||
self.name.get_rust(),
|
||||
self.type_.get_rust()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_static_type_path(path: &Path) -> String {
|
||||
let rust_path = path.get_rust();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user