commit_encoding_derive/
lib.rs

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