subset-struct #
Convenient and robust subset struct generation, for example for redacting a public API.
(This is work in progress. Nudge me about it if you're interested in using such a library.)
Installation #
Please use cargo-edit to always add the latest version of this library:
cargo add subset-struct
Example #
use serde::{Deserialize, Serialize};
use subset_struct::subset;
struct Extant {}
#[subset(
/// This documentation will appear on `Base`, as will the attribute.
#[derive(Deserialize)]
default,
/// Attributes, including documentation, added here will appear on `Filtered`, each conversion method and the `From` impl block.
{
// A `From` implementation is also generated,
// but a more specific method is easier to use flexibly.
// Use `&self` to clone each field instead.
/// Document specific conversion methods like this.
into_forwarded(self),
} ->
/// This will appear only on `Filtered`.
#[derive(Serialize)]
Filtered,
// Add a new lifetime (which will appear before any others)
// to generate a proxy structure instead.
// `mut` is optional.
{
// Conversions, including `From`, must happen from the matching reference.
as_proxy(&mut self),
} -> mut Proxy<'a>,
// Specifying custom conversion methods or additional attributes is optional:
/// This documentation will appear only on `Empty`, but not the `From` implementation.
Empty,
// You can use `ref` to skip generating the subset struct definition, which will only generate conversions.
// Use `ref mut` if needed.
// Additional attributes for the struct definition are not allowed here,
// but you can still specify some for the `From` impl if you write `{} -> ref Extant` instead and put them above that.
ref Extant,
)]
pub struct Base {
pub secret: String,
#[subset(
Filtered (
// You could specify set of subset-specific attributes here, as above.
),
Proxy,
)]
pub public: String,
}
License #
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution #
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Code of Conduct #
Changelog #
Versioning #
subset-struct strictly follows Semantic Versioning 2.0.0 with the following exceptions:
- The minor version will not reset to 0 on major version changes (except for v1).
Consider it the global feature level. - The patch version will not reset to 0 on major or minor version changes (except for v0.1 and v1).
Consider it the global patch level.
This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.
Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.