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