Skip to content

Commit

Permalink
skeleton for deriving print for enum
Browse files Browse the repository at this point in the history
  • Loading branch information
0xicosahedron committed Oct 24, 2023
1 parent 61b4f89 commit 2416546
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn handle_introspect_struct(db: &dyn SyntaxGroup, struct_ast: ItemStruct) ->
/// A handler for Dojo code derives Introspect for an enum
/// Parameters:
/// * db: The semantic database.
/// * struct_ast: The AST of the struct.
/// * enum_ast: The AST of the enum.
/// Returns:
/// * A RewriteNode containing the generated code.
pub fn handle_introspect_enum(
Expand Down
11 changes: 8 additions & 3 deletions crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::inline_macros::get::GetMacro;
use crate::inline_macros::set::SetMacro;
use crate::introspect::{handle_introspect_enum, handle_introspect_struct};
use crate::model::handle_model_struct;
use crate::print::derive_print;
use crate::print::handle_print_struct;

const DOJO_CONTRACT_ATTR: &str = "dojo::contract";

Expand Down Expand Up @@ -56,7 +56,11 @@ impl GeneratedFileAuxData for DojoAuxData {
self
}
fn eq(&self, other: &dyn GeneratedFileAuxData) -> bool {
if let Some(other) = other.as_any().downcast_ref::<Self>() { self == other } else { false }
if let Some(other) = other.as_any().downcast_ref::<Self>() {
self == other
} else {
false
}
}
}

Expand Down Expand Up @@ -163,6 +167,7 @@ impl MacroPlugin for BuiltinDojoPlugin {
enum_ast.clone(),
));
}
"Print" => rewrite_nodes.push(handle_print_enum(db, enum_ast.clone())),
_ => continue,
}
}
Expand Down Expand Up @@ -239,7 +244,7 @@ impl MacroPlugin for BuiltinDojoPlugin {
diagnostics.extend(model_diagnostics);
}
"Print" => {
rewrite_nodes.push(derive_print(db, struct_ast.clone()));
rewrite_nodes.push(handle_print_struct(db, struct_ast.clone()));
}
"Introspect" => {
rewrite_nodes
Expand Down
41 changes: 39 additions & 2 deletions crates/dojo-lang/src/print.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cairo_lang_defs::patcher::RewriteNode;
use cairo_lang_syntax::node::ast::ItemStruct;
use cairo_lang_syntax::node::ast::{ItemEnum, ItemStruct};
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode};
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
Expand All @@ -10,7 +10,44 @@ use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
/// * struct_ast: The AST of the model struct.
/// Returns:
/// * A RewriteNode containing the generated code.
pub fn derive_print(db: &dyn SyntaxGroup, struct_ast: ItemStruct) -> RewriteNode {
pub fn handle_print_struct(db: &dyn SyntaxGroup, struct_ast: ItemStruct) -> RewriteNode {
let prints: Vec<_> = struct_ast
.members(db)
.elements(db)
.iter()
.map(|m| {
format!(
"debug::PrintTrait::print('{}'); debug::PrintTrait::print(self.{});",
m.name(db).text(db).to_string(),
m.name(db).text(db).to_string()
)
})
.collect();

RewriteNode::interpolate_patched(
"#[cfg(test)]
impl $type_name$PrintImpl of debug::PrintTrait<$type_name$> {
fn print(self: $type_name$) {
$print$
}
}",
UnorderedHashMap::from([
(
"type_name".to_string(),
RewriteNode::new_trimmed(struct_ast.name(db).as_syntax_node()),
),
("print".to_string(), RewriteNode::Text(prints.join("\n"))),
]),
)
}

/// Derives PrintTrait for an enum.
/// Parameters:
/// * db: The semantic database.
/// * enum_ast: The AST of the model enum.
/// Returns:
/// * A RewriteNode containing the generated code.
pub fn handle_print_enum(db: &dyn SyntaxGroup, struct_ast: ItemEnum) -> RewriteNode {
let prints: Vec<_> = struct_ast
.members(db)
.elements(db)
Expand Down

0 comments on commit 2416546

Please sign in to comment.