From 697f295afb930298f8e37e536ce89a573b863a29 Mon Sep 17 00:00:00 2001 From: Alex Pasmantier <47638216+alexpasmantier@users.noreply.github.com> Date: Sat, 28 Dec 2024 20:09:33 +0100 Subject: [PATCH] refactor: update default configuration and simplify channel enum conversions (#157) --- .config/config.toml | 6 ++- crates/television-derive/src/lib.rs | 80 ++--------------------------- 2 files changed, 10 insertions(+), 76 deletions(-) diff --git a/.config/config.toml b/.config/config.toml index 435a9e8..3f39595 100644 --- a/.config/config.toml +++ b/.config/config.toml @@ -163,6 +163,10 @@ toggle_preview = "ctrl-o" # shell history (according to your shell) "" = "zsh-history" +# environment variables +"export" = "env" +"unset" = "env" + # dirs channel "cd" = "dirs" "ls" = "dirs" @@ -184,4 +188,4 @@ toggle_preview = "ctrl-o" "docker run" = "docker-images" # gitrepos channel -"nvim" = "gitrepos" +"nvim" = "git-repos" diff --git a/crates/television-derive/src/lib.rs b/crates/television-derive/src/lib.rs index aedf8af..d80092f 100644 --- a/crates/television-derive/src/lib.rs +++ b/crates/television-derive/src/lib.rs @@ -67,43 +67,18 @@ fn impl_cli_channel(ast: &syn::DeriveInput) -> TokenStream { }) .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! { use clap::ValueEnum; use serde::{Deserialize, Serialize}; - use strum::Display; + use strum::{Display, EnumString}; 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 { #[default] #(#cli_enum_variants),* } - - impl std::convert::TryFrom<&str> for CliTvChannel { - type Error = String; - - fn try_from(channel: &str) -> Result { - match channel { - #( - #lowercase_match_arms - )* - _ => Err(format!("Invalid cli channel name: {}", channel)), - } - } - } }; // 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 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 { #( #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 { - 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! { #unit_enum #into_impl #from_impl - #try_from_str_impl - //#from_str_impl - #into_str_impl }; gen.into()