Skip to main content

twine_rs_macros/
lib.rs

1// Copyright (c) 2024 Jake Swensen
2// SPDX-License-Identifier: MPL-2.0
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8mod tlv;
9mod twine_shell;
10
11use proc_macro::TokenStream;
12use syn::parse_macro_input;
13
14/// Derive macro for implementing TLV encoding/decoding traits.
15///
16/// See the [`tlv`] module for the full attribute grammar.
17#[proc_macro_derive(Tlv, attributes(tlv))]
18pub fn derive_tlv(input: TokenStream) -> TokenStream {
19    let input = parse_macro_input!(input as syn::DeriveInput);
20    tlv::expand(&input).into()
21}
22
23/// Derive macro for implementing `TwineCtl` on shell-based interfaces.
24///
25/// Generates an `impl TwineCtl for T` block that delegates each trait method
26/// to the corresponding `shell_*` method provided by `TwineCtlShell`.
27///
28/// # Attributes
29///
30/// - `#[twine_shell(crate_path = "...")]` — Override the path used to reference the
31///   `twine_ctl` crate. Defaults to `::twine_ctl`. Set to `crate` when deriving from
32///   within the `twine-ctl` crate itself.
33///
34/// # Example
35///
36/// ```ignore
37/// // From an external crate (default):
38/// #[derive(TwineShell)]
39/// pub struct MyShell { /* ... */ }
40///
41/// // From within twine-ctl:
42/// #[derive(TwineShell)]
43/// #[twine_shell(crate_path = "crate")]
44/// pub struct TwineCtlSerialShell { /* ... */ }
45/// ```
46#[proc_macro_derive(TwineShell, attributes(twine_shell))]
47pub fn derive_twine_shell(input: TokenStream) -> TokenStream {
48    let input = parse_macro_input!(input as syn::DeriveInput);
49    twine_shell::expand(&input).into()
50}