Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend gbuilder by aliases and constructor #92

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions crates/config/src/spacing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ use serde::{de::Visitor, Deserialize};
use shared::value::TryFromValue;

#[derive(GenericBuilder, Debug, Default, Clone)]
#[gbuilder(name(GBuilderSpacing), derive(Clone))]
#[gbuilder(name(GBuilderSpacing), derive(Clone), constructor)]
pub struct Spacing {
#[gbuilder(default(0))]
#[gbuilder(default(0), aliases(vertical, all))]
top: u8,

#[gbuilder(default(0))]
#[gbuilder(default(0), aliases(horizontal, all))]
right: u8,

#[gbuilder(default(0))]
#[gbuilder(default(0), aliases(vertical, all))]
bottom: u8,

#[gbuilder(default(0))]
#[gbuilder(default(0), aliases(horizontal, all))]
left: u8,
}

Expand Down
36 changes: 32 additions & 4 deletions crates/filetype/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,18 @@ fn convert_type_value<'a>(
let type_name = type_value_pairs.next().unwrap().as_str();
let mut type_gbuilder: GBuilder = (type_name, alias_storage).try_into()?;

type_gbuilder.set_properties(
type_name,
convert_properties(&mut type_value_pairs, alias_storage),
);
let maybe_value = type_value_pairs.clone().nth(1).unwrap();
if maybe_value.as_rule() == Rule::Properties {
type_gbuilder.set_properties(
type_name,
convert_properties(&mut type_value_pairs, alias_storage),
);
} else {
type_gbuilder.constructor(
type_name,
convert_property_value(maybe_value, alias_storage)?,
);
}

Ok(type_gbuilder)
}
Expand All @@ -258,6 +266,26 @@ impl GBuilder {
}
}

fn constructor(&mut self, self_name: &str, value: Value) {
macro_rules! implement_variants {
($($variant:ident),*) => {
match self {
$(
Self::$variant(val) => {
val.constructor(value).err()
}
)*
}
};
}

if let Some(err) =
implement_variants!(FlexContainer, WImage, WText, Spacing, Alignment, Border)
{
warn!("Failed to call constructor of {self_name}, trying to defaulting. Error: {err}");
}
}

fn set_value(&mut self, field_name: &str, value: Value) -> Result<&mut Self, ConversionError> {
macro_rules! implement_variants {
($($variant:ident),*) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/filetype/src/layout.pest
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ PropertyValue = { TypeValue | UInt | Literal }

TypeValue = {
Identifier ~ OpeningParenthesis
~ Properties?
~ (Properties | PropertyValue)?
~ ClosingParenthesis }

Literal = ${ Hashtag? ~ ASCII_ALPHANUMERIC ~ (ASCII_ALPHANUMERIC | "_" | "-")* }
Expand Down
23 changes: 9 additions & 14 deletions crates/macros/src/config_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use syn::{

use crate::{
general::{
field_name, wrap_by_option, AttributeInfo, DefaultAssignment, DeriveInfo, Structure,
field_name, wrap_by_option, AttributeInfo, DefaultAssignment, DeriveInfo, ExpectIdent,
Structure,
},
propagate_err,
};
Expand Down Expand Up @@ -190,17 +191,14 @@ impl Structure {
&self,
attribute_info: &AttributeInfo<StructInfo, FieldInfo>,
) -> proc_macro2::TokenStream {
let field_idents: Punctuated<&syn::Ident, Token![,]> = self
.fields
.iter()
.map(|field| field.ident.as_ref().expect("Must be a name field!"))
.collect();
let field_idents: Punctuated<&syn::Ident, Token![,]> =
self.fields.iter().map(ExpectIdent::expect_ident).collect();

let init_members: Punctuated<proc_macro2::TokenStream, Token![,]> = self
.fields
.iter()
.map(|field| {
let ident = field.ident.as_ref().expect("Must be a named field");
let ident = field.expect_ident();
let mut line = quote! { #ident: #ident };

if attribute_info.is_mergeable_field(field)
Expand Down Expand Up @@ -237,7 +235,7 @@ impl Structure {
.fields
.iter()
.flat_map(|field| {
let field_ident = field.ident.as_ref().expect("Fields should be named");
let field_ident = field.expect_ident();
attribute_info
.fields_info
.get(&field_name(field))
Expand Down Expand Up @@ -266,18 +264,15 @@ impl Structure {
target_type: &syn::Ident,
attribute_info: &AttributeInfo<StructInfo, FieldInfo>,
) -> proc_macro2::TokenStream {
let field_idents: Punctuated<&syn::Ident, Token![,]> = self
.fields
.iter()
.map(|field| field.ident.as_ref().expect("Must be a name field!"))
.collect();
let field_idents: Punctuated<&syn::Ident, Token![,]> =
self.fields.iter().map(ExpectIdent::expect_ident).collect();

let init_members: Punctuated<proc_macro2::TokenStream, Token![,]> = self
.fields
.iter()
.filter(|field| !attribute_info.is_temporary_field(field))
.map(|field| {
let ident = field.ident.as_ref().expect("Must be a named field");
let ident = field.expect_ident();
let mut line = quote! { #ident: #ident };

if let Some(field_info) = attribute_info.fields_info.get(&field_name(field)) {
Expand Down
16 changes: 11 additions & 5 deletions crates/macros/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ fn matches(attribute: &syn::Attribute, attribute_name: &str) -> bool {
false
}
pub(crate) fn field_name(field: &syn::Field) -> String {
field
.ident
.as_ref()
.expect("Must be a named field")
.to_string()
field.expect_ident().to_string()
}

pub(crate) enum DefaultAssignment {
Expand Down Expand Up @@ -184,6 +180,16 @@ impl ToTokens for DeriveInfo {
}
}

pub(crate) trait ExpectIdent {
fn expect_ident(&self) -> &syn::Ident;
}

impl ExpectIdent for syn::Field {
fn expect_ident(&self) -> &syn::Ident {
self.ident.as_ref().expect("Fields should be named!")
}
}

pub(crate) fn wrap_by_option(ty: syn::Type) -> syn::Type {
use proc_macro2::Span;
use syn::PathSegment;
Expand Down
Loading
Loading