Skip to content

Commit e67561a

Browse files
committed
Document the use of the pdl attribute with examples and tests
1 parent a448477 commit e67561a

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

doc/rust-generated-code-guide.rst

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,23 @@ Rust Generated Code Guide
44
Usage
55
-----
66

7-
Example invocation:
7+
The crate `pdl_derive` lets developers embed their grammar in a source module
8+
using the `pdl` proc_macro attribute. Example usage:
9+
10+
.. sourcecode:: rust
11+
12+
use pdl_derive::pdl
13+
14+
#[pdl("my-protocol.pdl")]
15+
mod my_protocol {
16+
}
17+
18+
The `pdl` proc_macro attribute must be attached to a module declaration.
19+
`pdl` preserves the original name, attributes, and items of the associated
20+
module.
21+
22+
The backend can also be pre-generated from the `pdlc` tool,
23+
and compiled as source. Example invocation:
824

925
.. sourcecode:: bash
1026

pdl-compiler/examples/jpeg.pdl examples/jpeg.pdl

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ enum MarkerType : 8 {
6161

6262
struct Marker {
6363
_fixed_ = 0xff : 8,
64-
type: MarkerType,
64+
type : MarkerType,
6565
_payload_,
6666
}
6767

6868
struct Segment: Marker {
69-
_size_(_payload_): 16,
70-
_payload_ [+2],
69+
_size_(_payload_) : 16,
70+
_payload_ : [+2],
7171
}
7272

7373
struct StartOfImage : Marker(type = SOI) {}
File renamed without changes.

pdl-derive/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ proc-macro2 = "1.0.66"
2626
quote = "1.0.33"
2727
syn = {version = "2.0.29", features = ["full"]}
2828
termcolor = "1.2.0"
29+
30+
[dev-dependencies]
31+
bytes = "1.4.0"
32+
thiserror = "1.0.47"

pdl-derive/tests/examples.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! PDL tests.
16+
17+
use pdl_derive::pdl;
18+
19+
#[test]
20+
fn test_pcap() {
21+
#[pdl("../examples/pcap.pdl")]
22+
mod pcap {}
23+
24+
use pcap::*;
25+
let pcap_file = PcapFileBuilder {
26+
header: PcapHeader {
27+
version_major: 1,
28+
version_minor: 0,
29+
thiszone: 0,
30+
sigfigs: 0,
31+
snaplen: 512,
32+
network: 42,
33+
},
34+
records: vec![PcapRecord {
35+
ts_sec: 0xdead,
36+
ts_usec: 0xbeef,
37+
orig_len: 1024,
38+
payload: vec![1, 2, 3],
39+
}],
40+
}
41+
.build();
42+
43+
assert!(PcapFile::parse(&pcap_file.to_vec()).is_ok());
44+
}
45+
46+
#[test]
47+
#[cfg(disabled)]
48+
fn test_jpeg() {
49+
// The JPEG syntax depends on struct inheritance which is currently
50+
// not supported. https://github.com/google/pdl/issues/62
51+
#[pdl("../examples/jpeg.pdl")]
52+
mod jpeg {}
53+
}

0 commit comments

Comments
 (0)