refactor: update default configuration and simplify channel enum conversions (#157)

This commit is contained in:
Alex Pasmantier 2024-12-28 20:09:33 +01:00 committed by GitHub
parent ee71e4788f
commit 697f295afb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 76 deletions

View File

@ -163,6 +163,10 @@ toggle_preview = "ctrl-o"
# shell history (according to your shell) # shell history (according to your shell)
"" = "zsh-history" "" = "zsh-history"
# environment variables
"export" = "env"
"unset" = "env"
# dirs channel # dirs channel
"cd" = "dirs" "cd" = "dirs"
"ls" = "dirs" "ls" = "dirs"
@ -184,4 +188,4 @@ toggle_preview = "ctrl-o"
"docker run" = "docker-images" "docker run" = "docker-images"
# gitrepos channel # gitrepos channel
"nvim" = "gitrepos" "nvim" = "git-repos"

View File

@ -67,43 +67,18 @@ fn impl_cli_channel(ast: &syn::DeriveInput) -> TokenStream {
}) })
.collect(); .collect();
// Generate lowercase mappings for the TryFrom implementation
let lowercase_match_arms: Vec<_> = variants
.iter()
.filter(|variant| !has_attribute(&variant.attrs, EXCLUDE_FROM_CLI))
.map(|variant| {
let variant_name = &variant.ident;
let lowercase_name = variant_name.to_string().to_lowercase();
quote! {
#lowercase_name => Ok(Self::#variant_name),
}
})
.collect();
let cli_enum = quote! { let cli_enum = quote! {
use clap::ValueEnum; use clap::ValueEnum;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::Display; use strum::{Display, EnumString};
use std::default::Default; use std::default::Default;
#[derive(Debug, Clone, ValueEnum, Default, Copy, PartialEq, Eq, Serialize, Deserialize, Display)] #[derive(Debug, Clone, ValueEnum, EnumString, Default, Copy, PartialEq, Eq, Serialize, Deserialize, Display)]
#[strum(serialize_all = "kebab_case")]
pub enum CliTvChannel { pub enum CliTvChannel {
#[default] #[default]
#(#cli_enum_variants),* #(#cli_enum_variants),*
} }
impl std::convert::TryFrom<&str> for CliTvChannel {
type Error = String;
fn try_from(channel: &str) -> Result<Self, Self::Error> {
match channel {
#(
#lowercase_match_arms
)*
_ => Err(format!("Invalid cli channel name: {}", channel)),
}
}
}
}; };
// Generate the match arms for the `to_channel` method // Generate the match arms for the `to_channel` method
@ -331,7 +306,8 @@ fn impl_unit_channel(ast: &syn::DeriveInput) -> TokenStream {
// Generate a unit enum from the given enum // Generate a unit enum from the given enum
let unit_enum = quote! { let unit_enum = quote! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumString)]
#[strum(serialize_all = "kebab_case")]
pub enum UnitChannel { pub enum UnitChannel {
#( #(
#variant_names, #variant_names,
@ -368,56 +344,10 @@ fn impl_unit_channel(ast: &syn::DeriveInput) -> TokenStream {
} }
}; };
// Generate TryFrom<&str> implementation
let try_from_str_impl = quote! {
impl std::convert::TryFrom<&str> for UnitChannel {
type Error = String;
fn try_from(channel: &str) -> Result<Self, Self::Error> {
match channel {
#(
stringify!(#variant_names) => Ok(Self::#variant_names),
)*
_ => Err(format!("Invalid unit channel name: {}", channel)),
}
}
}
};
// Generate From<&str> implementation
//let from_str_impl = quote! {
// impl From<&str> for UnitChannel {
// fn from(channel: &str) -> Self {
// match channel {
// #(
// stringify!(#variant_names) => Self::#variant_names,
// )*
// _ => panic!("Invalid unit channel name."),
// }
// }
// }
//};
// Generate Into<&str> implementation
let into_str_impl = quote! {
impl Into<&str> for UnitChannel {
fn into(self) -> &'static str {
match self {
#(
UnitChannel::#variant_names => stringify!(#variant_names),
)*
}
}
}
};
let gen = quote! { let gen = quote! {
#unit_enum #unit_enum
#into_impl #into_impl
#from_impl #from_impl
#try_from_str_impl
//#from_str_impl
#into_str_impl
}; };
gen.into() gen.into()