From ef3ac8fc95d592928ae5552a6d784399d35efa69 Mon Sep 17 00:00:00 2001 From: Paris DOUADY Date: Sun, 30 Jun 2024 13:19:30 +0200 Subject: [PATCH] put base & template in own file --- prototypes/src/prototypes/_template.rs | 44 +++++++++++++++ prototypes/src/prototypes/base.rs | 27 +++++++++ prototypes/src/prototypes/mod.rs | 77 +------------------------- 3 files changed, 74 insertions(+), 74 deletions(-) create mode 100644 prototypes/src/prototypes/_template.rs create mode 100644 prototypes/src/prototypes/base.rs diff --git a/prototypes/src/prototypes/_template.rs b/prototypes/src/prototypes/_template.rs new file mode 100644 index 00000000..aa452294 --- /dev/null +++ b/prototypes/src/prototypes/_template.rs @@ -0,0 +1,44 @@ +// Prototype template. remplace $proto with the root name e.g Item + +use crate::{NoParent, Prototype, PrototypeBase, get_lua}; +use mlua::Table; +use std::ops::Deref; + +use super::*; + +/// $protoPrototype is +#[derive(Clone, Debug)] +pub struct $protoPrototype { + pub base: $parent, + pub id: $protoPrototypeID, +} + +impl Prototype for $protoPrototype { + type Parent = $parent; + type ID = $protoPrototypeID; + const NAME: &'static str = ; + + fn from_lua(table: &Table) -> mlua::Result { + let base = $parent::from_lua(table)?; + Ok(Self { + id: Self::ID::new(&base.name), + base, + }) + } + + fn id(&self) -> Self::ID { + self.id + } + + fn parent(&self) -> Option<&Self::Parent> { + Some(&self.base) + } +} + +impl Deref for $protoPrototype { + type Target = $parent; + + fn deref(&self) -> &Self::Target { + &self.base + } +} \ No newline at end of file diff --git a/prototypes/src/prototypes/base.rs b/prototypes/src/prototypes/base.rs new file mode 100644 index 00000000..e7445627 --- /dev/null +++ b/prototypes/src/prototypes/base.rs @@ -0,0 +1,27 @@ +#[derive(Debug, Clone, egui_inspect::Inspect)] +pub struct PrototypeBase { + pub name: String, + pub order: String, + pub label: String, +} + +impl crate::Prototype for PrototypeBase { + type Parent = crate::NoParent; + type ID = (); + const NAME: &'static str = "base"; + + fn from_lua(table: &mlua::Table) -> mlua::Result { + use crate::get_lua; + Ok(Self { + name: get_lua(table, "name")?, + order: get_lua(table, "order").unwrap_or(String::new()), + label: get_lua(table, "label")?, + }) + } + + fn id(&self) -> Self::ID {} + + fn parent(&self) -> &Self::Parent { + &crate::NoParent + } +} diff --git a/prototypes/src/prototypes/mod.rs b/prototypes/src/prototypes/mod.rs index a77a20c3..6bf41143 100644 --- a/prototypes/src/prototypes/mod.rs +++ b/prototypes/src/prototypes/mod.rs @@ -1,3 +1,4 @@ +// Find template in _template.rs crate::gen_prototypes!( mod item: ItemID = ItemPrototype, mod building: BuildingPrototypeID = BuildingPrototype, @@ -13,77 +14,5 @@ crate::gen_prototypes!( mod freightstation: FreightStationPrototypeID = FreightStationPrototype, ); -/** Prototype template. remplace $proto with the root name e.g Item -```rs -use crate::{NoParent, Prototype, PrototypeBase, get_lua}; -use mlua::Table; -use std::ops::Deref; - -use super::*; - -/// $protoPrototype is -#[derive(Clone, Debug)] -pub struct $protoPrototype { - pub base: $parent, - pub id: $protoPrototypeID, -} - -impl Prototype for $protoPrototype { - type Parent = $parent; - type ID = $protoPrototypeID; - const NAME: &'static str = ; - - fn from_lua(table: &Table) -> mlua::Result { - let base = $parent::from_lua(table)?; - Ok(Self { - id: Self::ID::new(&base.name), - base, - }) - } - - fn id(&self) -> Self::ID { - self.id - } - - fn parent(&self) -> Option<&Self::Parent> { - Some(&self.base) - } -} - -impl Deref for $protoPrototype { - type Target = $parent; - - fn deref(&self) -> &Self::Target { - &self.base - } -} -``` -*/ - -#[derive(Debug, Clone, egui_inspect::Inspect)] -pub struct PrototypeBase { - pub name: String, - pub order: String, - pub label: String, -} - -impl crate::Prototype for PrototypeBase { - type Parent = crate::NoParent; - type ID = (); - const NAME: &'static str = "base"; - - fn from_lua(table: &mlua::Table) -> mlua::Result { - use crate::get_lua; - Ok(Self { - name: get_lua(table, "name")?, - order: get_lua(table, "order").unwrap_or(String::new()), - label: get_lua(table, "label")?, - }) - } - - fn id(&self) -> Self::ID {} - - fn parent(&self) -> &Self::Parent { - &crate::NoParent - } -} +mod base; +pub use base::*;