ui_macros: Remove DerivePathStr macro (#30862)

This PR removes the `DerivePathStr` macro, as it is no longer used.

Also removes the `PathStaticStr` macro from `gpui_macros`, which was
also unused.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2025-05-17 12:05:55 +02:00 committed by GitHub
parent f56960ab5b
commit 03419da6f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 0 additions and 277 deletions

2
Cargo.lock generated
View file

@ -15696,8 +15696,6 @@ dependencies = [
name = "ui_macros"
version = "0.1.0"
dependencies = [
"convert_case 0.8.0",
"proc-macro2",
"quote",
"syn 1.0.109",
"workspace-hack",

View file

@ -1,73 +0,0 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{Attribute, Data, DeriveInput, Lit, Meta, NestedMeta, parse_macro_input};
pub fn derive_path_static_str(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let prefix = get_attr_value(&input.attrs, "prefix").unwrap_or_else(|| "".to_string());
let suffix = get_attr_value(&input.attrs, "suffix").unwrap_or_else(|| "".to_string());
let delimiter = get_attr_value(&input.attrs, "delimiter").unwrap_or_else(|| "/".to_string());
let path_str_impl = impl_path_str(name, &input.data, &prefix, &suffix, &delimiter);
let expanded = quote! {
impl #name {
pub fn path_str(&self) -> &'static str {
#path_str_impl
}
}
};
TokenStream::from(expanded)
}
fn impl_path_str(
name: &syn::Ident,
data: &Data,
prefix: &str,
suffix: &str,
delimiter: &str,
) -> proc_macro2::TokenStream {
match *data {
Data::Enum(ref data) => {
let match_arms = data.variants.iter().map(|variant| {
let ident = &variant.ident;
let path = format!("{}{}{}{}{}", prefix, delimiter, ident, delimiter, suffix);
quote! {
#name::#ident => #path,
}
});
quote! {
match self {
#(#match_arms)*
}
}
}
_ => panic!("DerivePathStr only supports enums"),
}
}
fn get_attr_value(attrs: &[Attribute], key: &str) -> Option<String> {
attrs
.iter()
.filter(|attr| attr.path.is_ident("derive_path_static_str"))
.find_map(|attr| {
if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
meta_list.nested.iter().find_map(|nested_meta| {
if let NestedMeta::Meta(Meta::NameValue(name_value)) = nested_meta {
if name_value.path.is_ident(key) {
if let Lit::Str(lit_str) = &name_value.lit {
return Some(lit_str.value());
}
}
}
None
})
} else {
None
}
})
}

View file

@ -1,6 +1,5 @@
mod derive_app_context;
mod derive_into_element;
mod derive_path_static_str;
mod derive_render;
mod derive_visual_context;
mod register_action;
@ -31,12 +30,6 @@ pub fn derive_render(input: TokenStream) -> TokenStream {
derive_render::derive_render(input)
}
#[proc_macro_derive(PathStaticStr)]
#[doc(hidden)]
pub fn derive_path_static_str(input: TokenStream) -> TokenStream {
derive_path_static_str::derive_path_static_str(input)
}
/// #[derive(AppContext)] is used to create a context out of anything that holds a `&mut App`
/// Note that a `#[app]` attribute is required to identify the variable holding the &mut App.
///

View file

@ -1 +0,0 @@
mod path_str;

View file

@ -1,35 +0,0 @@
// We need to test [ui_macros::DerivePathStr] here as we can't invoke it
// in the `ui_macros` crate.
#[cfg(test)]
mod tests {
use strum::EnumString;
use ui_macros::{DerivePathStr, path_str};
#[test]
fn test_derive_path_str_with_prefix() {
#[derive(Debug, EnumString, DerivePathStr)]
#[strum(serialize_all = "snake_case")]
#[path_str(prefix = "test_prefix")]
enum SomeAsset {
FooBar,
Baz,
}
assert_eq!(SomeAsset::FooBar.path(), "test_prefix/foo_bar");
assert_eq!(SomeAsset::Baz.path(), "test_prefix/baz");
}
#[test]
fn test_derive_path_str_with_prefix_and_suffix() {
#[derive(Debug, EnumString, DerivePathStr)]
#[strum(serialize_all = "snake_case")]
#[path_str(prefix = "test_prefix", suffix = ".svg")]
enum SomeAsset {
FooBar,
Baz,
}
assert_eq!(SomeAsset::FooBar.path(), "test_prefix/foo_bar.svg");
assert_eq!(SomeAsset::Baz.path(), "test_prefix/baz.svg");
}
}

View file

@ -11,7 +11,6 @@ pub mod component_prelude;
mod components;
pub mod prelude;
mod styles;
mod tests;
mod traits;
pub mod utils;

View file

@ -13,8 +13,6 @@ path = "src/ui_macros.rs"
proc-macro = true
[dependencies]
convert_case.workspace = true
proc-macro2.workspace = true
quote.workspace = true
syn.workspace = true
workspace-hack.workspace = true

