diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index a8f4433..770acd2 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -156,18 +156,6 @@ impl GetRust for Path { } } -impl GetRust for TypePostfix { - fn get_rust(&self) -> String { - match self { - TypePostfix::Ref => format!("&"), - TypePostfix::RefMut => format!("&mut "), - TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()), - TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()), - TypePostfix::Dyn => format!("dyn "), - } - } -} - impl GetRust for Visibility { fn get_rust(&self) -> String { match self { @@ -186,12 +174,47 @@ impl GetRust for Identifier { impl GetRust for TypeExpr { fn get_rust(&self) -> String { - self.1 - .iter() - .map(TypePostfix::get_rust) - .rev() - .collect::() - + &self.0.get_rust() + match self { + // TypePostfix::Ref => format!("&"), + // TypePostfix::RefMut => format!("&mut "), + // TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()), + // TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()), + // TypePostfix::Dyn => format!("dyn "), + Self::Path(path, generics) => { + if let Some(generics) = generics { + format!("{}{}", get_static_type_path(path), generics.get_rust()) + } else { + get_static_type_path(path) + } + } + Self::Lifetime(name) => format!("'{}", name.get_rust()), + Self::Tuple(types) => format!( + "({})", + types + .into_iter() + .map(|t| t.get_rust()) + .collect::>() + .join(", ") + ), + + Self::Ref { + lifetime, + mutable, + ty, + } => { + let mutable = if *mutable { "mut " } else { "" }; + + if let Some(lifetime) = lifetime { + format!("&{}{mutable}{}", lifetime.get_rust(), ty.get_rust()) + } else { + format!("&{mutable}{}", ty.get_rust()) + } + } + + Self::Dyn(ty) => { + format!("dyn {}", ty.get_rust()) + } + } } } @@ -205,29 +228,6 @@ pub fn get_static_type_path(path: &Path) -> String { } } -impl GetRust for TypeExprKind { - fn get_rust(&self) -> String { - match self { - TypeExprKind::Path(path, generics) => { - if let Some(generics) = generics { - format!("{}{}", get_static_type_path(path), generics.get_rust()) - } else { - get_static_type_path(path) - } - } - TypeExprKind::Lifetime(name) => format!("'{}", name.get_rust()), - TypeExprKind::Tuple(types) => format!( - "({})", - types - .into_iter() - .map(|t| t.get_rust()) - .collect::>() - .join(", ") - ), - } - } -} - impl GenRust for Pattern { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { match self { diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index bc4ef66..a9ef282 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -234,10 +234,14 @@ impl GenRust for (&Vec>, &Spanned) { let mut constructor_params = vec![VarDecl { name: Pattern::Path(false, Path(vec![Identifier(String::from("self"))])), - type_: Some(TypeExpr( - TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))]), None), - vec![TypePostfix::RefMut], - )), + type_: Some(TypeExpr::Ref { + lifetime: None, + mutable: true, + ty: Box::new(TypeExpr::Path( + Path(vec![Identifier(String::from("Self"))]), + None, + )), + }), }]; constructor_params.append(&mut self.1.item.params.0.clone()); @@ -250,7 +254,7 @@ impl GenRust for (&Vec>, &Spanned) { name: Identifier(String::from("constructor")), generics: self.1.item.generics.clone(), params: ParamList(constructor_params), - return_type: TypeExpr::no_px(TypeExprKind::Tuple(Vec::new())), + return_type: TypeExpr::Tuple(Vec::new()), body: Some(self.1.item.body.clone()), }, } @@ -423,10 +427,7 @@ impl GenRust for TopLevelKind { let mut impl_ = impl_.clone(); impl_.item.trait_ = Some(impl_.item.target); - impl_.item.target = TypeExpr( - TypeExprKind::Path(Path(vec![name.clone()]), None), - Vec::new(), - ); + impl_.item.target = TypeExpr::Path(Path(vec![name.clone()]), None); impl_.gen_rust(ctx, cg); }