commit_encoding_derive/lib.rs
1// Client-side-validation foundation libraries.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
6// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2024 LNP/BP Standards Association, Switzerland.
9// Copyright (C) 2024-2025 LNP/BP Laboratories,
10// Institute for Distributed and Cognitive Systems
11// (InDCS), Switzerland. Copyright (C) 2019-2025 Dr Maxim Orlovsky.
12// All rights under the above copyrights are reserved.
13//
14// Licensed under the Apache License, Version 2.0 (the "License"); you may not
15// use this file except in compliance with the License. You may obtain a copy of
16// the License at
17//
18// http://www.apache.org/licenses/LICENSE-2.0
19//
20// Unless required by applicable law or agreed to in writing, software
21// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
23// License for the specific language governing permissions and limitations under
24// the License.
25
26// Coding conventions
27#![recursion_limit = "256"]
28
29//! Derivation macros for commit encoding. To learn more about the strict
30//! commit please check `commit_verify` crate.
31//!
32//! # Derivation macros
33//!
34//! Library exports derivation macros `#[derive(`[`CommitEncode`]`)]`,
35//! which can be added on top of any structure you'd like to support commitment
36//! encoding.
37//!
38//! Encoding/decoding implemented by both of these macros may be configured at
39//! type and individual field level using `#[commit_encode(...)]` attributes.
40//!
41//! # Attribute
42//!
43//! [`CommitEncode`] behavior can be customized with `#[commit_encoding(...)]`
44//! attribute, which accepts different arguments depending to which part of the
45//! data type it is applied.
46//!
47//! ## Attribute arguments at type declaration level
48//!
49//! Derivation macros accept `#[commit_encoding()]` attribute with the following
50//! arguments:
51
52#[macro_use]
53extern crate quote;
54extern crate proc_macro;
55#[macro_use]
56extern crate syn;
57#[macro_use]
58extern crate amplify;
59#[macro_use]
60extern crate amplify_syn;
61
62pub(crate) mod params;
63mod derive;
64
65use proc_macro::TokenStream;
66use syn::DeriveInput;
67
68use crate::params::CommitDerive;
69
70/// Derives [`CommitEncode`] implementation for the type.
71#[proc_macro_derive(CommitEncode, attributes(commit_encode))]
72pub fn derive_commit_encode(input: TokenStream) -> TokenStream {
73 let derive_input = parse_macro_input!(input as DeriveInput);
74 CommitDerive::try_from(derive_input)
75 .and_then(|engine| engine.derive_encode())
76 .unwrap_or_else(|e| e.to_compile_error())
77 .into()
78}