Crates.io | expect_macro_derive |
lib.rs | expect_macro_derive |
version | 0.0.0 |
source | src |
created_at | 2023-06-23 09:26:18.683899 |
updated_at | 2023-06-23 09:26:18.683899 |
description | This crate is used to generate a expected method for each variant of an enum. |
homepage | https://github.com/NightProg/expect_macro_derive.git |
repository | https://github.com/NightProg/expect_macro_derive.git |
max_upload_size | |
id | 898129 |
size | 11,970 |
This crate is used to generate a expected method for each variant of an enum.
this derive macro is used to generate a method for each variant of an enum.
he method will be named expect_{variant_name}
and will return a Option.
if it matches the pattern it will return Some with the fields of the variant.
otherwise it will return None.
#[panic]
if this attribute is present on a variant, the generated method will panic instead of returning None. Note: the enum need to implement Debug.
use expect_macro::Expect;
#[derive(Debug, Expect)]
enum Foo {
#[panic] Bar { a: i32, b: i32 },
Baz(i32, i32),
Qux,
}
fn main() {
let bar = Foo::Bar { a: 1, b: 2 };
let baz = Foo::Baz(1, 2);
let qux = Foo::Qux;
let (a, b) = bar.expect_bar(1, 2);
let opt: Option<(i32, i32)> = baz.expect_baz(1, 2);
assert_eq!(qux.expect_qux(), Some(()));
assert_eq!(a, 1);
assert_eq!(b, 2);
assert_eq!(opt, Some((1, 2)));
}
Licensed under either of MIT license or Apache License, Version 2.0 at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
You can contribute to this project in many forms: