Skip to content

Commit

Permalink
Merge pull request #50 from killercup/feature/from-lit
Browse files Browse the repository at this point in the history
Add a bunch of From impls for Lit
  • Loading branch information
dtolnay committed Oct 19, 2016
2 parents 79aaee6 + 36342c5 commit 391e1ab
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ impl<'a, T> FilterAttrs<'a> for T
pub mod parsing {
use super::*;
use ident::parsing::ident;
use lit::{Lit, StrStyle};
use lit::parsing::lit;
use space::{block_comment, whitespace};

Expand All @@ -111,10 +110,7 @@ pub mod parsing {
style: AttrStyle::Inner,
value: MetaItem::NameValue(
"doc".into(),
Lit::Str(
format!("//!{}", content),
StrStyle::Cooked,
),
format!("//!{}", content).into(),
),
is_sugared_doc: true,
})
Expand All @@ -128,7 +124,7 @@ pub mod parsing {
style: AttrStyle::Inner,
value: MetaItem::NameValue(
"doc".into(),
Lit::Str(com.to_owned(), StrStyle::Cooked),
com.into(),
),
is_sugared_doc: true,
})
Expand Down Expand Up @@ -156,10 +152,7 @@ pub mod parsing {
style: AttrStyle::Outer,
value: MetaItem::NameValue(
"doc".into(),
Lit::Str(
format!("///{}", content),
StrStyle::Cooked,
),
format!("///{}", content).into(),
),
is_sugared_doc: true,
})
Expand All @@ -173,7 +166,7 @@ pub mod parsing {
style: AttrStyle::Outer,
value: MetaItem::NameValue(
"doc".into(),
Lit::Str(com.to_owned(), StrStyle::Cooked),
com.into(),
),
is_sugared_doc: true,
})
Expand Down
75 changes: 75 additions & 0 deletions src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@ pub enum StrStyle {
Raw(usize),
}

impl From<String> for Lit {
fn from(input: String) -> Lit {
Lit::Str(input, StrStyle::Cooked)
}
}

impl<'a> From<&'a str> for Lit {
fn from(input: &str) -> Lit {
Lit::Str(input.into(), StrStyle::Cooked)
}
}

impl From<Vec<u8>> for Lit {
fn from(input: Vec<u8>) -> Lit {
Lit::ByteStr(input)
}
}

impl<'a> From<&'a [u8]> for Lit {
fn from(input: &[u8]) -> Lit {
Lit::ByteStr(input.into())
}
}

impl From<char> for Lit {
fn from(input: char) -> Lit {
Lit::Char(input)
}
}

impl From<bool> for Lit {
fn from(input: bool) -> Lit {
Lit::Bool(input)
}
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum IntTy {
Isize,
Expand All @@ -51,6 +87,45 @@ pub enum FloatTy {
Unsuffixed,
}

macro_rules! impl_from_for_lit {
(Int, [$($rust_type:ty => $syn_type:expr),+]) => {
$(
impl From<$rust_type> for Lit {
fn from(input: $rust_type) -> Lit {
Lit::Int(input as u64, $syn_type)
}
}
)+
};
(Float, [$($rust_type:ty => $syn_type:expr),+]) => {
$(
impl From<$rust_type> for Lit {
fn from(input: $rust_type) -> Lit {
Lit::Float(format!("{}", input), $syn_type)
}
}
)+
};
}

impl_from_for_lit! {Int, [
isize => IntTy::Isize,
i8 => IntTy::I8,
i16 => IntTy::I16,
i32 => IntTy::I32,
i64 => IntTy::I64,
usize => IntTy::Usize,
u8 => IntTy::U8,
u16 => IntTy::U16,
u32 => IntTy::U32,
u64 => IntTy::U64
]}

impl_from_for_lit! {Float, [
f32 => FloatTy::F32,
f64 => FloatTy::F64
]}

#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
Expand Down

0 comments on commit 391e1ab

Please sign in to comment.