From c92c44de3862ba85b1476aa5c27b26b3ebf78441 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Thu, 6 Apr 2023 13:52:58 -0500 Subject: [PATCH] refactor: use serde impl rather than component class impl (#190) --- crates/dojo-lang/src/commands/set.rs | 12 +- crates/dojo-lang/src/component.rs | 15 -- .../dojo-lang/src/plugin_test_data/component | 15 -- .../src/plugin_test_data/component_typed | 15 -- crates/dojo-lang/src/plugin_test_data/system | 232 ++++++------------ examples/src/systems.cairo | 8 - 6 files changed, 84 insertions(+), 213 deletions(-) diff --git a/crates/dojo-lang/src/commands/set.rs b/crates/dojo-lang/src/commands/set.rs index 2f6f029f19..ca159b7c97 100644 --- a/crates/dojo-lang/src/commands/set.rs +++ b/crates/dojo-lang/src/commands/set.rs @@ -21,11 +21,13 @@ impl SetCommand { self.components.push(component.clone()); self.data.rewrite_nodes.push(RewriteNode::interpolate_patched( - " - IWorldDispatcher { contract_address: world_address }.set('$component$', \ - $storage_key$, 0_u8, I$component$LibraryDispatcher { class_hash: \ - IWorldDispatcher { contract_address: world_address \ - }.component('$component$') }.deserialize($ctor$)); + " + { + let mut calldata = ArrayTrait::new(); + serde::Serde::<$component$>::serialize(ref calldata, $ctor$); + IWorldDispatcher { contract_address: world_address }.set('$component$', \ + $storage_key$, 0_u8, calldata.span()); + } ", HashMap::from([ ("component".to_string(), RewriteNode::Text(component.to_string())), diff --git a/crates/dojo-lang/src/component.rs b/crates/dojo-lang/src/component.rs index f1f05f5a6b..267f9b4d60 100644 --- a/crates/dojo-lang/src/component.rs +++ b/crates/dojo-lang/src/component.rs @@ -23,21 +23,6 @@ pub fn handle_component_struct(db: &dyn SyntaxGroup, struct_ast: ast::ItemStruct fn len() -> usize { $len$_usize } - - // Serialize an entity. - #[view] - fn serialize(mut raw: Span) -> $type_name$ { - serde::Serde::<$type_name$>::deserialize(ref raw).unwrap() - } - - // Get the state of an entity. - #[view] - #[raw_output] - fn deserialize(value: $type_name$) -> Span { - let mut arr = ArrayTrait::new(); - serde::Serde::<$type_name$>::serialize(ref arr, value); - arr.span() - } ", HashMap::from([ ( diff --git a/crates/dojo-lang/src/plugin_test_data/component b/crates/dojo-lang/src/plugin_test_data/component index 3f8d978351..8ef24b9507 100644 --- a/crates/dojo-lang/src/plugin_test_data/component +++ b/crates/dojo-lang/src/plugin_test_data/component @@ -96,21 +96,6 @@ mod PositionComponent { fn len() -> usize { 2_usize } - - // Serialize an entity. - #[view] - fn serialize(mut raw: Span) -> Position { - serde::Serde::::deserialize(ref raw).unwrap() - } - - // Get the state of an entity. - #[view] - #[raw_output] - fn deserialize(value: Position) -> Span { - let mut arr = ArrayTrait::new(); - serde::Serde::::serialize(ref arr, value); - arr.span() - } } //! > expected_diagnostics diff --git a/crates/dojo-lang/src/plugin_test_data/component_typed b/crates/dojo-lang/src/plugin_test_data/component_typed index 13d0318d18..633f3aef56 100644 --- a/crates/dojo-lang/src/plugin_test_data/component_typed +++ b/crates/dojo-lang/src/plugin_test_data/component_typed @@ -68,21 +68,6 @@ mod FighterComponent { fn len() -> usize { 5_usize } - - // Serialize an entity. - #[view] - fn serialize(mut raw: Span) -> Fighter { - serde::Serde::::deserialize(ref raw).unwrap() - } - - // Get the state of an entity. - #[view] - #[raw_output] - fn deserialize(value: Fighter) -> Span { - let mut arr = ArrayTrait::new(); - serde::Serde::::serialize(ref arr, value); - arr.span() - } } //! > expected_diagnostics diff --git a/crates/dojo-lang/src/plugin_test_data/system b/crates/dojo-lang/src/plugin_test_data/system index 206ee67cc6..4673a0cad7 100644 --- a/crates/dojo-lang/src/plugin_test_data/system +++ b/crates/dojo-lang/src/plugin_test_data/system @@ -25,11 +25,7 @@ mod Spawn { use starknet::contract_address::ContractAddressIntoFelt252; use super::Position; - use super::IPositionLibraryDispatcher; - use super::IPositionDispatcherTrait; use super::Player; - use super::IPlayerLibraryDispatcher; - use super::IPlayerDispatcherTrait; fn execute(name: felt252) { let uuid = commands::uuid(); @@ -188,21 +184,6 @@ mod PositionComponent { fn len() -> usize { 2_usize } - - // Serialize an entity. - #[view] - fn serialize(mut raw: Span) -> Position { - serde::Serde::::deserialize(ref raw).unwrap() - } - - // Get the state of an entity. - #[view] - #[raw_output] - fn deserialize(value: Position) -> Span { - let mut arr = ArrayTrait::new(); - serde::Serde::::serialize(ref arr, value); - arr.span() - } } #[derive(Copy, Drop)] @@ -243,21 +224,6 @@ mod PlayerComponent { fn len() -> usize { 1_usize } - - // Serialize an entity. - #[view] - fn serialize(mut raw: Span) -> Player { - serde::Serde::::deserialize(ref raw).unwrap() - } - - // Get the state of an entity. - #[view] - #[raw_output] - fn deserialize(value: Player) -> Span { - let mut arr = ArrayTrait::new(); - serde::Serde::::serialize(ref arr, value); - arr.span() - } } #[contract] @@ -285,137 +251,93 @@ mod SpawnSystem { use starknet::contract_address::ContractAddressIntoFelt252; use super::Position; - use super::IPositionLibraryDispatcher; - use super::IPositionDispatcherTrait; use super::Player; - use super::IPlayerLibraryDispatcher; - use super::IPlayerDispatcherTrait; #[external] fn execute(name: felt252, world_address: starknet::ContractAddress) { let uuid = IWorldDispatcher { contract_address: world_address }.uuid(); - IWorldDispatcher { - contract_address: world_address - }.set( - 'Player', - (420, (69, uuid)).into(), - 0_u8, - IPlayerLibraryDispatcher { - class_hash: IWorldDispatcher { contract_address: world_address }.component('Player') - }.deserialize(Player { name: name }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Player { name: name }); + IWorldDispatcher { + contract_address: world_address + }.set('Player', (420, (69, uuid)).into(), 0_u8, calldata.span()); + } - IWorldDispatcher { - contract_address: world_address - }.set( - 'Position', - (420, (69, uuid)).into(), - 0_u8, - IPositionLibraryDispatcher { - class_hash: IWorldDispatcher { - contract_address: world_address - }.component('Position') - }.deserialize(Position { x: 0, y: 0 }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Position { x: 0, y: 0 }); + IWorldDispatcher { + contract_address: world_address + }.set('Position', (420, (69, uuid)).into(), 0_u8, calldata.span()); + } - IWorldDispatcher { - contract_address: world_address - }.set( - 'Player', - 420.into(), - 0_u8, - IPlayerLibraryDispatcher { - class_hash: IWorldDispatcher { contract_address: world_address }.component('Player') - }.deserialize(Player { name: name }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Player { name: name }); + IWorldDispatcher { + contract_address: world_address + }.set('Player', 420.into(), 0_u8, calldata.span()); + } - IWorldDispatcher { - contract_address: world_address - }.set( - 'Position', - 420.into(), - 0_u8, - IPositionLibraryDispatcher { - class_hash: IWorldDispatcher { - contract_address: world_address - }.component('Position') - }.deserialize(Position { x: 0, y: 0 }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Position { x: 0, y: 0 }); + IWorldDispatcher { + contract_address: world_address + }.set('Position', 420.into(), 0_u8, calldata.span()); + } let player_id = starknet::get_caller_address(); - IWorldDispatcher { - contract_address: world_address - }.set( - 'Player', - player_id.into(), - 0_u8, - IPlayerLibraryDispatcher { - class_hash: IWorldDispatcher { contract_address: world_address }.component('Player') - }.deserialize(Player { name: name }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Player { name: name }); + IWorldDispatcher { + contract_address: world_address + }.set('Player', player_id.into(), 0_u8, calldata.span()); + } - IWorldDispatcher { - contract_address: world_address - }.set( - 'Position', - player_id.into(), - 0_u8, - IPositionLibraryDispatcher { - class_hash: IWorldDispatcher { - contract_address: world_address - }.component('Position') - }.deserialize(Position { x: 0, y: 0 }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Position { x: 0, y: 0 }); + IWorldDispatcher { + contract_address: world_address + }.set('Position', player_id.into(), 0_u8, calldata.span()); + } - IWorldDispatcher { - contract_address: world_address - }.set( - 'Player', - (0, 0, 0, 0, 0), - 0_u8, - IPlayerLibraryDispatcher { - class_hash: IWorldDispatcher { contract_address: world_address }.component('Player') - }.deserialize(Player { name: name }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Player { name: name }); + IWorldDispatcher { + contract_address: world_address + }.set('Player', (0, 0, 0, 0, 0), 0_u8, calldata.span()); + } - IWorldDispatcher { - contract_address: world_address - }.set( - 'Position', - (0, 0, 0, 0, 0), - 0_u8, - IPositionLibraryDispatcher { - class_hash: IWorldDispatcher { - contract_address: world_address - }.component('Position') - }.deserialize(Position { x: 0, y: 0 }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Position { x: 0, y: 0 }); + IWorldDispatcher { + contract_address: world_address + }.set('Position', (0, 0, 0, 0, 0), 0_u8, calldata.span()); + } - IWorldDispatcher { - contract_address: world_address - }.set( - 'Player', - 1337.into(), - 0_u8, - IPlayerLibraryDispatcher { - class_hash: IWorldDispatcher { contract_address: world_address }.component('Player') - }.deserialize(Player { name: name }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Player { name: name }); + IWorldDispatcher { + contract_address: world_address + }.set('Player', 1337.into(), 0_u8, calldata.span()); + } - IWorldDispatcher { - contract_address: world_address - }.set( - 'Position', - 1337.into(), - 0_u8, - IPositionLibraryDispatcher { - class_hash: IWorldDispatcher { - contract_address: world_address - }.component('Position') - }.deserialize(Position { x: 0, y: 0 }) - ); + { + let mut calldata = ArrayTrait::new(); + serde::Serde::::serialize(ref calldata, Position { x: 0, y: 0 }); + IWorldDispatcher { + contract_address: world_address + }.set('Position', 1337.into(), 0_u8, calldata.span()); + } return (); } } @@ -633,16 +555,16 @@ mod MoveSystem { //! > expected_diagnostics error: Plugin diagnostic: Invalid arguments. Expected "(storage_key, (components,))" - --> lib.cairo:46:29 + --> lib.cairo:42:29 commands::set_entity(( ^^ error: Plugin diagnostic: Unexpected argument type. Expected: "dojo_core::storage::key::StorageKey", found: "(core::felt252, core::felt252, core::felt252, core::felt252, core::felt252)". - --> Spawn:51:88 - IWorldDispatcher { contract_address: world_address }.set('Player', (0, 0, 0, 0, 0), 0_u8, IPlayerLibraryDispatcher { class_hash: IWorldDispatcher { contract_address: world_address }.component('Player') }.deserialize(Player { name: name })); - ^*************^ + --> Spawn:74:92 + IWorldDispatcher { contract_address: world_address }.set('Player', (0, 0, 0, 0, 0), 0_u8, calldata.span()); + ^*************^ error: Plugin diagnostic: Unexpected argument type. Expected: "dojo_core::storage::key::StorageKey", found: "(core::felt252, core::felt252, core::felt252, core::felt252, core::felt252)". - --> Spawn:53:90 - IWorldDispatcher { contract_address: world_address }.set('Position', (0, 0, 0, 0, 0), 0_u8, IPositionLibraryDispatcher { class_hash: IWorldDispatcher { contract_address: world_address }.component('Position') }.deserialize(Position { x: 0, y: 0 })); - ^*************^ + --> Spawn:80:94 + IWorldDispatcher { contract_address: world_address }.set('Position', (0, 0, 0, 0, 0), 0_u8, calldata.span()); + ^*************^ diff --git a/examples/src/systems.cairo b/examples/src/systems.cairo index 435f346c18..37ecb9abeb 100644 --- a/examples/src/systems.cairo +++ b/examples/src/systems.cairo @@ -5,11 +5,7 @@ mod Spawn { use starknet::contract_address::ContractAddressIntoFelt252; use dojo_examples::components::Position; - use dojo_examples::components::IPositionLibraryDispatcher; - use dojo_examples::components::IPositionDispatcherTrait; use dojo_examples::components::Moves; - use dojo_examples::components::IMovesLibraryDispatcher; - use dojo_examples::components::IMovesDispatcherTrait; fn execute() { let caller = starknet::get_caller_address(); @@ -29,11 +25,7 @@ mod Move { use starknet::contract_address::ContractAddressIntoFelt252; use dojo_examples::components::Position; - use dojo_examples::components::IPositionLibraryDispatcher; - use dojo_examples::components::IPositionDispatcherTrait; use dojo_examples::components::Moves; - use dojo_examples::components::IMovesLibraryDispatcher; - use dojo_examples::components::IMovesDispatcherTrait; // TODO: Use enum once serde is derivable // left: 0, right: 1, up: 2, down: 3