This commit is contained in:
p0lunin 2020-06-24 19:14:31 +03:00
parent 9fa60a7596
commit 4ce3d96e20

View file

@ -26,7 +26,11 @@ pub fn impl_parse_args_unnamed(
variant: impl ToTokens,
parser_type: &ParserType,
) -> quote::__private::TokenStream {
let get_arguments = create_parser(parser_type, data.unnamed.iter().map(|f| &f.ty), data.unnamed.len());
let get_arguments = create_parser(
parser_type,
data.unnamed.iter().map(|f| &f.ty),
data.unnamed.len(),
);
let iter = 0..data.unnamed.len();
let mut initialization = quote! {};
for i in iter {
@ -46,7 +50,11 @@ pub fn impl_parse_args_named(
variant: impl ToTokens,
parser_type: &ParserType,
) -> quote::__private::TokenStream {
let get_arguments = create_parser(parser_type, data.named.iter().map(|f| &f.ty), data.named.len());
let get_arguments = create_parser(
parser_type,
data.named.iter().map(|f| &f.ty),
data.named.len(),
);
let i = 0..data.named.len();
let name = data.named.iter().map(|f| f.ident.as_ref().unwrap());
let res = quote! {
@ -58,7 +66,11 @@ pub fn impl_parse_args_named(
res
}
fn create_parser<'a>(parser_type: &ParserType, types: impl Iterator<Item = &'a Type>, count_args: usize) -> quote::__private::TokenStream {
fn create_parser<'a>(
parser_type: &ParserType,
types: impl Iterator<Item = &'a Type>,
count_args: usize,
) -> quote::__private::TokenStream {
let function_to_parse = match parser_type {
ParserType::Default => match count_args {
1 => {
@ -66,9 +78,11 @@ fn create_parser<'a>(parser_type: &ParserType, types: impl Iterator<Item = &'a T
}
_ => quote! { compile_error!("Expected 1 argument") },
},
ParserType::Split { separator } => {
parser_with_separator(&separator.clone().unwrap_or(" ".to_owned()), types, count_args)
}
ParserType::Split { separator } => parser_with_separator(
&separator.clone().unwrap_or(" ".to_owned()),
types,
count_args,
),
ParserType::Custom(s) => {
let ident = syn::Ident::new(&s, Span::call_site());
quote! { #ident }
@ -79,17 +93,20 @@ fn create_parser<'a>(parser_type: &ParserType, types: impl Iterator<Item = &'a T
}
}
fn parser_with_separator<'a>(separator: &str, types: impl Iterator<Item = &'a Type>, count_args: usize) -> quote::__private::TokenStream {
fn parser_with_separator<'a>(
separator: &str,
types: impl Iterator<Item = &'a Type>,
count_args: usize,
) -> quote::__private::TokenStream {
let inner = quote! { let mut splited = s.split(#separator); };
let i = 0..count_args;
let inner2 =
quote! {
#(#types::from_str(splited.next().ok_or(ParseError::TooFewArguments {
expected: #count_args,
found: #i,
message: format!("Expected but not found arg number {}", #i + 1),
})?).map_err(|e|ParseError::IncorrectFormat({ let e: Box<dyn std::error::Error> = e.into(); e }))?,)*
};
let inner2 = quote! {
#(#types::from_str(splited.next().ok_or(ParseError::TooFewArguments {
expected: #count_args,
found: #i,
message: format!("Expected but not found arg number {}", #i + 1),
})?).map_err(|e|ParseError::IncorrectFormat({ let e: Box<dyn std::error::Error> = e.into(); e }))?,)*
};
let res = quote! {
(|s: String| {
#inner