Tightened code some in codegen

This commit is contained in:
greg 2018-09-29 03:23:48 -07:00
parent 80eb703f5e
commit 79e02b0999

View File

@ -10,15 +10,19 @@ extern crate schala_repl;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use syn::{Ident, Attribute, DeriveInput}; use syn::{Ident, Attribute, DeriveInput};
fn find_attr_by_name<'a>(name: &str, attrs: &'a Vec<Attribute>) -> Option<&'a Attribute> {
attrs.iter().find(|attr| {
let first = attr.path.segments.first();
let seg: Option<&&syn::PathSegment> = first.as_ref().map(|x| x.value());
seg.map(|seg| seg.ident.to_string() == name).unwrap_or(false)
})
}
fn extract_attribute_arg_by_name(name: &str, attrs: &Vec<Attribute>) -> Option<String> { fn extract_attribute_arg_by_name(name: &str, attrs: &Vec<Attribute>) -> Option<String> {
use syn::{Meta, Lit, MetaNameValue}; use syn::{Meta, Lit, MetaNameValue};
attrs.iter().map(|attr| attr.interpret_meta()).find(|meta| { find_attr_by_name(name, attrs)
match meta { .and_then(|attr| {
&Some(Meta::NameValue(MetaNameValue { ref ident, .. })) if ident == name => true, match attr.interpret_meta() {
_ => false
}
}).and_then(|meta| {
match meta {
Some(Meta::NameValue(MetaNameValue { lit: Lit::Str(litstr), .. })) => Some(litstr.value()), Some(Meta::NameValue(MetaNameValue { lit: Lit::Str(litstr), .. })) => Some(litstr.value()),
_ => None, _ => None,
} }
@ -27,12 +31,8 @@ fn extract_attribute_arg_by_name(name: &str, attrs: &Vec<Attribute>) -> Option<S
fn extract_attribute_list(name: &str, attrs: &Vec<Attribute>) -> Option<Vec<(Ident, Option<Vec<Ident>>)>> { fn extract_attribute_list(name: &str, attrs: &Vec<Attribute>) -> Option<Vec<(Ident, Option<Vec<Ident>>)>> {
use syn::{Meta, MetaList, NestedMeta}; use syn::{Meta, MetaList, NestedMeta};
attrs.iter().find(|attr| { find_attr_by_name(name, attrs)
match attr.path.segments.iter().nth(0) { .and_then(|attr| {
Some(segment) if segment.ident == name => true,
_ => false
}
}).and_then(|attr| {
match attr.interpret_meta() { match attr.interpret_meta() {
Some(Meta::List(MetaList { nested, .. })) => { Some(Meta::List(MetaList { nested, .. })) => {
Some(nested.iter().map(|nested_meta| match nested_meta { Some(nested.iter().map(|nested_meta| match nested_meta {
@ -52,14 +52,6 @@ fn extract_attribute_list(name: &str, attrs: &Vec<Attribute>) -> Option<Vec<(Ide
}) })
} }
fn find_attr_by_name<'a>(name: &str, attrs: &'a Vec<Attribute>) -> Option<&'a Attribute> {
attrs.iter().find(|attr| {
let first = attr.path.segments.first();
let seg: Option<&&syn::PathSegment> = first.as_ref().map(|x| x.value());
seg.map(|seg| seg.ident.to_string() == name).unwrap_or(false)
})
}
#[proc_macro_derive(ProgrammingLanguageInterface, attributes(LanguageName, SourceFileExtension, PipelineSteps, DocMethod))] #[proc_macro_derive(ProgrammingLanguageInterface, attributes(LanguageName, SourceFileExtension, PipelineSteps, DocMethod))]
pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream { pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream {