https://github.com/roignpar/optfield
Macro that generates structs with optional fields from existing structs.
https://github.com/roignpar/optfield
rust
Last synced: 3 months ago
JSON representation
Macro that generates structs with optional fields from existing structs.
- Host: GitHub
- URL: https://github.com/roignpar/optfield
- Owner: roignpar
- License: apache-2.0
- Created: 2020-02-26T21:08:26.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-12-06T16:43:11.000Z (over 1 year ago)
- Last Synced: 2026-01-02T09:16:14.062Z (6 months ago)
- Topics: rust
- Language: Rust
- Homepage:
- Size: 110 KB
- Stars: 26
- Watchers: 1
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# optfield
[][crate]
[][documentation]
[][rustc]
[][ci]
`optfield` is a macro that, given a struct, generates another struct with
the same fields, but wrapped in `Option`.
__Minimum rustc version: `1.61.0`__
### Install
```
cargo add optfield
```
### Use
`optfield` takes the opt struct name as its first argument:
```rust
use optfield::optfield;
#[optfield(Opt)]
struct MyStruct {
text: String,
number: i32,
generic: T,
}
```
This will generate another struct that looks like:
```rust
struct Opt {
text: Option,
number: Option,
generic: Option,
}
```
`optfield` supports defining visibility, documentation, attributes and merge
methods. For more details and examples check its [documentation].
### License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE)
or [MIT](LICENSE-MIT) at your option.
[crate]: https://crates.io/crates/optfield
[documentation]: https://docs.rs/optfield
[rustc]: https://blog.rust-lang.org/2021/10/21/Rust-1.61.0.html
[ci]: https://github.com/roignpar/optfield/actions?query=workflow%3ACI