Crates.io | mdmodels-macro |
lib.rs | mdmodels-macro |
version | |
source | src |
created_at | 2024-07-01 12:32:24.90891 |
updated_at | 2024-12-28 16:48:16.65605 |
description | A procedural macro for generating models from a markdown data model |
homepage | |
repository | |
max_upload_size | |
id | 1288695 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This macro can be used to convert a markdown model using MD-Models into Rust structs and enums. The resulting Rust code can be used to serialize and deserialize the model and integrate it into your Rust project.
cargo install mdmodels-macro
Suppose you have a markdown file model.md
with the following content:
# Test
### Object
- string_value
- Type: string
- enum_value
- Type: SomeEnum
### SomeEnum
```python
VALUE = value
ANOTHER = another
```
You can convert this markdown file into Rust code using the following command:
use mdmodels_macro::parse_mdmodel;
// if you want to use the macro in the current module
parse_mdmodel!("tests/data/model.md");
// or if you want to use the macro in a module (e.g. in lib.rs)
mod my_module {
parse_mdmodel!("tests/data/model.md");
}
At this point, the macro will generate the corresponding structs and enums in Rust code, which will be available as a module. The module name is derived from the title (# Test
) as snake case, if present. Otherwise the module name will be model
.
You can then use the module in your code:
fn main () {
let obj = Object {
string_value: "Hello, World!".to_string(),
enum_value: model::SomeEnum::VALUE,
};
// Serialize the object
let serialized = serde_json::to_string(&obj).unwrap();
println!("Serialized: \n{}\n", serialized);
// Deserialize the object
let deserialized: Object = serde_json::from_str(&serialized).unwrap();
println!("Deserialized: \n{:#?}\n", deserialized);
}
This macro also supports the builder pattern. To use the builder pattern, you need to use the Builder
struct of the object in the markdown file:
fn main () -> Result<(), Box<dyn std::error::Error>> {
let obj = ObjectBuilder::new()
.string_value("Hello, World!")
.enum_value(model::SomeEnum::VALUE)
.build()?;
// Serialize the object
let serialized = serde_json::to_string(&obj).unwrap();
println!("Serialized: \n{}\n", serialized);
// Deserialize the object
let deserialized: Object = serde_json::from_str(&serialized).unwrap();
println!("Deserialized: \n{:#?}\n", deserialized);
Ok(())
}