opt_args

Crates.ioopt_args
lib.rsopt_args
version2.0.0
sourcesrc
created_at2022-03-18 17:55:38.491747
updated_at2024-03-15 16:40:18.336723
descriptionCreate macros for functions and structs with default values
homepage
repositoryhttps://github.com/Princic-1837592/opt_args
max_upload_size
id552815
size31,171
Andrea Princic (Princic-1837592)

documentation

README

opt_args: Optional arguments for functions and structs in Rust

Crates.io Crates.io docs.rs

This crate allows you to auto-generate macros to call functions and instantiate structs with default named arguments

Import the macro and use it on a function or struct like this

use opt_args::opt_args;

opt_args! {
    fn function(a: i32, b: &str = "default", c: (u8,)?) -> (i32, &str, (u8,)) {
        (a, b, c)
    }
}

opt_args! {
    #[derive(Debug, PartialEq, Eq)]
    struct Struct {
        x: i32,
        y: i32 = 1,
        z: i32?,
        other: Option<Box<Self>>?,
    }
}

To auto-generate macros that can be used like this

fn main() {
    assert_eq!(
        function!(1, b = "not the default"),
        (1, "not the default", (0,))
    );
    assert_eq!(
        Struct!(4, z = 5),
        Struct {
            x: 4,
            y: 1,
            z: 5,
            other: None
        }
    );
}

Full documentation here

Commit count: 12

cargo fmt