From 5c745a1c5c2a8fd1972ddbcf26a0224936cf1fa9 Mon Sep 17 00:00:00 2001 From: Roms1383 Date: Thu, 8 Feb 2024 10:05:38 +0700 Subject: [PATCH] :construction: work in progress : fixes --- macros/src/lib.rs | 65 ++++++++++------------ src/ink/mod.rs | 16 ++---- src/ink/widget/implementation.rs | 1 + src/lib.rs | 94 ++++++++++++++++---------------- 4 files changed, 80 insertions(+), 96 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index aedbd8a..4bf78eb 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -44,11 +44,12 @@ fn derive_reds_widget_leaf_for_struct(name: &syn::Ident, r#struct: &syn::DataStr fn reds_widget_leaf(&self, instance: &str, parent: Option<&str>) -> String { use ::red4ext_rs::conv::NativeRepr; use crate::RedsValue; + use crate::IsDefault; let mut steps = vec![]; steps.push(format!("let {} = new {}();", instance, Self::NAME)); #( - if let Some(v) = self.#oneliners.reds_value() { - steps.push(::std::format!("{}.{} = {};", instance, ::std::stringify!(#oneliners), v)); + if !self.#oneliners.is_default() { + steps.push(::std::format!("{}.{} = {};", instance, ::std::stringify!(#oneliners), self.#oneliners.reds_value())); } )* steps.join("\n") @@ -67,16 +68,13 @@ fn derive_reds_value_for_struct(name: &syn::Ident, r#struct: &syn::DataStruct) - let indexes = r#struct.fields.iter().enumerate().map(|(index, _)| syn::Index::from(index)); return quote! { impl crate::RedsValue for #name { - fn reds_value(&self) -> Option { + fn reds_value(&self) -> String { use ::red4ext_rs::conv::NativeRepr; - if self == &Self::default() { - return None; - } let mut args = Vec::::new(); #( - args.push(self.#indexes.reds_value().unwrap_or(Default::default())); + args.push(self.#indexes.reds_value()); )* - Some(format!("new {}({})", Self::NAME, args.join(", "))) + format!("new {}({})", Self::NAME, args.join(", ")) } } }.into() @@ -84,16 +82,13 @@ fn derive_reds_value_for_struct(name: &syn::Ident, r#struct: &syn::DataStruct) - let fields = r#struct.fields.iter().map(|x| x.ident.clone()); quote! { impl crate::RedsValue for #name { - fn reds_value(&self) -> Option { + fn reds_value(&self) -> String { use ::red4ext_rs::conv::NativeRepr; - if self == &Self::default() { - return None; - } let mut args = Vec::::new(); #( - args.push(self.#fields.reds_value().unwrap_or(Default::default())); + args.push(self.#fields.reds_value()); )* - Some(format!("new {}({})", Self::NAME, args.join(", "))) + format!("new {}({})", Self::NAME, args.join(", ")) } } }.into() @@ -109,16 +104,13 @@ fn derive_reds_value_for_enum(name: &syn::Ident, r#enum: &syn::DataEnum) -> Toke .into() } quote!{ - #name::#variant => Some(::std::format!("{}.{}", Self::NAME, ::std::stringify!(#variant))) + #name::#variant => ::std::format!("{}.{}", Self::NAME, ::std::stringify!(#variant)) } }); quote! { impl crate::RedsValue for #name { - fn reds_value(&self) -> Option { + fn reds_value(&self) -> String { use ::red4ext_rs::conv::NativeRepr; - if self == &Self::default() { - return None; - } match self { #(#matches),* } @@ -138,36 +130,37 @@ fn derive_reds_widget_compound_for_struct(name: &syn::Ident, r#struct: &syn::Dat use ::red4ext_rs::conv::NativeRepr; use crate::widget::layout::inkEChildOrder; use crate::RedsValue; + use crate::IsDefault; let mut steps = vec![]; - steps.push(format!("let {} = new {}();", instance, Self::NAME)); + steps.push(::std::format!("let {} = new {}();", instance, Self::NAME)); #( - if let Some(v) = self.#oneliners.reds_value() { - steps.push(::std::format!("{}.{} = {};", instance, ::std::stringify!(#oneliners), v)); + if !self.#oneliners.is_default() { + steps.push(::std::format!("{}.{} = {};", instance, ::std::stringify!(#oneliners), self.#oneliners.reds_value())); } )* let mut child_name; - let parent_name = self.name.reds_value(); + let mut result: String; + let parent_name = if self.name.is_default() { None } else { Some(self.name.reds_value()) }; + let borrow_parent_name = parent_name.as_ref().map(|x| x.as_str()); if self.child_order == inkEChildOrder::Forward { for child in self.children.iter() { child_name = child.name().expect("no child should be a inkMultiChildren"); - steps.push(child.reds_widget(child_name, parent_name.as_deref())); + result = child.reds_widget(child_name, borrow_parent_name); + steps.push(result.to_owned()); } - if let Some(v) = parent_name { - for child in self.children.iter() { - child_name = child.name().expect("no child should be a inkMultiChildren"); - steps.push(format!("{}.AddChild({});", instance, child_name)); - } + for child in self.children.iter() { + child_name = child.name().expect("no child should be a inkMultiChildren"); + steps.push(format!("{}.AddChild({});", instance, child_name)); } } else { for child in self.children.iter().rev() { - child_name = child.name().expect("no child to be a inkMultiChildren"); - steps.push(child.reds_widget(child_name, parent_name.as_deref())); + child_name = child.name().expect("no child should be a inkMultiChildren"); + result = child.reds_widget(child_name, borrow_parent_name); + steps.push(result.to_owned()); } - if let Some(v) = parent_name { - for child in self.children.iter() { - child_name = child.name().expect("no child should be a inkMultiChildren"); - steps.push(format!("{}.AddChild({});", instance, child_name)); - } + for child in self.children.iter().rev() { + child_name = child.name().expect("no child should be a inkMultiChildren"); + steps.push(format!("{}.AddChild({});", instance, child_name)); } } steps.join("\n") diff --git a/src/ink/mod.rs b/src/ink/mod.rs index 7398a7d..e6a53d8 100644 --- a/src/ink/mod.rs +++ b/src/ink/mod.rs @@ -40,13 +40,8 @@ impl Default for ResourcePath { } impl RedsValue for ResourcePath { - fn reds_value(&self) -> Option { - if self.0.as_os_str().to_str().is_none() - || self.0.as_os_str().to_str().unwrap().to_string().is_empty() - { - return None; - } - Some(format!("r\"{}\"", self.0.as_os_str().to_str().unwrap())) + fn reds_value(&self) -> String { + format!("r\"{}\"", self.0.as_os_str().to_str().unwrap_or_default()) } } @@ -159,11 +154,8 @@ impl CName { } impl RedsValue for CName { - fn reds_value(&self) -> Option { - if self.0 == "None" { - return None; - } - Some(self.0.clone()) + fn reds_value(&self) -> String { + format!("n\"{}\"", self.0.clone()) } } diff --git a/src/ink/widget/implementation.rs b/src/ink/widget/implementation.rs index df5021d..c00c45b 100644 --- a/src/ink/widget/implementation.rs +++ b/src/ink/widget/implementation.rs @@ -161,6 +161,7 @@ impl_classname!(inkShapeWidget); impl_classname!(inkCircleWidget); impl_classname!(inkRectangleWidget); impl_classname!(inkVectorGraphicWidget); + impl Widget { pub fn name(&self) -> Option<&str> { match self { diff --git a/src/lib.rs b/src/lib.rs index ad6fc16..ea28d83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,21 @@ mod ink; use ink::widget::Widget; pub use ink::*; +pub trait IsDefault { + fn is_default(&self) -> bool; +} + +impl IsDefault for T +where + T: Default + PartialEq, +{ + fn is_default(&self) -> bool { + self == &T::default() + } +} + pub trait RedsValue { - fn reds_value(&self) -> Option; + fn reds_value(&self) -> String; } pub trait RedsWidgetLeaf { @@ -71,12 +84,13 @@ impl RedsValue for Vec where T: RedsValue, { - fn reds_value(&self) -> Option { - Some( + fn reds_value(&self) -> String { + format!( + "[{}]", self.iter() - .map(|x| x.reds_value().unwrap_or_default()) + .map(|x| x.reds_value()) .collect::>() - .join(""), + .join(", "), ) } } @@ -85,86 +99,70 @@ impl RedsValue for Option where T: RedsValue, { - fn reds_value(&self) -> Option { - self.as_ref().and_then(|x| x.reds_value()) + fn reds_value(&self) -> String { + if self.is_some() { + self.as_ref().unwrap().reds_value() + } else { + "".to_string() + } } } impl RedsValue for f32 { - fn reds_value(&self) -> Option { + fn reds_value(&self) -> String { if self == &self.trunc() { - Some(format!("{}.", self)) + format!("{}.", self) } else { - Some(format!("{}", self)) + format!("{}", self) } } } impl RedsValue for i32 { - fn reds_value(&self) -> Option { - Some(format!("{}", self.clone())) + fn reds_value(&self) -> String { + format!("{}", self.clone()) } } impl RedsValue for u16 { - fn reds_value(&self) -> Option { - Some(format!("{}", self.clone())) + fn reds_value(&self) -> String { + format!("{}", self.clone()) } } impl RedsValue for u32 { - fn reds_value(&self) -> Option { - Some(format!("{}", self.clone())) + fn reds_value(&self) -> String { + format!("{}", self.clone()) } } impl RedsValue for bool { - fn reds_value(&self) -> Option { + fn reds_value(&self) -> String { if !self { - None + "false".to_string() } else { - Some("true".to_string()) + "true".to_string() } } } impl RedsValue for String { - fn reds_value(&self) -> Option { - if self.is_empty() { - None - } else { - Some(self.clone()) - } + fn reds_value(&self) -> String { + self.clone() } } -// impl RedsValue for Name { -// fn reds_value(&self) -> Option { -// match ( -// self.r#type.as_str(), -// self.storage.as_str(), -// self.value.as_str(), -// ) { -// ("ResourcePath", "string", "") => None, -// ("ResourcePath", "string", v) => Some(format!("r\"{v}\"")), -// ("CName", "string", "None") => None, -// ("CName", "string", v) => Some(format!("n\"{v}\"")), -// _ => unreachable!(), -// } -// } -// } - impl RedsValue for LocalizationString { - fn reds_value(&self) -> Option { + fn reds_value(&self) -> String { if let Some(ref v) = self.value { return match v { - LocKey::ID(v) if v == &0 => None, - LocKey::ID(v) => Some(format!("LocKey#{}", v)), - LocKey::Value(v) if v.as_str() == "null" => None, - LocKey::Value(v) if v.as_str() == "None" => None, - LocKey::Value(v) => Some(format!("l\"{}\"", v)), + LocKey::ID(v) if v == &0 => "null".to_string(), + LocKey::ID(v) => format!("LocKey#{}", v), + LocKey::Value(v) if v.as_str() == "null" => "null".to_string(), + LocKey::Value(v) if v.as_str() == "None" => "null".to_string(), + LocKey::Value(v) => format!("l\"{}\"", v), }; } - None + "null".to_string() } }