Skip to main content

ld_core_derive/
lib.rs

1//! Derive macros for the `ld-core` library. Not meant to be used directly:
2//! `ld-core` re-exports them behind its `derive` feature.
3use proc_macro::TokenStream;
4use proc_macro_error::{abort, proc_macro_error};
5use syn::DeriveInput;
6
7mod generate;
8pub(crate) mod utils;
9
10/// Derives the `LinkedData` serialization traits from `#[ld(...)]`
11/// attributes.
12///
13/// See the `ld-core` documentation for the attribute vocabulary.
14#[proc_macro_derive(Serialize, attributes(ld))]
15#[proc_macro_error]
16pub fn derive_serialize(item: TokenStream) -> TokenStream {
17	let input = syn::parse_macro_input!(item as DeriveInput);
18	let mut output = proc_macro2::TokenStream::new();
19
20	match generate::ser::subject(input) {
21		Ok(tokens) => output.extend(tokens),
22		Err(e) => {
23			abort!(e.span(), e)
24		}
25	}
26
27	output.into()
28}
29
30/// Derives the `FromLinkedData` deserialization traits from `#[ld(...)]`
31/// attributes.
32///
33/// See the `ld-core` documentation for the attribute vocabulary.
34#[proc_macro_derive(Deserialize, attributes(ld))]
35#[proc_macro_error]
36pub fn derive_deserialize(item: TokenStream) -> TokenStream {
37	let input = syn::parse_macro_input!(item as DeriveInput);
38	let mut output = proc_macro2::TokenStream::new();
39
40	match generate::de::subject(input) {
41		Ok(tokens) => output.extend(tokens),
42		Err(e) => {
43			abort!(e.span(), e)
44		}
45	}
46
47	output.into()
48}