diff --git a/src/response/mapping/macros.rs b/src/response/mapping/macros.rs index db92100..1e725b2 100644 --- a/src/response/mapping/macros.rs +++ b/src/response/mapping/macros.rs @@ -1,5 +1,7 @@ //! mapping macros +mod map_deserialized_matcher; + /// Generate implementations of `TryFrom` for a `structure` /// /// # Examples @@ -37,8 +39,7 @@ #[macro_export] macro_rules! map_deserialized { ($v:vis $p:ident, $t:lifetime => $l:ident) => { - #[doc = concat!("Container with _inner_ `", stringify!($p), "`")] - #[doc = "\nImplements `Deref`"] + #[doc = concat!("Container with _inner_ `", stringify!($p), "`\n\nImplements `Deref`\n")] #[derive(Debug)] $v struct $l<$t> { inner: Vec<$p<$t>>, @@ -71,43 +72,12 @@ macro_rules! map_deserialized { type Error = $crate::Error; fn try_from(mapping: $crate::response::mapping::Mapping) -> Result { - match mapping { - $crate::response::mapping::Mapping::Associative(mut associative) => { - let mut v = Vec::new(); - - while let Some(row) = associative.rows.pop() { - v.insert(0, $p::deserialize(serde::de::value::MapDeserializer::new(row.into_iter())) - .map_err(|err| $crate::Error::ResultError(err.to_string()))?); - } - - Ok($l::new(v)) - } - $crate::response::mapping::Mapping::Error(err) => Err((err.error.as_str()).into()), - $crate::response::mapping::Mapping::Execute(_) => { - unimplemented!("execute mapping") - }, - $crate::response::mapping::Mapping::Standard(standard) => { - let mut associative = $crate::response::mapping::Associative::from(standard); - - let mut v = Vec::new(); - - while let Some(row) = associative.rows.pop() { - v.insert(0, $p::deserialize(serde::de::value::MapDeserializer::new(row.into_iter())) - .map_err(|err| $crate::Error::ResultError(err.to_string()))?); - } - - Ok($l::new(v)) - } - $crate::response::mapping::Mapping::Empty(_) => { - Err("empty result".into()) - } - } + $crate::map_deserialized_matcher!($p, $l, mapping) } } }; ($v:vis $p:ident => $l:ident) => { - #[doc = concat!("Container with _inner_ `", stringify!($p), "`")] - #[doc = "\nImplements `Deref`"] + #[doc = concat!("Container with _inner_ `", stringify!($p), "`\n\nImplements `Deref`\n")] #[derive(Debug)] $v struct $l { inner: Vec<$p>, @@ -139,37 +109,7 @@ macro_rules! map_deserialized { type Error = $crate::Error; fn try_from(mapping: $crate::response::mapping::Mapping) -> Result { - match mapping { - $crate::response::mapping::Mapping::Associative(mut associative) => { - let mut v = Vec::new(); - - while let Some(row) = associative.rows.pop() { - v.insert(0, $p::deserialize(serde::de::value::MapDeserializer::new(row.into_iter())) - .map_err(|err| $crate::Error::ResultError(err.to_string()))?); - } - - Ok($l::new(v)) - } - $crate::response::mapping::Mapping::Error(err) => Err((err.error.as_str()).into()), - $crate::response::mapping::Mapping::Execute(_) => { - unimplemented!("execute mapping") - }, - $crate::response::mapping::Mapping::Standard(standard) => { - let mut associative = $crate::response::mapping::Associative::from(standard); - - let mut v = Vec::new(); - - while let Some(row) = associative.rows.pop() { - v.insert(0, $p::deserialize(serde::de::value::MapDeserializer::new(row.into_iter())) - .map_err(|err| $crate::Error::ResultError(err.to_string()))?); - } - - Ok($l::new(v)) - } - $crate::response::mapping::Mapping::Empty(_) => { - Err("empty result".into()) - } - } + $crate::map_deserialized_matcher!($p, $l, mapping) } } }; diff --git a/src/response/mapping/macros/map_deserialized_matcher.rs b/src/response/mapping/macros/map_deserialized_matcher.rs new file mode 100644 index 0000000..eff3f86 --- /dev/null +++ b/src/response/mapping/macros/map_deserialized_matcher.rs @@ -0,0 +1,45 @@ +//! Matcher used in [`crate::map_deserialized!`] +#![doc(hidden)] + +/// Matcher used in [`crate::map_deserialized!`] +#[macro_export] +#[doc(hidden)] +macro_rules! map_deserialized_matcher { + ($p:ident, $l:ident, $m:expr) => {{ + match $m { + $crate::response::mapping::Mapping::Associative(mut associative) => { + let mut v = Vec::new(); + + while let Some(row) = associative.rows.pop() { + v.insert( + 0, + $p::deserialize(serde::de::value::MapDeserializer::new(row.into_iter())) + .map_err(|err| $crate::Error::ResultError(err.to_string()))?, + ); + } + + Ok($l::new(v)) + } + $crate::response::mapping::Mapping::Error(err) => Err((err.error.as_str()).into()), + $crate::response::mapping::Mapping::Execute(_) => { + std::unimplemented!("execute mapping") + } + $crate::response::mapping::Mapping::Standard(standard) => { + let mut associative = $crate::response::mapping::Associative::from(standard); + + let mut v = Vec::new(); + + while let Some(row) = associative.rows.pop() { + v.insert( + 0, + $p::deserialize(serde::de::value::MapDeserializer::new(row.into_iter())) + .map_err(|err| $crate::Error::ResultError(err.to_string()))?, + ); + } + + Ok($l::new(v)) + } + $crate::response::mapping::Mapping::Empty(_) => Err("empty result".into()), + } + }}; +}