1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use darling::FromDeriveInput;
use proc_macro::TokenStream;
use std::iter::Iterator;
use syn::{Error, Path};
pub(crate) trait IteratorExt {
fn collect_error(self) -> Result<(), Error>
where
Self: Iterator<Item = Result<(), Error>> + Sized,
{
let accu = Ok(());
self.fold(accu, |accu, error| match (accu, error) {
(Ok(()), error) => error,
(accu, Ok(())) => accu,
(Err(mut err), Err(error)) => {
err.combine(error);
Err(err)
}
})
}
}
impl<I> IteratorExt for I where I: Iterator<Item = Result<(), Error>> + Sized {}
#[derive(FromDeriveInput, Debug)]
#[darling(attributes(serde_with))]
pub(crate) struct DeriveOptions {
#[darling(rename = "crate", default)]
pub(crate) alt_crate_path: Option<Path>,
}
impl DeriveOptions {
pub(crate) fn from_derive_input(input: &syn::DeriveInput) -> Result<Self, TokenStream> {
match <Self as FromDeriveInput>::from_derive_input(input) {
Ok(v) => Ok(v),
Err(e) => Err(TokenStream::from(e.write_errors())),
}
}
pub(crate) fn get_serde_with_path(&self) -> Path {
self.alt_crate_path
.clone()
.unwrap_or_else(|| syn::parse_str("::serde_with").unwrap())
}
}