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