Skip to main content

tonlib_tlb_derive/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(missing_docs)]
3#![deny(rust_2018_idioms)]
4
5//! Derive support for [`tonlib_core::tlb_types::tlb::TLB`].
6//!
7//! The macro is intentionally compatibility-first with the existing local `tlb-derive`
8//! surface used in TON projects built on `tonlib-core`.
9//!
10//! Crates that use `#[derive(TLB)]` must declare `tonlib-core` in their own
11//! `[dependencies]`, because the generated impls reference `tonlib-core`
12//! directly. This proc-macro crate only needs `tonlib-core` in
13//! `[dev-dependencies]` for its own test suite.
14
15mod attrs;
16mod codegen;
17mod crate_path;
18mod generics;
19
20use proc_macro::TokenStream;
21use syn::parse_macro_input;
22
23/// Derives [`tonlib_core::tlb_types::tlb::TLB`] for structs and enums.
24///
25/// Supported attributes:
26/// - `#[tlb(tag = "$001")]`
27/// - `#[tlb(tag = "#c0ffee")]`
28/// - `#[tlb(bit_len = 5)]`
29/// - `#[tlb(skip)]`
30/// - `#[tlb(skip_read)]`
31/// - `#[tlb(skip_write)]`
32/// - `#[tlb(default)]`
33/// - `#[tlb(default_with = "path::to::default")]`
34/// - `#[tlb(read_with = "path::to::reader", write_with = "path::to::writer")]`
35///
36/// The deriving crate must depend on `tonlib-core`.
37#[proc_macro_derive(TLB, attributes(tlb))]
38pub fn derive_tlb(input: TokenStream) -> TokenStream {
39    let input = parse_macro_input!(input as syn::DeriveInput);
40
41    match codegen::derive_tlb(input) {
42        Ok(tokens) => tokens.into(),
43        Err(error) => error.to_compile_error().into(),
44    }
45}