use std::ops::{Deref, DerefMut};
use serde::{Deserialize, Serialize};
use super::PathItem;
use crate::PropMap;
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct Callback(pub PropMap<String, PathItem>);
impl Callback {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn path<S: Into<String>, P: Into<PathItem>>(mut self, expression: S, path_item: P) -> Self {
self.0.insert(expression.into(), path_item.into());
self
}
pub fn insert<S: Into<String>, P: Into<PathItem>>(&mut self, expression: S, path_item: P) {
self.0.insert(expression.into(), path_item.into());
}
}
impl Deref for Callback {
type Target = PropMap<String, PathItem>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Callback {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl IntoIterator for Callback {
type Item = (String, PathItem);
type IntoIter = <PropMap<String, PathItem> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<S, P> FromIterator<(S, P)> for Callback
where
S: Into<String>,
P: Into<PathItem>,
{
fn from_iter<I: IntoIterator<Item = (S, P)>>(iter: I) -> Self {
Self(
iter.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect(),
)
}
}
#[cfg(test)]
mod tests {
use assert_json_diff::assert_json_eq;
use serde_json::json;
use super::*;
use crate::{Operation, PathItemType};
#[test]
fn callback_default_is_empty() {
let callback = Callback::default();
assert!(callback.is_empty());
}
#[test]
fn callback_serializes_as_map_of_path_items() {
let callback = Callback::new().path(
"{$request.body#/callbackUrl}",
PathItem::new(PathItemType::Post, Operation::new()),
);
assert_json_eq!(
callback,
json!({
"{$request.body#/callbackUrl}": {
"post": { "responses": {} }
}
})
);
}
}