Skip to content

Commit

Permalink
feat: derive PrintTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
ftupas committed Aug 7, 2023
1 parent d441aad commit 2321120
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/dojo-core/tests/src/executor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::result::ResultTrait;
use array::ArrayTrait;
use option::OptionTrait;
use traits::TryInto;
use debug::PrintTrait;

use starknet::syscalls::deploy_syscall;
use starknet::class_hash::Felt252TryIntoClassHash;
Expand Down
1 change: 1 addition & 0 deletions crates/dojo-core/tests/src/world.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use array::ArrayTrait;
use array::SpanTrait;
use debug::PrintTrait;
use clone::Clone;
use core::result::ResultTrait;
use traits::Into;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use traits::{Into, TryInto};
use option::OptionTrait;
use debug::PrintTrait;

// Cubit fixed point math library
use cubit::types::fixed::{Fixed, FixedInto, FixedTrait, ONE_u128};
Expand Down
1 change: 1 addition & 0 deletions crates/dojo-erc/src/erc20/components.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use starknet::ContractAddress;
use debug::PrintTrait;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
struct Allowance {
Expand Down
18 changes: 18 additions & 0 deletions crates/dojo-lang/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ pub fn handle_component_struct(
.collect(),
});

let member_prints: Vec<_> = members
.iter()
.map(|member| {
let member_name = &member.0;
format!("'{}'.print(); self.{}.print();", member_name, member_name)
})
.collect();

let print_body = member_prints.join("\n");

(
RewriteNode::interpolate_patched(
"
Expand Down Expand Up @@ -167,6 +177,13 @@ pub fn handle_component_struct(
}
}
#[cfg(test)]
impl $type_name$PrintImpl of PrintTrait<$type_name$> {
fn print(self: $type_name$) {
$print_body$
}
}
#[starknet::interface]
trait I$type_name$<T> {
fn name(self: @T) -> felt252;
Expand Down Expand Up @@ -219,6 +236,7 @@ pub fn handle_component_struct(
RewriteNode::new_modified(component_serialized_values),
),
("schema".to_string(), RewriteNode::new_modified(schema)),
("print_body".to_string(), RewriteNode::Text(print_body)),
]),
),
diagnostics,
Expand Down
37 changes: 36 additions & 1 deletion crates/dojo-lang/src/plugin_test_data/component
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test_expand_plugin

//! > cairo_code
use serde::Serde;
use debug::PrintTrait;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
struct Position {
Expand Down Expand Up @@ -60,6 +61,8 @@ struct Player {
//! > generated_cairo_code
use serde::Serde;

use debug::PrintTrait;

struct Position {
#[key]
id: felt252,
Expand Down Expand Up @@ -90,6 +93,18 @@ impl PositionComponent of dojo::traits::Component<Position> {
}
}

#[cfg(test)]
impl PositionPrintImpl of PrintTrait<Position> {
fn print(self: Position) {
'id'.print();
self.id.print();
'x'.print();
self.x.print();
'y'.print();
self.y.print();
}
}

#[starknet::interface]
trait IPosition<T> {
fn name(self: @T) -> felt252;
Expand Down Expand Up @@ -175,6 +190,14 @@ impl RolesComponent of dojo::traits::Component<Roles> {
}
}

#[cfg(test)]
impl RolesPrintImpl of PrintTrait<Roles> {
fn print(self: Roles) {
'role_ids'.print();
self.role_ids.print();
}
}

#[starknet::interface]
trait IRoles<T> {
fn name(self: @T) -> felt252;
Expand Down Expand Up @@ -249,6 +272,18 @@ impl PlayerComponent of dojo::traits::Component<Player> {
}
}

#[cfg(test)]
impl PlayerPrintImpl of PrintTrait<Player> {
fn print(self: Player) {
'game'.print();
self.game.print();
'player'.print();
self.player.print();
'name'.print();
self.name.print();
}
}

#[starknet::interface]
trait IPlayer<T> {
fn name(self: @T) -> felt252;
Expand Down Expand Up @@ -290,6 +325,6 @@ impl SerdeLenPlayer of dojo::SerdeLen<Player> {

//! > expected_diagnostics
error: Component must define atleast one #[key] attribute
--> dummy_file.cairo:31:8
--> dummy_file.cairo:32:8
struct Roles {
^***^
4 changes: 4 additions & 0 deletions crates/dojo-lang/src/plugin_test_data/inline_macros
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
test_expand_plugin

//! > cairo_code
use debug::PrintTrait;

struct Player {
#[key]
key: felt252,
Expand Down Expand Up @@ -31,6 +33,8 @@ fn foo() {
}

//! > generated_cairo_code
use debug::PrintTrait;

struct Player {
#[key]
key: felt252,
Expand Down
1 change: 1 addition & 0 deletions examples/ecs/src/components.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use array::ArrayTrait;
use debug::PrintTrait;
use starknet::ContractAddress;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
Expand Down
2 changes: 2 additions & 0 deletions examples/ecs/src/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod move {
use box::BoxTrait;
use traits::Into;
use dojo::world::Context;
use debug::PrintTrait;

use dojo_examples::components::Position;
use dojo_examples::components::Moves;
Expand All @@ -55,6 +56,7 @@ mod move {
fn execute(ctx: Context, direction: Direction) {
let (mut position, mut moves) = get !(ctx.world, ctx.origin, (Position, Moves));
moves.remaining -= 1;
moves.print();
let next = next_position(position, direction);
set !(ctx.world, (moves, next));
return ();
Expand Down

0 comments on commit 2321120

Please sign in to comment.