strict_encoding_derive/
lib.rs

1// Derivation macro library for strict encoding.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
6// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7//
8// Copyright (C) 2022-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
9//                         Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
10// Copyright (C) 2022-2025 Dr Maxim Orlovsky.
11// All rights under the above copyrights are reserved.
12//
13// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
14// in compliance with the License. You may obtain a copy of the License at
15//
16//        http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software distributed under the License
19// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
20// or implied. See the License for the specific language governing permissions and limitations under
21// the License.
22
23// Coding conventions
24#![recursion_limit = "256"]
25
26//! Derivation macros for strict encoding. To learn more about the strict
27//! encoding please check `strict_encoding` crate.
28//!
29//! # Derivation macros
30//!
31//! Library exports derivation macros `#[derive(`[`StrictEncode`]`)]`,
32//! `#[derive(`[`StrictDecode`]`)]`, which can be added on top of any structure
33//! you'd like to support string encoding (see Example section below).
34//!
35//! Encoding/decoding implemented by both of these macros may be configured at
36//! type and individual field level using `#[strict_type(...)]` attributes.
37//!
38//! # Attribute
39//!
40//! [`StrictEncode`] and [`StrictDecode`] behavior can be customized with
41//! `#[strict_encoding(...)]` attribute, which accepts different arguments
42//! depending to which part of the data type it is applied.
43//!
44//! ## Attribute arguments at type declaration level
45//!
46//! Derivation macros accept `#[strict_encoding()]` attribute with the following
47//! arguments:
48
49#[macro_use]
50extern crate quote;
51extern crate proc_macro;
52#[macro_use]
53extern crate syn;
54#[macro_use]
55extern crate amplify_syn;
56
57pub(crate) mod params;
58mod derive_dumb;
59mod derive_type;
60mod derive_encode;
61mod derive_decode;
62
63use proc_macro::TokenStream;
64use syn::DeriveInput;
65
66use crate::params::StrictDerive;
67
68/// Derives [`StrictDumb`] implementation for the type.
69#[proc_macro_derive(StrictDumb, attributes(strict_type))]
70pub fn derive_strict_dumb(input: TokenStream) -> TokenStream {
71    let derive_input = parse_macro_input!(input as DeriveInput);
72    StrictDerive::try_from(derive_input)
73        .and_then(|engine| engine.derive_dumb())
74        .unwrap_or_else(|e| e.to_compile_error())
75        .into()
76}
77
78/// Derives [`StrictType`] implementation for the type.
79#[proc_macro_derive(StrictType, attributes(strict_type))]
80pub fn derive_strict_type(input: TokenStream) -> TokenStream {
81    let derive_input = parse_macro_input!(input as DeriveInput);
82    StrictDerive::try_from(derive_input)
83        .and_then(|engine| engine.derive_type())
84        .unwrap_or_else(|e| e.to_compile_error())
85        .into()
86}
87
88/// Derives [`StrictEncode`] implementation for the type.
89#[proc_macro_derive(StrictEncode, attributes(strict_type))]
90pub fn derive_strict_encode(input: TokenStream) -> TokenStream {
91    let derive_input = parse_macro_input!(input as DeriveInput);
92    StrictDerive::try_from(derive_input)
93        .and_then(|engine| engine.derive_encode())
94        .unwrap_or_else(|e| e.to_compile_error())
95        .into()
96}
97
98/// Derives [`StrictDecode`] implementation for the type.
99#[proc_macro_derive(StrictDecode, attributes(strict_type))]
100pub fn derive_strict_decode(input: TokenStream) -> TokenStream {
101    let derive_input = parse_macro_input!(input as DeriveInput);
102    StrictDerive::try_from(derive_input)
103        .and_then(|engine| engine.derive_decode())
104        .unwrap_or_else(|e| e.to_compile_error())
105        .into()
106}