palette_derive/
lib.rs

1//! Derives traits from the [palette](https://crates.io/crates/palette) crate.
2
3use proc_macro::TokenStream;
4
5macro_rules! syn_try {
6    ($e:expr) => {
7        match $e {
8            Ok(value) => value,
9            Err(errors) => {
10                trait IntoErrors {
11                    fn into_errors(self) -> Vec<::syn::parse::Error>;
12                }
13                impl IntoErrors for Vec<::syn::parse::Error> {
14                    fn into_errors(self) -> Vec<::syn::parse::Error> {
15                        self
16                    }
17                }
18                impl IntoErrors for ::syn::parse::Error {
19                    fn into_errors(self) -> Vec<::syn::parse::Error> {
20                        vec![self]
21                    }
22                }
23
24                let errors: ::proc_macro2::TokenStream = IntoErrors::into_errors(errors)
25                    .iter()
26                    .map(::syn::parse::Error::to_compile_error)
27                    .collect();
28                return ::proc_macro::TokenStream::from(errors);
29            }
30        }
31    };
32}
33
34mod alpha;
35mod cast;
36mod color_types;
37mod convert;
38mod meta;
39mod util;
40
41#[proc_macro_derive(WithAlpha, attributes(palette))]
42pub fn derive_with_alpha(tokens: TokenStream) -> TokenStream {
43    syn_try!(alpha::derive_with_alpha(tokens))
44}
45
46#[proc_macro_derive(FromColorUnclamped, attributes(palette))]
47pub fn derive_from_color_unclamped(tokens: TokenStream) -> TokenStream {
48    syn_try!(convert::derive_from_color_unclamped(tokens))
49}
50
51#[proc_macro_derive(ArrayCast, attributes(palette))]
52pub fn derive_array_cast(tokens: TokenStream) -> TokenStream {
53    syn_try!(cast::derive_array_cast(tokens))
54}