From 62c8efeceb3af7e7445726ad860c66e3d7b4b199 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Wed, 27 Sep 2023 09:32:00 -0400 Subject: [PATCH] Rename some usages of component to model --- crates/dojo-core/src/executor_test.cairo | 2 +- crates/dojo-core/src/lib.cairo | 2 +- .../src/{component.cairo => model.cairo} | 9 ++------- crates/dojo-core/src/world_test.cairo | 4 ++-- crates/dojo-defi/src/market/components.cairo | 10 +++++----- .../dojo-erc/src/token/erc20_components.cairo | 6 +++--- .../src/token/erc721/components.cairo | 10 +++++----- crates/dojo-lang/src/inline_macros/set.rs | 10 +++++----- crates/dojo-lang/src/lib.rs | 2 +- crates/dojo-lang/src/manifest.rs | 2 +- .../dojo-lang/src/{component.rs => model.rs} | 14 ++++++------- crates/dojo-lang/src/plugin.rs | 14 ++++++------- crates/dojo-lang/src/plugin_test.rs | 2 +- .../dojo-lang/src/plugin_test_data/introspect | 6 +++--- .../src/plugin_test_data/{component => model} | 20 +++++++++---------- crates/dojo-lang/src/system.rs | 2 +- examples/ecs/src/components.cairo | 4 ++-- 17 files changed, 57 insertions(+), 62 deletions(-) rename crates/dojo-core/src/{component.cairo => model.cairo} (72%) rename crates/dojo-lang/src/{component.rs => model.rs} (91%) rename crates/dojo-lang/src/plugin_test_data/{component => model} (96%) diff --git a/crates/dojo-core/src/executor_test.cairo b/crates/dojo-core/src/executor_test.cairo index 0efbdf852b..cfb0f5f4fe 100644 --- a/crates/dojo-core/src/executor_test.cairo +++ b/crates/dojo-core/src/executor_test.cairo @@ -10,7 +10,7 @@ use starknet::class_hash::Felt252TryIntoClassHash; use dojo::executor::{executor, IExecutorDispatcher, IExecutorDispatcherTrait}; use dojo::world::{IWorldDispatcher}; -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Foo { #[key] id: felt252, diff --git a/crates/dojo-core/src/lib.cairo b/crates/dojo-core/src/lib.cairo index 9370cb4f6a..450842abc6 100644 --- a/crates/dojo-core/src/lib.cairo +++ b/crates/dojo-core/src/lib.cairo @@ -4,7 +4,7 @@ mod database_test; mod executor; #[cfg(test)] mod executor_test; -mod component; +mod model; mod packing; #[cfg(test)] mod packing_test; diff --git a/crates/dojo-core/src/component.cairo b/crates/dojo-core/src/model.cairo similarity index 72% rename from crates/dojo-core/src/component.cairo rename to crates/dojo-core/src/model.cairo index 7366a0b869..d7e9ef7b42 100644 --- a/crates/dojo-core/src/component.cairo +++ b/crates/dojo-core/src/model.cairo @@ -1,4 +1,4 @@ -trait Component { +trait Model { fn name(self: @T) -> felt252; fn keys(self: @T) -> Span; fn values(self: @T) -> Span; @@ -6,13 +6,8 @@ trait Component { } #[starknet::interface] -trait IComponent { +trait IModel { fn name(self: @T) -> felt252; fn layout(self: @T) -> Span; fn schema(self: @T) -> Span; } - -#[starknet::interface] -trait ISystem { - fn name(self: @T) -> felt252; -} diff --git a/crates/dojo-core/src/world_test.cairo b/crates/dojo-core/src/world_test.cairo index 4259a98ce8..ef433e8ca2 100644 --- a/crates/dojo-core/src/world_test.cairo +++ b/crates/dojo-core/src/world_test.cairo @@ -14,7 +14,7 @@ use dojo::test_utils::{spawn_test_world, deploy_with_world_address}; // Components and Systems -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Foo { #[key] caller: ContractAddress, @@ -22,7 +22,7 @@ struct Foo { b: u128, } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Fizz { #[key] caller: ContractAddress, diff --git a/crates/dojo-defi/src/market/components.cairo b/crates/dojo-defi/src/market/components.cairo index 1619ce5e5c..c7da959dfe 100644 --- a/crates/dojo-defi/src/market/components.cairo +++ b/crates/dojo-defi/src/market/components.cairo @@ -1,5 +1,5 @@ use starknet::ContractAddress; -use dojo::component::StorageIntrospection; +use dojo::model::StorageIntrospection; // Cubit fixed point math library use cubit::f128::types::fixed::Fixed; @@ -24,14 +24,14 @@ impl SchemaIntrospectionFixed of SchemaIntrospection { } } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Cash { #[key] player: ContractAddress, amount: u128, } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Item { #[key] player: ContractAddress, @@ -40,7 +40,7 @@ struct Item { quantity: u128, } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Liquidity { #[key] player: ContractAddress, @@ -49,7 +49,7 @@ struct Liquidity { shares: Fixed, } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Market { #[key] item_id: u32, diff --git a/crates/dojo-erc/src/token/erc20_components.cairo b/crates/dojo-erc/src/token/erc20_components.cairo index bbde98ef81..ab68718dc9 100644 --- a/crates/dojo-erc/src/token/erc20_components.cairo +++ b/crates/dojo-erc/src/token/erc20_components.cairo @@ -1,6 +1,6 @@ use starknet::ContractAddress; -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct ERC20Balance { #[key] token: ContractAddress, @@ -9,7 +9,7 @@ struct ERC20Balance { amount: u256, } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct ERC20Allowance { #[key] token: ContractAddress, @@ -20,7 +20,7 @@ struct ERC20Allowance { amount: u256, } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct ERC20Meta { #[key] token: ContractAddress, diff --git a/crates/dojo-erc/src/token/erc721/components.cairo b/crates/dojo-erc/src/token/erc721/components.cairo index d42d1b040d..1bc4bcadd6 100644 --- a/crates/dojo-erc/src/token/erc721/components.cairo +++ b/crates/dojo-erc/src/token/erc721/components.cairo @@ -1,6 +1,6 @@ use starknet::ContractAddress; -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct ERC721Meta { #[key] token: ContractAddress, @@ -9,7 +9,7 @@ struct ERC721Meta { base_uri: felt252, } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct ERC721OperatorApproval { #[key] token: ContractAddress, @@ -20,7 +20,7 @@ struct ERC721OperatorApproval { approved: bool } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct ERC721Owner { #[key] token: ContractAddress, @@ -29,7 +29,7 @@ struct ERC721Owner { address: ContractAddress } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct ERC721Balance { #[key] token: ContractAddress, @@ -38,7 +38,7 @@ struct ERC721Balance { amount: u256, } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct ERC721TokenApproval { #[key] token: ContractAddress, diff --git a/crates/dojo-lang/src/inline_macros/set.rs b/crates/dojo-lang/src/inline_macros/set.rs index 8595c11366..0542affd95 100644 --- a/crates/dojo-lang/src/inline_macros/set.rs +++ b/crates/dojo-lang/src/inline_macros/set.rs @@ -68,7 +68,7 @@ impl InlineMacroExprPlugin for SetMacro { return InlinePluginResult { code: None, diagnostics: vec![PluginDiagnostic { - message: "Invalid arguments: No components provided.".to_string(), + message: "Invalid arguments: No models provided.".to_string(), stable_ptr: arg_list.args(db).stable_ptr().untyped(), }], }; @@ -77,10 +77,10 @@ impl InlineMacroExprPlugin for SetMacro { for entity in bundle { builder.add_str(&format!( "\n let __set_macro_value__ = {}; - {}.set_entity(dojo::component::Component::name(@__set_macro_value__), \ - dojo::component::Component::keys(@__set_macro_value__), 0_u8, \ - dojo::component::Component::values(@__set_macro_value__), \ - dojo::component::Component::layout(@__set_macro_value__));", + {}.set_entity(dojo::model::Model::name(@__set_macro_value__), \ + dojo::model::Model::keys(@__set_macro_value__), 0_u8, \ + dojo::model::Model::values(@__set_macro_value__), \ + dojo::model::Model::layout(@__set_macro_value__));", entity, world.as_syntax_node().get_text(db), )); diff --git a/crates/dojo-lang/src/lib.rs b/crates/dojo-lang/src/lib.rs index ba4f83d75c..e761ad00f4 100644 --- a/crates/dojo-lang/src/lib.rs +++ b/crates/dojo-lang/src/lib.rs @@ -4,10 +4,10 @@ //! //! Learn more at [dojoengine.gg](http://dojoengine.gg). pub mod compiler; -pub mod component; pub mod inline_macros; pub mod introspect; mod manifest; +pub mod model; pub mod plugin; pub mod print; pub mod system; diff --git a/crates/dojo-lang/src/manifest.rs b/crates/dojo-lang/src/manifest.rs index 17b3cdc55a..04dde184a4 100644 --- a/crates/dojo-lang/src/manifest.rs +++ b/crates/dojo-lang/src/manifest.rs @@ -93,7 +93,7 @@ impl Manifest { module_id: ModuleId, compiled_classes: &HashMap)>, ) { - for component in &aux_data.components { + for component in &aux_data.models { let component = component.clone(); let name: SmolStr = component.name.clone().into(); if let Ok(Some(ModuleItemId::Struct(_))) = diff --git a/crates/dojo-lang/src/component.rs b/crates/dojo-lang/src/model.rs similarity index 91% rename from crates/dojo-lang/src/component.rs rename to crates/dojo-lang/src/model.rs index c05beeef88..ba84e76a50 100644 --- a/crates/dojo-lang/src/component.rs +++ b/crates/dojo-lang/src/model.rs @@ -9,15 +9,15 @@ use convert_case::{Case, Casing}; use dojo_world::manifest::Member; use crate::introspect::handle_introspect_struct; -use crate::plugin::{Component, DojoAuxData}; +use crate::plugin::{DojoAuxData, Model}; -/// A handler for Dojo code that modifies a component struct. +/// A handler for Dojo code that modifies a model struct. /// Parameters: /// * db: The semantic database. -/// * struct_ast: The AST of the component struct. +/// * struct_ast: The AST of the model struct. /// Returns: /// * A RewriteNode containing the generated code. -pub fn handle_component_struct( +pub fn handle_model_struct( db: &dyn SyntaxGroup, aux_data: &mut DojoAuxData, struct_ast: ItemStruct, @@ -38,7 +38,7 @@ pub fn handle_component_struct( if keys.is_empty() { diagnostics.push(PluginDiagnostic { - message: "Component must define atleast one #[key] attribute".into(), + message: "Model must define atleast one #[key] attribute".into(), stable_ptr: struct_ast.name(db).stable_ptr().untyped(), }); } @@ -68,12 +68,12 @@ pub fn handle_component_struct( members.iter().filter_map(|m| serialize_member(m, false)).collect::<_>(); let name = struct_ast.name(db).text(db); - aux_data.components.push(Component { name: name.to_string(), members: members.to_vec() }); + aux_data.models.push(Model { name: name.to_string(), members: members.to_vec() }); ( RewriteNode::interpolate_patched( " - impl $type_name$Component of dojo::component::Component<$type_name$> { + impl $type_name$Model of dojo::model::Model<$type_name$> { #[inline(always)] fn name(self: @$type_name$) -> felt252 { '$type_name$' diff --git a/crates/dojo-lang/src/plugin.rs b/crates/dojo-lang/src/plugin.rs index 36db6b5c2d..767fdf068f 100644 --- a/crates/dojo-lang/src/plugin.rs +++ b/crates/dojo-lang/src/plugin.rs @@ -21,18 +21,18 @@ use semver::Version; use smol_str::SmolStr; use url::Url; -use crate::component::handle_component_struct; use crate::inline_macros::emit::EmitMacro; use crate::inline_macros::get::GetMacro; use crate::inline_macros::set::SetMacro; use crate::introspect::handle_introspect_struct; +use crate::model::handle_model_struct; use crate::print::derive_print; use crate::system::System; const SYSTEM_ATTR: &str = "system"; #[derive(Clone, Debug, PartialEq)] -pub struct Component { +pub struct Model { pub name: String, pub members: Vec, } @@ -46,8 +46,8 @@ pub struct SystemAuxData { /// Dojo related auxiliary data of the Dojo plugin. #[derive(Debug, Default, PartialEq)] pub struct DojoAuxData { - /// A list of components that were processed by the plugin. - pub components: Vec, + /// A list of models that were processed by the plugin. + pub models: Vec, /// A list of systems that were processed by the plugin and their component dependencies. pub systems: Vec, } @@ -150,13 +150,13 @@ impl MacroPlugin for DojoPlugin { continue; }; - // Get the text of the segment and check if it is "Component" + // Get the text of the segment and check if it is "Model" let derived = segment.ident(db).text(db); match derived.as_str() { - "Component" => { + "Model" => { let (component_rewrite_nodes, component_diagnostics) = - handle_component_struct(db, &mut aux_data, struct_ast.clone()); + handle_model_struct(db, &mut aux_data, struct_ast.clone()); rewrite_nodes.push(component_rewrite_nodes); diagnostics.extend(component_diagnostics); } diff --git a/crates/dojo-lang/src/plugin_test.rs b/crates/dojo-lang/src/plugin_test.rs index be26d616c2..7429e9c7dd 100644 --- a/crates/dojo-lang/src/plugin_test.rs +++ b/crates/dojo-lang/src/plugin_test.rs @@ -16,7 +16,7 @@ cairo_lang_test_utils::test_file_test!( expand_plugin, "src/plugin_test_data", { - component: "component", + model: "model", print: "print", introspect: "introspect", system: "system", diff --git a/crates/dojo-lang/src/plugin_test_data/introspect b/crates/dojo-lang/src/plugin_test_data/introspect index 07e7434705..81bb75b6ea 100644 --- a/crates/dojo-lang/src/plugin_test_data/introspect +++ b/crates/dojo-lang/src/plugin_test_data/introspect @@ -12,7 +12,7 @@ struct Vec2 { y: u32 } -#[derive(Component, Copy, Drop, Print, Introspect)] +#[derive(Model, Copy, Drop, Print, Introspect)] struct Position { #[key] player: ContractAddress, @@ -81,13 +81,13 @@ impl Vec2SchemaIntrospection of dojo::database::schema::SchemaIntrospection { +impl PositionModel of dojo::model::Model { #[inline(always)] fn name(self: @Position) -> felt252 { 'Position' diff --git a/crates/dojo-lang/src/plugin_test_data/component b/crates/dojo-lang/src/plugin_test_data/model similarity index 96% rename from crates/dojo-lang/src/plugin_test_data/component rename to crates/dojo-lang/src/plugin_test_data/model index 1009724cea..d2d3a2c57b 100644 --- a/crates/dojo-lang/src/plugin_test_data/component +++ b/crates/dojo-lang/src/plugin_test_data/model @@ -6,7 +6,7 @@ test_expand_plugin //! > cairo_code use serde::Serde; -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Position { #[key] id: felt252, @@ -32,14 +32,14 @@ impl PositionImpl of PositionTrait { } } -#[derive(Component, Serde)] +#[derive(Model, Serde)] struct Roles { role_ids: Array } use starknet::ContractAddress; -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Player { #[key] game: felt252, @@ -53,7 +53,7 @@ struct Player { use serde::Serde; -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Position { #[key] id: felt252, @@ -61,7 +61,7 @@ struct Position { y: felt252 } -impl PositionComponent of dojo::component::Component { +impl PositionModel of dojo::model::Model { #[inline(always)] fn name(self: @Position) -> felt252 { 'Position' @@ -195,12 +195,12 @@ impl PositionImpl of PositionTrait { } -#[derive(Component, Serde)] +#[derive(Model, Serde)] struct Roles { role_ids: Array } -impl RolesComponent of dojo::component::Component { +impl RolesModel of dojo::model::Model { #[inline(always)] fn name(self: @Roles) -> felt252 { 'Roles' @@ -300,7 +300,7 @@ mod roles { use starknet::ContractAddress; -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Player { #[key] game: felt252, @@ -309,7 +309,7 @@ struct Player { name: felt252, } -impl PlayerComponent of dojo::component::Component { +impl PlayerModel of dojo::model::Model { #[inline(always)] fn name(self: @Player) -> felt252 { 'Player' @@ -420,7 +420,7 @@ mod player { } //! > expected_diagnostics -error: Component must define atleast one #[key] attribute +error: Model must define atleast one #[key] attribute --> dummy_file.cairo:30:8 struct Roles { ^***^ diff --git a/crates/dojo-lang/src/system.rs b/crates/dojo-lang/src/system.rs index cabb5f7516..dd22a75068 100644 --- a/crates/dojo-lang/src/system.rs +++ b/crates/dojo-lang/src/system.rs @@ -71,7 +71,7 @@ impl System { name: name.clone(), content: builder.code, aux_data: Some(DynGeneratedFileAuxData::new(DojoAuxData { - components: vec![], + models: vec![], systems: vec![SystemAuxData { name, dependencies: system.dependencies.values().cloned().collect(), diff --git a/examples/ecs/src/components.cairo b/examples/ecs/src/components.cairo index 0fa9761372..df6b9d1155 100644 --- a/examples/ecs/src/components.cairo +++ b/examples/ecs/src/components.cairo @@ -68,7 +68,7 @@ impl DirectionIntoFelt252 of Into { } } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Moves { #[key] player: ContractAddress, @@ -82,7 +82,7 @@ struct Vec2 { y: u32 } -#[derive(Component, Copy, Drop, Print, Serde)] +#[derive(Model, Copy, Drop, Print, Serde)] struct Position { #[key] player: ContractAddress,