📝 | Field enum generation for a struct [maintainer=@orzklv] https://crates.io/crates/maydon
Find a file
2025-02-17 19:37:43 +05:00
.github chore: proper readme for introduction 2025-02-17 05:37:47 +05:00
src feat: with test 2025-02-17 05:10:11 +05:00
tests feat: with test 2025-02-17 05:10:11 +05:00
.envrc init: starting new repo for a proc macro 2025-02-17 04:41:34 +05:00
.gitattributes chore: vendor out nix files 2025-02-17 19:37:43 +05:00
.gitignore Initial commit 2025-02-17 04:39:57 +05:00
Cargo.lock feat: with test 2025-02-17 05:10:11 +05:00
Cargo.toml feat: with test 2025-02-17 05:10:11 +05:00
default.nix init: starting new repo for a proc macro 2025-02-17 04:41:34 +05:00
flake.lock init: starting new repo for a proc macro 2025-02-17 04:41:34 +05:00
flake.nix init: starting new repo for a proc macro 2025-02-17 04:41:34 +05:00
LICENSE-APACHE chore: proper readme for introduction 2025-02-17 05:37:47 +05:00
LICENSE-MIT chore: proper readme for introduction 2025-02-17 05:37:47 +05:00
README.md chore: badge coloring fix 2025-02-17 05:40:48 +05:00
rust-toolchain.toml init: starting new repo for a proc macro 2025-02-17 04:41:34 +05:00
shell.nix init: starting new repo for a proc macro 2025-02-17 04:41:34 +05:00

Rust Uzbekistan's {Maydon}

Field enum generation for struct.

Telegram Chat Test CI

Motivation

Upon writing some rusty code for CLI config management, I simply declared a struct like this:

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    pub url: String,
    pub port: u16,
    pub database_url: String,
    pub threads: u16,
}

Also, in order to have set function with proper typed selection, I declared another enum named Field:

pub enum Field {
    Url,
    Port,
    Database,
    Unknown,
    Threads,
}

The thing is, I wanted to automate creation of enum generation. Ordinary macros wouldn't be a good viable solution, so I wrote maydon from ground up. I might be missing something or prequisites, but this is what I came up with:

use maydon::Maydon;

#[derive(Maydon)]
#[field_name = "Selector"]
pub struct Config {
    pub url: String,
    pub port: u16,
}

// The macro should generate:
fn main() {
    let _ = Selector::Url;
    let _ = Selector::Port;
    let _ = Selector::Unknown;
}

License

This project is licensed under the MIT or Apache-2.0 license - see the LICENSE-MIT or LICENSE-APACHE file for details.

Rust Uzbekistan's {Maydon}