View file

@ -1,105 +0,0 @@
use convert_case::{Case, Casing};
use proc_macro::TokenStream;
use quote::quote;
use syn::{Attribute, Data, DeriveInput, Lit, Meta, NestedMeta, parse_macro_input};
pub fn derive_path_str(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let prefix = get_attr_value(&input.attrs, "prefix").expect("prefix attribute is required");
let suffix = get_attr_value(&input.attrs, "suffix").unwrap_or_else(|| "".to_string());
let serialize_all = get_strum_serialize_all(&input.attrs);
let path_str_impl = impl_path_str(name, &input.data, &prefix, &suffix, serialize_all);
let expanded = quote! {
impl #name {
pub fn path(&self) -> &'static str {
#path_str_impl
}
}
};
TokenStream::from(expanded)
}
fn impl_path_str(
name: &syn::Ident,
data: &Data,
prefix: &str,
suffix: &str,
serialize_all: Option<String>,
) -> proc_macro2::TokenStream {
match *data {
Data::Enum(ref data) => {
let match_arms = data.variants.iter().map(|variant| {
let ident = &variant.ident;
let variant_name = if let Some(ref case) = serialize_all {
match case.as_str() {
"snake_case" => ident.to_string().to_case(Case::Snake),
"lowercase" => ident.to_string().to_lowercase(),
_ => ident.to_string(),
}
} else {
ident.to_string()
};
let path = format!("{}/{}{}", prefix, variant_name, suffix);
quote! {
#name::#ident => #path,
}
});
quote! {
match self {
#(#match_arms)*
}
}
}
_ => panic!("DerivePathStr only supports enums"),
}
}
fn get_strum_serialize_all(attrs: &[Attribute]) -> Option<String> {
attrs
.iter()
.filter(|attr| attr.path.is_ident("strum"))
.find_map(|attr| {
if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
meta_list.nested.iter().find_map(|nested_meta| {
if let NestedMeta::Meta(Meta::NameValue(name_value)) = nested_meta {
if name_value.path.is_ident("serialize_all") {
if let Lit::Str(lit_str) = &name_value.lit {
return Some(lit_str.value());
}
}
}
None
})
} else {
None
}
})
}
fn get_attr_value(attrs: &[Attribute], key: &str) -> Option<String> {
attrs
.iter()
.filter(|attr| attr.path.is_ident("path_str"))
.find_map(|attr| {
if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
meta_list.nested.iter().find_map(|nested_meta| {
if let NestedMeta::Meta(Meta::NameValue(name_value)) = nested_meta {
if name_value.path.is_ident(key) {
if let Lit::Str(lit_str) = &name_value.lit {
return Some(lit_str.value());
}
}
}
None
})
} else {
None
}
})
}

View file

@ -1,59 +1,8 @@
mod derive_path_str;
mod derive_register_component;
mod dynamic_spacing;
use proc_macro::TokenStream;
/// Derives the `path` method for an enum.
///
/// This macro generates a `path` method for each variant of the enum, which returns a string
/// representation of the enum variant's path. The path is constructed using a prefix and
/// optionally a suffix, which are specified using attributes.
///
/// # Attributes
///
/// - `#[path_str(prefix = "...")]`: Required. Specifies the prefix for all paths.
/// - `#[path_str(suffix = "...")]`: Optional. Specifies a suffix for all paths.
/// - `#[strum(serialize_all = "...")]`: Optional. Specifies the case conversion for variant names.
///
/// # Example
///
/// ```
/// use strum::EnumString;
/// use ui_macros::{path_str, DerivePathStr};
///
/// #[derive(EnumString, DerivePathStr)]
/// #[path_str(prefix = "my_prefix", suffix = ".txt")]
/// #[strum(serialize_all = "snake_case")]
/// enum MyEnum {
/// VariantOne,
/// VariantTwo,
/// }
///
/// // These assertions would work if we could instantiate the enum
/// // assert_eq!(MyEnum::VariantOne.path(), "my_prefix/variant_one.txt");
/// // assert_eq!(MyEnum::VariantTwo.path(), "my_prefix/variant_two.txt");
/// ```
///
/// # Panics
///
/// This macro will panic if used on anything other than an enum.
#[proc_macro_derive(DerivePathStr, attributes(path_str))]
pub fn derive_path_str(input: TokenStream) -> TokenStream {
derive_path_str::derive_path_str(input)
}
/// A marker attribute for use with `DerivePathStr`.
///
/// This attribute is used to specify the prefix and suffix for the `path` method
/// generated by `DerivePathStr`. It doesn't modify the input and is only used as a
/// marker for the derive macro.
#[proc_macro_attribute]
pub fn path_str(_args: TokenStream, input: TokenStream) -> TokenStream {
// This attribute doesn't modify the input, it's just a marker
input
}
/// Generates the DynamicSpacing enum used for density-aware spacing in the UI.
#[proc_macro]
pub fn derive_dynamic_spacing(input: TokenStream) -> TokenStream {