Skip to content

Commit

Permalink
refactor(derive): Make it easier to work with 'Name'
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 25, 2024
1 parent be73195 commit 8eab48f
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions clap_derive/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,8 @@ impl Item {
quote!( #(#doc_comment)* #(#methods)* )
}

pub fn group_id(&self) -> TokenStream {
self.group_id.clone().raw()
pub fn group_id(&self) -> &Name {
&self.group_id
}

pub fn group_methods(&self) -> TokenStream {
Expand All @@ -998,8 +998,8 @@ impl Item {
quote!( #(#next_help_heading)* )
}

pub fn id(&self) -> TokenStream {
self.name.clone().raw()
pub fn id(&self) -> &Name {
&self.name
}

pub fn cased_name(&self) -> TokenStream {
Expand Down Expand Up @@ -1410,16 +1410,6 @@ pub enum Name {
}

impl Name {
pub fn raw(self) -> TokenStream {
match self {
Name::Assigned(tokens) => tokens,
Name::Derived(ident) => {
let s = ident.unraw().to_string();
quote_spanned!(ident.span()=> #s)
}
}
}

pub fn translate(self, style: CasingStyle) -> TokenStream {
use CasingStyle::*;

Expand Down Expand Up @@ -1466,3 +1456,15 @@ impl Name {
}
}
}

impl ToTokens for Name {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Name::Assigned(t) => t.to_tokens(tokens),
Name::Derived(ident) => {
let s = ident.unraw().to_string();
quote_spanned!(ident.span()=> #s).to_tokens(tokens)
}
}
}
}

0 comments on commit 8eab48f

Please sign in to comment.