Skip to content

Commit

Permalink
chore: map_deserialized_matcher!
Browse files Browse the repository at this point in the history
  • Loading branch information
kolbma committed Oct 13, 2023
1 parent 39c55eb commit 8f92a1a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 66 deletions.
72 changes: 6 additions & 66 deletions src/response/mapping/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! mapping macros
mod map_deserialized_matcher;

/// Generate implementations of `TryFrom<Mapping>` for a `structure`
///
/// # Examples
Expand Down Expand Up @@ -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>>,
Expand Down Expand Up @@ -71,43 +72,12 @@ macro_rules! map_deserialized {
type Error = $crate::Error;

fn try_from(mapping: $crate::response::mapping::Mapping) -> Result<Self, Self::Error> {
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>,
Expand Down Expand Up @@ -139,37 +109,7 @@ macro_rules! map_deserialized {
type Error = $crate::Error;

fn try_from(mapping: $crate::response::mapping::Mapping) -> Result<Self, Self::Error> {
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)
}
}
};
Expand Down
45 changes: 45 additions & 0 deletions src/response/mapping/macros/map_deserialized_matcher.rs
Original file line number Diff line number Diff line change
@@ -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()),
}
}};
}

0 comments on commit 8f92a1a

Please sign in to comment.