Skip to content
This repository has been archived by the owner on Feb 5, 2020. It is now read-only.

Commit

Permalink
Update dependencies to syn/quote 1.0.*, other misc. fixes (#13)
Browse files Browse the repository at this point in the history
* fix: warning for `dyn` keyword usage

* fix: doctest compilation error, use `named_type` as a dev-dep in `name_type_derive`

* build(deps): upgrade `syn` 0.11.9 -> 1.0.11,  `quote` 0.3.15 -> 1.0.2; add `proc-macro2` 1.0.6; remove `named_type` (unnecessary)
  • Loading branch information
ErichDonGubler authored Feb 5, 2020
1 parent 2ef024f commit 90f95cd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 54 deletions.
3 changes: 2 additions & 1 deletion named_type/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ documentation = "https://docs.rs/named_type"
homepage = "https://github.com/cjhowe7/named_type"
repository = "https://github.com/cjhowe7/named_type"

[dependencies]
[dev-dependencies]
named_type_derive = { version = "0.2.1", path = "../named_type_derive" }
6 changes: 3 additions & 3 deletions named_type_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ repository = "https://github.com/cjhowe7/named_type"
proc-macro = true

[dependencies]
syn = "0.11.9"
quote = "0.3.15"
named_type = "0.1.4"
proc-macro2 = "1.0.6"
quote = "1.0.2"
syn = "1.0.11"
83 changes: 34 additions & 49 deletions named_type_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,73 @@
//! This crate provides support for deriving the `NamedType` trait from the
//! `named_type` crate. See that crate for further documentation.
#![crate_type = "proc-macro"]

extern crate proc_macro; // must use extern crate for proc macros
// see here: https://users.rust-lang.org/t/how-to-use-proc-macro-on-rust-2018/20833/2

use proc_macro::TokenStream;
use quote::{quote, quote_each_token, Tokens};
use syn::{Lit, MetaItem, NestedMetaItem};
use proc_macro2::Span;
use quote::quote;
use syn::{punctuated::Punctuated, Ident, Lit, Meta, NestedMeta};

#[doc(hidden)]
#[proc_macro_derive(NamedType, attributes(named_type))]
pub fn derive(input: TokenStream) -> TokenStream {
let input = input.to_string();

let ast = syn::parse_macro_input(&input).expect("Couldn't parse item");

let result = named_type_derive(ast).to_string();

result
.parse()
.expect(&format!("Couldn't parse `{}` to tokens", result))
named_type_derive(syn::parse_macro_input!(input))
}

fn find_prefix_suffix(props: &Vec<NestedMetaItem>) -> Option<(&str, &str)> {
fn find_prefix_suffix<P>(props: &Punctuated<NestedMeta, P>) -> Option<(String, String)> {
let prefix = props
.iter()
.find(|item| match item {
&&NestedMetaItem::MetaItem(MetaItem::NameValue(ref ident, _)) => {
ident == "short_prefix"
.find_map(|item| {
if let NestedMeta::Meta(Meta::NameValue(name_value)) = item {
if name_value.path.get_ident() == Some(&Ident::new("short_prefix", Span::call_site())) {
if let Lit::Str(s) = &name_value.lit {
return Some(s.value())
}
}
}
_ => false,
None
})
.and_then(|item| match item {
&NestedMetaItem::MetaItem(MetaItem::NameValue(_, ref value)) => match value {
&Lit::Str(ref string, _) => Some(string.as_ref()),
_ => None,
},
_ => None,
})
.unwrap_or("");
.unwrap_or_else(String::new);

let suffix = props
.iter()
.find(|item| match item {
&&NestedMetaItem::MetaItem(MetaItem::NameValue(ref ident, _)) => {
ident.to_string() == "short_suffix"
.find_map(|item| {
if let NestedMeta::Meta(Meta::NameValue(name_value)) = item {
if name_value.path.get_ident() == Some(&Ident::new("short_suffix", Span::call_site())) {
if let Lit::Str(s) = &name_value.lit {
return Some(s.value())
}
}
}
_ => false,
})
.and_then(|item| match item {
&NestedMetaItem::MetaItem(MetaItem::NameValue(_, ref value)) => match value {
&Lit::Str(ref string, _) => Some(string.as_ref()),
_ => None,
},
_ => None,
None
})
.unwrap_or("");
.unwrap_or_else(String::new);

Some((prefix, suffix))
}

fn named_type_derive(ast: syn::MacroInput) -> Tokens {
fn named_type_derive(ast: syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

let (prefix, suffix) = {
ast.attrs
.iter()
.find(|attr| match &attr.value {
&MetaItem::List(ref ident, _) => ident == "named_type",
_ => false,
})
.and_then(|attr| match &attr.value {
&MetaItem::List(_, ref props) => find_prefix_suffix(props),
_ => None,
.find_map(|attr| {
if let Meta::List(meta_list) = attr.parse_meta().unwrap() {
if meta_list.path.get_ident() == Some(&Ident::new("named_type", Span::call_site())) {
return find_prefix_suffix(&meta_list.nested);
}
}
None
})
.unwrap_or(("", ""))
.unwrap_or_else(|| (String::new(), String::new()))
};

let short_type_name: String = format!("{}{}{}", prefix, name, suffix);

quote! {
quote!(
impl #impl_generics NamedType for #name #ty_generics #where_clause {
fn type_name() -> &'static str {
concat!(module_path!(), "::", stringify!(#name))
Expand All @@ -92,5 +77,5 @@ fn named_type_derive(ast: syn::MacroInput) -> Tokens {
#short_type_name
}
}
}
).into()
}
2 changes: 1 addition & 1 deletion named_type_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn test_suffix() {

#[test]
fn test_ensure_that_structs_could_be_made_into_objects() {
let list_of_boxed_named_type: Vec<Box<NamedType>> = vec![
let list_of_boxed_named_type: Vec<Box<dyn NamedType>> = vec![
Box::new(MyStruct {}),
Box::new(MyEnum::V1),
Box::new(Prefixed::V1),
Expand Down

0 comments on commit 90f95cd

Please sign in to comment.