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 syn::DeriveInput;
5
6mod generate;
7pub(crate) mod utils;
8
9/// Derives the `LinkedData` serialization traits from `#[ld(...)]`
10/// attributes.
11///
12/// See the `ld-core` documentation for the attribute vocabulary.
13#[proc_macro_derive(Serialize, attributes(ld))]
14pub fn derive_serialize(item: TokenStream) -> TokenStream {
15	let input = syn::parse_macro_input!(item as DeriveInput);
16	let mut output = proc_macro2::TokenStream::new();
17
18	match generate::ser::subject(input) {
19		Ok(tokens) => output.extend(tokens),
20		Err(e) => output.extend(syn::Error::from(e).to_compile_error()),
21	}
22
23	output.into()
24}
25
26/// Derives the `FromLinkedData` deserialization traits from `#[ld(...)]`
27/// attributes.
28///
29/// See the `ld-core` documentation for the attribute vocabulary.
30#[proc_macro_derive(Deserialize, attributes(ld))]
31pub fn derive_deserialize(item: TokenStream) -> TokenStream {
32	let input = syn::parse_macro_input!(item as DeriveInput);
33	let mut output = proc_macro2::TokenStream::new();
34
35	match generate::de::subject(input) {
36		Ok(tokens) => output.extend(tokens),
37		Err(e) => output.extend(syn::Error::from(e).to_compile_error()),
38	}
39
40	output.into()
41}