diff --git a/Cargo.toml b/Cargo.toml index 6019aee..76b6890 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "quote" -version = "0.3.8" # don't forget to update version in readme +version = "0.3.9" # don't forget to update version in readme authors = ["David Tolnay "] license = "MIT/Apache-2.0" description = "Quasi-quoting macro quote!(...)" diff --git a/README.md b/README.md index f354152..2ecb01d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Quasi-quoting without a Syntex dependency, intended for use with [Macros ```toml [dependencies] -quote = "0.3" +quote = "0.3.9" ``` ```rust diff --git a/src/ident.rs b/src/ident.rs new file mode 100644 index 0000000..3d6393b --- /dev/null +++ b/src/ident.rs @@ -0,0 +1,56 @@ +use {Tokens, ToTokens}; +use std::borrow::Cow; +use std::fmt; + +#[derive(Debug, Clone, Eq, Hash)] +pub struct Ident(String); + +impl Ident { + pub fn new>(t: T) -> Self { + t.into() + } +} + +impl<'a> From<&'a str> for Ident { + fn from(s: &str) -> Self { + Ident(s.to_owned()) + } +} + +impl<'a> From> for Ident { + fn from(s: Cow<'a, str>) -> Self { + Ident(s.into_owned()) + } +} + +impl From for Ident { + fn from(s: String) -> Self { + Ident(s) + } +} + +impl AsRef for Ident { + fn as_ref(&self) -> &str { + &self.0 + } +} + +impl fmt::Display for Ident { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + self.0.fmt(formatter) + } +} + +impl PartialEq for Ident + where T: AsRef +{ + fn eq(&self, other: &T) -> bool { + self.0 == other.as_ref() + } +} + +impl ToTokens for Ident { + fn to_tokens(&self, tokens: &mut Tokens) { + tokens.append(self.as_ref()) + } +} diff --git a/src/lib.rs b/src/lib.rs index ead094a..d2e55b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,6 +64,9 @@ pub use tokens::Tokens; mod to_tokens; pub use to_tokens::{ToTokens, ByteStr}; +mod ident; +pub use ident::Ident; + /// The whole point. #[macro_export] macro_rules! quote { diff --git a/tests/test.rs b/tests/test.rs index 7774d4d..adaa785 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -192,6 +192,15 @@ fn test_byte_str() { assert_eq!(expected, tokens.to_string()); } +#[test] +fn test_ident() { + let foo = quote::Ident::from("Foo"); + let bar = quote::Ident::from(format!("Bar{}", 7)); + let tokens = quote!(struct #foo; enum #bar {}); + let expected = "struct Foo ; enum Bar7 { }"; + assert_eq!(expected, tokens.to_string()); +} + #[test] fn test_duplicate() { let ch = 'x';