-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from dtolnay/unraw
Add a type for representing raw-agnostic format vars
- Loading branch information
Showing
3 changed files
with
76 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ mod generics; | |
mod prop; | ||
mod scan_expr; | ||
mod span; | ||
mod unraw; | ||
mod valid; | ||
|
||
use proc_macro::TokenStream; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use proc_macro2::{Ident, Span, TokenStream}; | ||
use quote::ToTokens; | ||
use std::cmp::Ordering; | ||
use std::fmt::{self, Display}; | ||
use syn::ext::IdentExt as _; | ||
use syn::parse::{Parse, ParseStream, Result}; | ||
|
||
#[derive(Clone)] | ||
#[repr(transparent)] | ||
pub(crate) struct IdentUnraw(Ident); | ||
|
||
impl IdentUnraw { | ||
pub fn new(ident: Ident) -> Self { | ||
IdentUnraw(ident) | ||
} | ||
|
||
pub fn to_local(&self) -> Ident { | ||
let unraw = self.0.unraw(); | ||
let repr = unraw.to_string(); | ||
if syn::parse_str::<Ident>(&repr).is_err() { | ||
if let "_" | "super" | "self" | "Self" | "crate" = repr.as_str() { | ||
// Some identifiers are never allowed to appear as raw, like r#self and r#_. | ||
} else { | ||
return Ident::new_raw(&repr, Span::call_site()); | ||
} | ||
} | ||
unraw | ||
} | ||
} | ||
|
||
impl Display for IdentUnraw { | ||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
Display::fmt(&self.0.unraw(), formatter) | ||
} | ||
} | ||
|
||
impl Eq for IdentUnraw {} | ||
|
||
impl PartialEq for IdentUnraw { | ||
fn eq(&self, other: &Self) -> bool { | ||
PartialEq::eq(&self.0.unraw(), &other.0.unraw()) | ||
} | ||
} | ||
|
||
impl Ord for IdentUnraw { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
Ord::cmp(&self.0.unraw(), &other.0.unraw()) | ||
} | ||
} | ||
|
||
impl PartialOrd for IdentUnraw { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(Self::cmp(self, other)) | ||
} | ||
} | ||
|
||
impl Parse for IdentUnraw { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
input.call(Ident::parse_any).map(IdentUnraw::new) | ||
} | ||
} | ||
|
||
impl ToTokens for IdentUnraw { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
self.0.unraw().to_tokens(tokens); | ||
} | ||
} |