test_output_directory/
macros.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/// Generates an enum value that supports an |all_variants| method that returns all
6/// possible values of the enum.
7#[macro_export]
8macro_rules! enumerable_enum {
9    ($(#[$meta:meta])* $name:ident {
10        $($(#[$variant_meta:meta])* $variant:ident),*,
11    }) => {
12        $(#[$meta])*
13        pub enum $name {
14            $($(#[$variant_meta])* $variant),*
15        }
16
17        impl $name {
18            pub fn all_variants() -> Vec<Self> {
19                vec![
20                    $($name::$variant),*
21                ]
22            }
23        }
24    }
25}
26
27#[cfg(test)]
28mod test {
29    #[test]
30    fn test_all_variants() {
31        enumerable_enum! {
32            #[derive(Debug, PartialEq)]
33            TestEnum {
34                VarOne,
35                VarTwo,
36            }
37        }
38
39        assert_eq!(TestEnum::all_variants(), vec![TestEnum::VarOne, TestEnum::VarTwo,]);
40    }
41}