~hutzdog/moretypes-rs

bab87ef6b2a2a87f172a1741b7f23d642add3bb6 — Hutzdog 2 years ago ceb0768
Add private field notation to records
2 files changed, 11 insertions(+), 4 deletions(-)

M README.md
M src/record.rs
M README.md => README.md +2 -3
@@ 10,12 10,11 @@ More types for your types!
This library is a successor to my `records` library, designed to make it more modular and comosable. Instead of one `record` attribute, `moretypes` provides several attributes that can be commposed to give objects certain properties. This is different from traits, as these properties are often implementations of multiple traits, constructors, behavioural changes, etc.

## Records
Records in MoreTypes are structs with all fields public (like `records::record`, but without most of the methods). They are useful for cases like configuration structs, where placing `pub` before every field is annoying boilerplate at best and logically erroneous at worst.
Records in MoreTypes are structs with all fields public (like `records::record`, but without most of the methods). They are useful for cases like configuration structs, where placing `pub` before every field is annoying boilerplate at best and logically erroneous at worst. Private fields preceeded by underscores remain private.
```rust
use moretypes::record;

#[record]
#[derive(Debug)]
pub struct Config {
    option1: String,
    option2: u32,


@@ 27,7 26,7 @@ pub fn main() {
        option2: 69,
    };

    println!("{cfg:?}");
    println!("option1 = {}, option2 = {}", cfg.option1, cfg.option2);
}
```


M src/record.rs => src/record.rs +9 -1
@@ 13,7 13,15 @@ fn pub_visibility() -> syn::Visibility {

fn make_all_pub<'a>(fields: impl IntoIterator<Item = &'a mut syn::Field>) {
    for field in fields {
        field.vis = pub_visibility();
        if matches!(field.vis, syn::Visibility::Inherited)
            && !field
                .ident
                .as_ref()
                .filter(|it| it.to_string().starts_with("_"))
                .is_some()
        {
            field.vis = pub_visibility();
        }
    }
}