Skip to content

Commit

Permalink
Merge branch 'debridge-finance-352-const-into-str'
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Glotfelty committed Nov 25, 2024
2 parents b03e1a0 + d332030 commit b023d23
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
4 changes: 4 additions & 0 deletions strum_macros/src/helpers/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod kw {

// enum metadata
custom_keyword!(serialize_all);
custom_keyword!(const_into_str);
custom_keyword!(use_phf);
custom_keyword!(prefix);
custom_keyword!(parse_err_ty);
Expand Down Expand Up @@ -61,6 +62,7 @@ pub enum EnumMeta {
kw: kw::parse_err_fn,
path: Path,
},
ConstIntoStr(kw::const_into_str)
}

impl Parse for EnumMeta {
Expand Down Expand Up @@ -104,6 +106,8 @@ impl Parse for EnumMeta {
let path_tokens = parse_str(&path_str.value())?;
let path = parse2(path_tokens)?;
Ok(EnumMeta::ParseErrFn { kw, path })
} else if lookahead.peek(kw::const_into_str) {
Ok(EnumMeta::ConstIntoStr(input.parse()?))
} else {
Err(lookahead.error())
}
Expand Down
11 changes: 11 additions & 0 deletions strum_macros/src/helpers/type_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct StrumTypeProperties {
pub use_phf: bool,
pub prefix: Option<LitStr>,
pub enum_repr: Option<TokenStream>,
pub const_into_str: bool,
}

impl HasTypeProperties for DeriveInput {
Expand All @@ -41,6 +42,8 @@ impl HasTypeProperties for DeriveInput {
let mut use_phf_kw = None;
let mut crate_module_path_kw = None;
let mut prefix_kw = None;
let mut const_into_str = None;

for meta in strum_meta {
match meta {
EnumMeta::SerializeAll { case_style, kw } => {
Expand Down Expand Up @@ -102,6 +105,14 @@ impl HasTypeProperties for DeriveInput {
parse_err_fn_kw = Some(kw);
output.parse_err_fn = Some(path);
}
EnumMeta::ConstIntoStr(kw) => {
if let Some(fst_kw) = const_into_str {
return Err(occurrence_error(fst_kw, kw, "const_into_str"));
}

const_into_str = Some(kw);
output.const_into_str = true;
}
}
}

Expand Down
24 changes: 23 additions & 1 deletion strum_macros/src/macros/strings/as_ref_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn as_static_str_inner(
}
}
},
GenerateTraitVariant::From => quote! {
GenerateTraitVariant::From if !type_properties.const_into_str => quote! {
impl #impl_generics ::core::convert::From<#name #ty_generics> for &'static str #where_clause {
#[inline]
fn from(x: #name #ty_generics) -> &'static str {
Expand All @@ -119,5 +119,27 @@ pub fn as_static_str_inner(
}
}
},
GenerateTraitVariant::From => quote! {
impl #impl_generics #name #ty_generics #where_clause {
pub const fn into_str(&self) -> &'static str {
match self {
#(#arms3),*
}
}
}

impl #impl_generics ::core::convert::From<#name #ty_generics> for &'static str #where_clause {
fn from(x: #name #ty_generics) -> &'static str {
match x {
#(#arms2),*
}
}
}
impl #impl_generics2 ::core::convert::From<&'_derivative_strum #name #ty_generics> for &'static str #where_clause {
fn from(x: &'_derivative_strum #name #ty_generics) -> &'static str {
x.into_str()
}
}
},
})
}
74 changes: 74 additions & 0 deletions strum_tests/tests/as_ref_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,77 @@ fn brightness_serialize_all() {
assert_eq!("dim", <&'static str>::from(Brightness::Dim { glow: 0 }));
assert_eq!("Bright", <&'static str>::from(Brightness::BrightWhite));
}

#[derive(IntoStaticStr)]
#[strum(const_into_str)]
enum Bar<'a, T>
where
T: AsRef<str>,
{
A(T),
B,
C(&'a i32),
#[strum(serialize = "Dark")]
D,
#[strum(to_string = "Green")]
G,
#[strum(serialize = "b", to_string = "blue")]
Blue { hue: usize },
#[strum(serialize = "y", serialize = "yellow")]
Yellow,
}

#[derive(IntoStaticStr)]
#[strum(const_into_str)]
enum Baz<'a, T> {
A(T),
C(&'a i32),
}

#[derive(IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
#[strum(const_into_str)]
enum BrightnessConst {
DarkBlack,
Dim {
glow: usize,
},
#[strum(serialize = "Bright")]
BrightWhite,
}

#[test]
fn test_const_into_static_str() {

const A: &'static str = Bar::A("foo").into_str();
assert_eq!("A", A);
const B: &'static str = Bar::B::<&str>.into_str();
assert_eq!("B", B);
const C: &'static str = Bar::C::<&str>(&12).into_str();
assert_eq!("C", C);

const D: &'static str = Bar::D::<&str>.into_str();
assert_eq!("Dark", D);

const G: &'static str = Bar::G::<&str>.into_str();
assert_eq!("Green", G);

const BLUE: &'static str = Bar::Blue::<&str>{ hue: 2 }.into_str();
assert_eq!("blue", BLUE);

const YELLOW: &'static str = Bar::Yellow::<&str>.into_str();
assert_eq!("yellow", YELLOW);

const BAZ_A: &'static str = Baz::A("foo").into_str();
assert_eq!("A", BAZ_A);

const BAZ_C: &'static str = Baz::C::<&str>(&6).into_str();
assert_eq!("C", BAZ_C);

const DARK_BLACK: &'static str = BrightnessConst::DarkBlack.into_str();
assert_eq!("dark_black", DARK_BLACK);
const DIM: &'static str = BrightnessConst::Dim {glow:1}.into_str();
assert_eq!("dim", DIM);
const BRIGHT_WHITE: &'static str = BrightnessConst::BrightWhite.into_str();
assert_eq!("Bright", BRIGHT_WHITE);
}

0 comments on commit b023d23

Please sign in to comment.