tonlib-tlb-derive 0.1.0

Derive macro for tonlib-core TLB types
Documentation
#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![deny(rust_2018_idioms)]

//! Derive support for [`tonlib_core::tlb_types::tlb::TLB`].
//!
//! The macro is intentionally compatibility-first with the existing local `tlb-derive`
//! surface used in TON projects built on `tonlib-core`.
//!
//! Crates that use `#[derive(TLB)]` must declare `tonlib-core` in their own
//! `[dependencies]`, because the generated impls reference `tonlib-core`
//! directly. This proc-macro crate only needs `tonlib-core` in
//! `[dev-dependencies]` for its own test suite.

mod attrs;
mod codegen;
mod crate_path;
mod generics;

use proc_macro::TokenStream;
use syn::parse_macro_input;

/// Derives [`tonlib_core::tlb_types::tlb::TLB`] for structs and enums.
///
/// Supported attributes:
/// - `#[tlb(tag = "$001")]`
/// - `#[tlb(tag = "#c0ffee")]`
/// - `#[tlb(bit_len = 5)]`
/// - `#[tlb(skip)]`
/// - `#[tlb(skip_read)]`
/// - `#[tlb(skip_write)]`
/// - `#[tlb(default)]`
/// - `#[tlb(default_with = "path::to::default")]`
/// - `#[tlb(read_with = "path::to::reader", write_with = "path::to::writer")]`
///
/// The deriving crate must depend on `tonlib-core`.
#[proc_macro_derive(TLB, attributes(tlb))]
pub fn derive_tlb(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as syn::DeriveInput);

    match codegen::derive_tlb(input) {
        Ok(tokens) => tokens.into(),
        Err(error) => error.to_compile_error().into(),
    }
}