Skip to main content

ephemeris_forge/
operation.rs

1/// Define the enum that wraps the operations of each resource.
2///
3/// Each variant holds the operation type of one resource. The enum gets a
4/// `Display` that shows the inner operation and a `From` for each inner type.
5///
6/// ```
7/// #[derive(Clone, Debug, Eq, PartialEq)]
8/// pub struct Rename;
9///
10/// impl std::fmt::Display for Rename {
11///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12///         f.write_str("repository")
13///     }
14/// }
15///
16/// ephemeris_forge::operation_enum! {
17///     /// One write.
18///     pub enum Operation {
19///         /// A write to the repository.
20///         Repository(Rename),
21///     }
22/// }
23///
24/// assert_eq!(Operation::from(Rename).to_string(), "repository");
25/// ```
26#[macro_export]
27macro_rules! operation_enum {
28    (
29        $(#[$meta:meta])*
30        $vis:vis enum $name:ident {
31            $($(#[$variant_meta:meta])* $variant:ident($inner:ty),)+
32        }
33    ) => {
34        $(#[$meta])*
35        #[derive(Clone, Debug, Eq, PartialEq)]
36        $vis enum $name {
37            $($(#[$variant_meta])* $variant($inner),)+
38        }
39
40        impl ::core::fmt::Display for $name {
41            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
42                match self {
43                    $(Self::$variant(operation) => ::core::fmt::Display::fmt(operation, f),)+
44                }
45            }
46        }
47
48        $(
49            impl ::core::convert::From<$inner> for $name {
50                fn from(operation: $inner) -> Self {
51                    Self::$variant(operation)
52                }
53            }
54        )+
55    };
56}