Skip to content

Commit

Permalink
refactor: use serde impl rather than component class impl (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored Apr 6, 2023
1 parent 184eb7f commit c92c44d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 213 deletions.
12 changes: 7 additions & 5 deletions crates/dojo-lang/src/commands/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
Expand Down
15 changes: 0 additions & 15 deletions crates/dojo-lang/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<felt252>) -> $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<felt252> {
let mut arr = ArrayTrait::new();
serde::Serde::<$type_name$>::serialize(ref arr, value);
arr.span()
}
",
HashMap::from([
(
Expand Down
15 changes: 0 additions & 15 deletions crates/dojo-lang/src/plugin_test_data/component
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,6 @@ mod PositionComponent {
fn len() -> usize {
2_usize
}

// Serialize an entity.
#[view]
fn serialize(mut raw: Span<felt252>) -> Position {
serde::Serde::<Position>::deserialize(ref raw).unwrap()
}

// Get the state of an entity.
#[view]
#[raw_output]
fn deserialize(value: Position) -> Span<felt252> {
let mut arr = ArrayTrait::new();
serde::Serde::<Position>::serialize(ref arr, value);
arr.span()
}
}

//! > expected_diagnostics
15 changes: 0 additions & 15 deletions crates/dojo-lang/src/plugin_test_data/component_typed
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,6 @@ mod FighterComponent {
fn len() -> usize {
5_usize
}

// Serialize an entity.
#[view]
fn serialize(mut raw: Span<felt252>) -> Fighter {
serde::Serde::<Fighter>::deserialize(ref raw).unwrap()
}

// Get the state of an entity.
#[view]
#[raw_output]
fn deserialize(value: Fighter) -> Span<felt252> {
let mut arr = ArrayTrait::new();
serde::Serde::<Fighter>::serialize(ref arr, value);
arr.span()
}
}

//! > expected_diagnostics
232 changes: 77 additions & 155 deletions crates/dojo-lang/src/plugin_test_data/system
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -188,21 +184,6 @@ mod PositionComponent {
fn len() -> usize {
2_usize
}

// Serialize an entity.
#[view]
fn serialize(mut raw: Span<felt252>) -> Position {
serde::Serde::<Position>::deserialize(ref raw).unwrap()
}

// Get the state of an entity.
#[view]
#[raw_output]
fn deserialize(value: Position) -> Span<felt252> {
let mut arr = ArrayTrait::new();
serde::Serde::<Position>::serialize(ref arr, value);
arr.span()
}
}

#[derive(Copy, Drop)]
Expand Down Expand Up @@ -243,21 +224,6 @@ mod PlayerComponent {
fn len() -> usize {
1_usize
}

// Serialize an entity.
#[view]
fn serialize(mut raw: Span<felt252>) -> Player {
serde::Serde::<Player>::deserialize(ref raw).unwrap()
}

// Get the state of an entity.
#[view]
#[raw_output]
fn deserialize(value: Player) -> Span<felt252> {
let mut arr = ArrayTrait::new();
serde::Serde::<Player>::serialize(ref arr, value);
arr.span()
}
}

#[contract]
Expand Down Expand Up @@ -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::<Player>::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::<Position>::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::<Player>::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::<Position>::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::<Player>::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::<Position>::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::<Player>::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::<Position>::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::<Player>::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::<Position>::serialize(ref calldata, Position { x: 0, y: 0 });
IWorldDispatcher {
contract_address: world_address
}.set('Position', 1337.into(), 0_u8, calldata.span());
}
return ();
}
}
Expand Down Expand Up @@ -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());
^*************^
8 changes: 0 additions & 8 deletions examples/src/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit c92c44d

Please sign in to comment.