sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
/// Convenience macro to define response types.
#[macro_export]
macro_rules! response {
    (
        $ty:ident {
            response_mode: $mode:ident,
        }
    ) => {
        ::paste::paste! {
            impl $ty {
                #[doc = "Gets the response type for the [" $ty "]."]
                #[inline]
                pub const fn response_type(&self) -> $crate::response::ResponseType {
                    $crate::response::ResponseType::$ty
                }

                #[doc = "Gets the response mode for the [" $ty "]."]
                #[inline]
                pub const fn response_mode(&self) -> $crate::response::ResponseMode {
                    $crate::response::ResponseMode::$mode
                }
            }
        }
    };
}

/// Convenience macro to define a response type wrapper enum.
#[macro_export]
macro_rules! response_enum {
    (
        $(#[$doc:meta])*
        $ty:ident {
            default: $default:ident($default_mod:ident :: $default_ty:ident),
            $(
                $(#[$var_doc:meta])*
                $var:ident ( $var_mod:ident :: $var_ty:ident ),
            )+
        }
    ) => {
        ::paste::paste! {
            $(#[$doc])*
            #[repr(C)]
            #[derive(Clone, Copy, Debug, Eq, PartialEq)]
            pub enum $ty {
                $($var($var_mod::$var_ty),)+
            }

            impl $ty {
                #[doc = "Creates a new [" $ty "]."]
                pub const fn new() -> Self {
                    Self::$default($default_mod::$default_ty::new())
                }

                #[doc = "Gets the response type for the [" $ty "]."]
                pub const fn response_type(&self) -> $crate::response::ResponseType {
                    match self {
                        $(
                            Self::$var(r) => r.response_type(),
                        )+
                    }
                }

                #[doc = "Gets the response mode for the [" $ty "]."]
                pub const fn response_mode(&self) -> $crate::response::ResponseMode {
                    match self {
                        $(
                            Self::$var(r) => r.response_mode(),
                        )+
                    }
                }

                $(
                    #[doc = "Converts the [" $ty "] into the inner type."]
                    pub const fn [<into_ $var_mod>](self) -> $crate::result::Result<$var_mod::$var_ty> {
                        match self {
                            Self::$var(r) => Ok(r),
                            _ => Err($crate::result::Error::invalid_field_variant("response", self.response_type() as usize)),
                        }
                    }
                )+
            }

            impl Default for $ty {
                fn default() -> Self {
                    Self::new()
                }
            }

            $(
                impl From<$var_mod::$var_ty> for $ty {
                    fn from(val: $var_mod::$var_ty) -> Self {
                        Self::$var(val)
                    }
                }

                impl TryFrom<$ty> for $var_mod::$var_ty {
                    type Error = $crate::result::Error;

                    fn try_from(val: $ty) -> $crate::result::Result<Self> {
                        val.[<into_ $var_mod>]()
                    }
                }
            )+
        }
    };
}