plctag_derive/lib.rs
1// plctag-rs
2//
3// a rust wrapper of libplctag, with rust style APIs and useful extensions.
4// Copyright: 2022, Joylei <leingliu@gmail.com>
5// License: MIT
6
7/*!
8# plctag-derive
9
10macros for `plctag`
11
12[](https://crates.io/crates/plctag-derive)
13[](https://docs.rs/plctag-derive)
14[](https://github.com/joylei/plctag-rs/actions?query=workflow%3A%22build%22)
15[](https://github.com/joylei/plctag-rs/blob/master/LICENSE)
16
17## Usage
18
19please use it with [plctag](https://crates.io/crates/plctag)
20
21With this crate, the macros derive `plctag_core::Decode` and `plctag_core::Encode` for you automatically.
22
23### Examples
24
25```rust,no_run
26use plctag_core::{RawTag, Result, ValueExt};
27use plctag_derive::{Decode, Encode};
28
29#[derive(Debug, Default, Decode, Encode)]
30struct MyUDT {
31 #[tag(offset=0)]
32 a: u32,
33 #[tag(offset=4)]
34 b: u32,
35 #[tag(decode_fn="my_decode", encode_fn="my_encode")]
36 c: u32,
37 }
38
39fn my_decode(tag:&RawTag, offset: u32)->plctag::Result<u32> {
40 tag.get_u32(offset + 8).map(|v|v+1)
41}
42
43fn my_encode(v: &u32, tag: &RawTag, offset: u32)->plctag::Result<()> {
44 tag.set_u32(offset + 8, *v - 1)
45}
46
47let tag = RawTag::new("make=system&family=library&name=debug&debug=4", 100).unwrap();
48let res = tag.read(100);
49assert!(res.is_ok());
50let udt: MyUDT = tag.get_value(0).unwrap();
51assert_eq!(udt.a, 4);
52assert_eq!(udt.b, 0);
53
54```
55
56## License
57
58MIT
59
60*/
61#![warn(missing_docs)]
62
63extern crate proc_macro;
64
65mod decode_derive;
66mod encode_derive;
67mod shared;
68
69use proc_macro::TokenStream;
70use syn::DeriveInput;
71
72use syn::parse_macro_input;
73
74/// the macro derives `plctag_core::Decode` for you automatically.
75///
76/// ```rust,no_run
77/// use plctag_core::RawTag;
78/// use plctag_derive::{Decode, Encode};
79///
80/// #[derive(Debug, Default, Decode)]
81/// struct MyUDT {
82/// #[tag(offset=0)]
83/// a: u32,
84/// #[tag(offset=4)]
85/// b: u32,
86/// #[tag(decode_fn="my_decode")]
87/// c: u32,
88/// }
89///
90/// fn my_decode(tag:&RawTag, offset: u32)->plctag_core::Result<u32> {
91/// tag.get_u32(offset + 8).map(|v|v+1)
92/// }
93/// ```
94#[proc_macro_derive(Decode, attributes(tag))]
95pub fn decode_derive(input: TokenStream) -> TokenStream {
96 let input = parse_macro_input!(input as DeriveInput);
97 decode_derive::expand_tag_derive(input)
98 .unwrap_or_else(syn::Error::into_compile_error)
99 .into()
100}
101
102/// the macro derives `plctag_core::Encode` for you automatically.
103///
104/// ```rust,no_run
105/// use plctag_core::RawTag;
106/// use plctag_derive::{Decode, Encode};
107///
108/// #[derive(Debug, Default, Encode)]
109/// struct MyUDT {
110/// #[tag(offset=0)]
111/// a: u32,
112/// #[tag(offset=4)]
113/// b: u32,
114/// #[tag(encode_fn="my_encode")]
115/// c: u32,
116/// }
117///
118/// fn my_encode(v: &u32, tag: &RawTag, offset: u32)->plctag_core::Result<()> {
119/// tag.set_u32(offset + 8, *v - 1)
120/// }
121/// ```
122#[proc_macro_derive(Encode, attributes(tag))]
123pub fn encode_derive(input: TokenStream) -> TokenStream {
124 let input = parse_macro_input!(input as DeriveInput);
125 encode_derive::expand_tag_derive(input)
126 .unwrap_or_else(syn::Error::into_compile_error)
127 .into()
128}