From 79e02b099985ab5427d7493384be1241cf1918b2 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 29 Sep 2018 03:23:48 -0700 Subject: [PATCH] Tightened code some in codegen --- schala-codegen/src/lib.rs | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/schala-codegen/src/lib.rs b/schala-codegen/src/lib.rs index 3ef4430..108aba5 100644 --- a/schala-codegen/src/lib.rs +++ b/schala-codegen/src/lib.rs @@ -10,15 +10,19 @@ extern crate schala_repl; use proc_macro::TokenStream; use syn::{Ident, Attribute, DeriveInput}; +fn find_attr_by_name<'a>(name: &str, attrs: &'a Vec) -> 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) -> Option { use syn::{Meta, Lit, MetaNameValue}; - attrs.iter().map(|attr| attr.interpret_meta()).find(|meta| { - match meta { - &Some(Meta::NameValue(MetaNameValue { ref ident, .. })) if ident == name => true, - _ => false - } - }).and_then(|meta| { - match meta { + find_attr_by_name(name, attrs) + .and_then(|attr| { + match attr.interpret_meta() { Some(Meta::NameValue(MetaNameValue { lit: Lit::Str(litstr), .. })) => Some(litstr.value()), _ => None, } @@ -27,12 +31,8 @@ fn extract_attribute_arg_by_name(name: &str, attrs: &Vec) -> Option) -> Option>)>> { use syn::{Meta, MetaList, NestedMeta}; - attrs.iter().find(|attr| { - match attr.path.segments.iter().nth(0) { - Some(segment) if segment.ident == name => true, - _ => false - } - }).and_then(|attr| { + find_attr_by_name(name, attrs) + .and_then(|attr| { match attr.interpret_meta() { Some(Meta::List(MetaList { nested, .. })) => { Some(nested.iter().map(|nested_meta| match nested_meta { @@ -52,14 +52,6 @@ fn extract_attribute_list(name: &str, attrs: &Vec) -> Option(name: &str, attrs: &'a Vec) -> 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))] pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream {