Skip to content

Commit

Permalink
🚧 work in progress : fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Roms1383 committed Feb 8, 2024
1 parent 68d9f67 commit 5c745a1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 96 deletions.
65 changes: 29 additions & 36 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -67,33 +68,27 @@ 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<String> {
fn reds_value(&self) -> String {
use ::red4ext_rs::conv::NativeRepr;
if self == &Self::default() {
return None;
}
let mut args = Vec::<String>::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()
}
let fields = r#struct.fields.iter().map(|x| x.ident.clone());
quote! {
impl crate::RedsValue for #name {
fn reds_value(&self) -> Option<String> {
fn reds_value(&self) -> String {
use ::red4ext_rs::conv::NativeRepr;
if self == &Self::default() {
return None;
}
let mut args = Vec::<String>::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()
Expand All @@ -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<String> {
fn reds_value(&self) -> String {
use ::red4ext_rs::conv::NativeRepr;
if self == &Self::default() {
return None;
}
match self {
#(#matches),*
}
Expand All @@ -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")
Expand Down
16 changes: 4 additions & 12 deletions src/ink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ impl Default for ResourcePath {
}

impl RedsValue for ResourcePath {
fn reds_value(&self) -> Option<String> {
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())
}
}

Expand Down Expand Up @@ -159,11 +154,8 @@ impl CName {
}

impl RedsValue for CName {
fn reds_value(&self) -> Option<String> {
if self.0 == "None" {
return None;
}
Some(self.0.clone())
fn reds_value(&self) -> String {
format!("n\"{}\"", self.0.clone())
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ink/widget/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
94 changes: 46 additions & 48 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ mod ink;
use ink::widget::Widget;
pub use ink::*;

pub trait IsDefault {
fn is_default(&self) -> bool;
}

impl<T> IsDefault for T
where
T: Default + PartialEq,
{
fn is_default(&self) -> bool {
self == &T::default()
}
}

pub trait RedsValue {
fn reds_value(&self) -> Option<String>;
fn reds_value(&self) -> String;
}

pub trait RedsWidgetLeaf {
Expand Down Expand Up @@ -71,12 +84,13 @@ impl<T> RedsValue for Vec<T>
where
T: RedsValue,
{
fn reds_value(&self) -> Option<String> {
Some(
fn reds_value(&self) -> String {
format!(
"[{}]",
self.iter()
.map(|x| x.reds_value().unwrap_or_default())
.map(|x| x.reds_value())
.collect::<Vec<_>>()
.join(""),
.join(", "),
)
}
}
Expand All @@ -85,86 +99,70 @@ impl<T> RedsValue for Option<T>
where
T: RedsValue,
{
fn reds_value(&self) -> Option<String> {
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<String> {
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<String> {
Some(format!("{}", self.clone()))
fn reds_value(&self) -> String {
format!("{}", self.clone())
}
}

impl RedsValue for u16 {
fn reds_value(&self) -> Option<String> {
Some(format!("{}", self.clone()))
fn reds_value(&self) -> String {
format!("{}", self.clone())
}
}

impl RedsValue for u32 {
fn reds_value(&self) -> Option<String> {
Some(format!("{}", self.clone()))
fn reds_value(&self) -> String {
format!("{}", self.clone())
}
}

impl RedsValue for bool {
fn reds_value(&self) -> Option<String> {
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<String> {
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<String> {
// 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<String> {
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()
}
}

0 comments on commit 5c745a1

Please sign in to comment.