sdmmc_core/macros/
response.rs

1/// Convenience macro to define response types.
2#[macro_export]
3macro_rules! response {
4    (
5        $ty:ident {
6            response_mode: $mode:ident,
7        }
8    ) => {
9        ::paste::paste! {
10            impl $ty {
11                #[doc = "Gets the response type for the [" $ty "]."]
12                #[inline]
13                pub const fn response_type(&self) -> $crate::response::ResponseType {
14                    $crate::response::ResponseType::$ty
15                }
16
17                #[doc = "Gets the response mode for the [" $ty "]."]
18                #[inline]
19                pub const fn response_mode(&self) -> $crate::response::ResponseMode {
20                    $crate::response::ResponseMode::$mode
21                }
22            }
23        }
24    };
25}
26
27/// Convenience macro to define a response type wrapper enum.
28#[macro_export]
29macro_rules! response_enum {
30    (
31        $(#[$doc:meta])*
32        $ty:ident {
33            default: $default:ident($default_mod:ident :: $default_ty:ident),
34            $(
35                $(#[$var_doc:meta])*
36                $var:ident ( $var_mod:ident :: $var_ty:ident ),
37            )+
38        }
39    ) => {
40        ::paste::paste! {
41            $(#[$doc])*
42            #[repr(C)]
43            #[derive(Clone, Copy, Debug, Eq, PartialEq)]
44            pub enum $ty {
45                $($var($var_mod::$var_ty),)+
46            }
47
48            impl $ty {
49                #[doc = "Creates a new [" $ty "]."]
50                pub const fn new() -> Self {
51                    Self::$default($default_mod::$default_ty::new())
52                }
53
54                #[doc = "Gets the response type for the [" $ty "]."]
55                pub const fn response_type(&self) -> $crate::response::ResponseType {
56                    match self {
57                        $(
58                            Self::$var(r) => r.response_type(),
59                        )+
60                    }
61                }
62
63                #[doc = "Gets the response mode for the [" $ty "]."]
64                pub const fn response_mode(&self) -> $crate::response::ResponseMode {
65                    match self {
66                        $(
67                            Self::$var(r) => r.response_mode(),
68                        )+
69                    }
70                }
71
72                $(
73                    #[doc = "Converts the [" $ty "] into the inner type."]
74                    pub const fn [<into_ $var_mod>](self) -> $crate::result::Result<$var_mod::$var_ty> {
75                        match self {
76                            Self::$var(r) => Ok(r),
77                            _ => Err($crate::result::Error::invalid_field_variant("response", self.response_type() as usize)),
78                        }
79                    }
80                )+
81            }
82
83            impl Default for $ty {
84                fn default() -> Self {
85                    Self::new()
86                }
87            }
88
89            $(
90                impl From<$var_mod::$var_ty> for $ty {
91                    fn from(val: $var_mod::$var_ty) -> Self {
92                        Self::$var(val)
93                    }
94                }
95
96                impl TryFrom<$ty> for $var_mod::$var_ty {
97                    type Error = $crate::result::Error;
98
99                    fn try_from(val: $ty) -> $crate::result::Result<Self> {
100                        val.[<into_ $var_mod>]()
101                    }
102                }
103            )+
104        }
105    };
106}