Crates.io | codas-macros |
lib.rs | codas-macros |
version | 0.4.0 |
source | src |
created_at | 2024-10-30 21:56:27.37896 |
updated_at | 2024-10-30 21:56:27.37896 |
description | Macros for Codas. |
homepage | https://gitlab.com/alicorn/pub/alicorn |
repository | https://gitlab.com/alicorn/pub/alicorn |
max_upload_size | |
id | 1429282 |
size | 7,361 |
Macros for generating Rust types from Codas.
export_coda!
This macro parses a coda from a file path relative to the crate's workspace root path, and generating Rust data structures for the coda in-place.
Note: A crate's workspace root is always the top-most directory containing a
Cargo.toml
.
Here's an example that exports Rust data structures
for the greeter_coda.md
:
# use codas::codec::*;
# use codas_macros::export_coda;
// The file path should be relative to
// the _root_ of a crate's workspace.
export_coda!("codas-macros/tests/greeter_coda.md");
# fn main() {
// A struct is generated for each data type in the coda.
let request = Request { message: "Hi!".into() };
// An enum is generated with variants for each data type
// in the coda. The enum's name will be the same as the
// coda's name, with `Data` appended.
let data = GreeterData::from(request.clone());
assert_eq!("Hi!", match data.clone() {
GreeterData::Request(Request { message }) => message,
GreeterData::Response(..) |
GreeterData::Unspecified(..) => unimplemented!(),
});
// The structs and enums have auto-generated coda codecs.
let mut request_bytes = vec![];
request_bytes.write_data(&request).unwrap();
let mut data_bytes = vec![];
data_bytes.write_data(&data).unwrap();
assert_eq!(request_bytes, data_bytes);
// The enum can safely decode bytes containing
// coda-encoded data.
let data = GreeterData::decode_from(&mut data_bytes.as_slice()).unwrap();
assert_eq!("Hi!", match data {
GreeterData::Request(Request { message }) => message,
GreeterData::Response(..) |
GreeterData::Unspecified(..) => unimplemented!(),
});
# }
Copyright 2024 Alicorn Systems, Inc.
Licensed under the GNU Affero General Public License version 3, as published by the Free Software Foundation. Refer to the license file for more information.
If you have any questions, please reach out to [hello@alicorn.systems
].