From 3da357788ba9a1a5d3c3320c92d5ddd6e767f434 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 15 Apr 2021 23:45:57 +0300 Subject: [PATCH] refac: move helper functions to utils --- itconfig-macro/src/expand.rs | 37 +----------------------------------- itconfig-macro/src/lib.rs | 2 ++ itconfig-macro/src/utils.rs | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 itconfig-macro/src/utils.rs diff --git a/itconfig-macro/src/expand.rs b/itconfig-macro/src/expand.rs index 99db9dc..50196c3 100644 --- a/itconfig-macro/src/expand.rs +++ b/itconfig-macro/src/expand.rs @@ -1,15 +1,7 @@ use crate::ast::*; +use crate::utils::{is_option_type, vec_to_token_stream_2}; use proc_macro2::TokenStream as TokenStream2; use quote::{quote, ToTokens, TokenStreamExt}; -use syn::Path; -use syn::Type; - -fn vec_to_token_stream_2(input: &Vec) -> Vec -where - T: ToTokens, -{ - input.iter().map(|ns| ns.into_token_stream()).collect() -} impl ToTokens for RootNamespace { fn to_tokens(&self, tokens: &mut TokenStream2) { @@ -174,30 +166,3 @@ impl ToTokens for Variable { } } } - -fn path_ident(path: &Path) -> String { - path.segments - .iter() - .into_iter() - .fold(String::with_capacity(250), |mut acc, v| { - acc.push_str(&v.ident.to_string()); - acc.push('|'); - acc - }) -} - -fn is_option_path_ident(path_ident: String) -> bool { - vec!["Option|", "std|option|Option|", "core|option|Option|"] - .into_iter() - .find(|s| &path_ident == *s) - .is_some() -} - -fn is_option_type(ty: &Type) -> bool { - match ty { - Type::Path(ty_path) => { - ty_path.qself.is_none() && is_option_path_ident(path_ident(&ty_path.path)) - } - _ => false, - } -} diff --git a/itconfig-macro/src/lib.rs b/itconfig-macro/src/lib.rs index 09040da..8c4aa63 100644 --- a/itconfig-macro/src/lib.rs +++ b/itconfig-macro/src/lib.rs @@ -4,9 +4,11 @@ mod ast; mod expand; mod parse; +mod utils; extern crate proc_macro; extern crate proc_macro2; + use self::proc_macro::TokenStream; use ast::RootNamespace; use quote::ToTokens; diff --git a/itconfig-macro/src/utils.rs b/itconfig-macro/src/utils.rs new file mode 100644 index 0000000..f909fbc --- /dev/null +++ b/itconfig-macro/src/utils.rs @@ -0,0 +1,37 @@ +use proc_macro2::TokenStream as TokenStream2; +use quote::ToTokens; +use syn::{Path, Type}; + +pub fn vec_to_token_stream_2(input: &Vec) -> Vec +where + T: ToTokens, +{ + input.iter().map(|ns| ns.into_token_stream()).collect() +} + +fn path_ident(path: &Path) -> String { + path.segments + .iter() + .into_iter() + .fold(String::with_capacity(250), |mut acc, v| { + acc.push_str(&v.ident.to_string()); + acc.push('|'); + acc + }) +} + +fn is_option_path_ident(path_ident: String) -> bool { + vec!["Option|", "std|option|Option|", "core|option|Option|"] + .into_iter() + .find(|s| &path_ident == *s) + .is_some() +} + +pub fn is_option_type(ty: &Type) -> bool { + match ty { + Type::Path(ty_path) => { + ty_path.qself.is_none() && is_option_path_ident(path_ident(&ty_path.path)) + } + _ => false, + } +}