mod request;
mod response;
pub use request::{Condition, ConditionMatch};
pub use response::{ResponseCondition, ResponseConditionMatch};
macro_rules! impl_condition_deserialize {
($cond:ident, $match_:ty, $label:expr) => {
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct ConditionDeserHelper {
#[serde(default)]
when: Option<$match_>,
#[serde(default)]
unless: Option<$match_>,
}
impl<'de> serde::Deserialize<'de> for $cond {
fn deserialize<D: serde::de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let helper = ConditionDeserHelper::deserialize(deserializer)?;
match (helper.when, helper.unless) {
(Some(m), None) => Ok($cond::When(m)),
(None, Some(m)) => Ok($cond::Unless(m)),
(Some(_), Some(_)) => Err(serde::de::Error::custom(concat!(
$label,
" must have exactly one of 'when' or 'unless', not both"
))),
(None, None) => Err(serde::de::Error::custom(concat!(
$label,
" must have either 'when' or 'unless'"
))),
}
}
}
};
}
pub(crate) use impl_condition_deserialize;
macro_rules! impl_condition_serialize {
($cond:ident, $match_:ty) => {
impl serde::Serialize for $cond {
fn serialize<S: serde::ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeMap as _;
let mut map = serializer.serialize_map(Some(1))?;
match self {
$cond::When(m) => map.serialize_entry("when", m)?,
$cond::Unless(m) => map.serialize_entry("unless", m)?,
}
map.end()
}
}
};
}
pub(crate) use impl_condition_serialize;