use std::ops::Deref;
use http::{HeaderMap, StatusCode};
use serde::Serialize;
use crate::{Hal, Link, Template};
pub struct HalResponse {
pub hal: Hal,
pub status_code: StatusCode,
pub headers: HeaderMap,
}
impl HalResponse {
#[must_use]
pub fn with_link<N, L>(mut self, name: N, link: L) -> Self
where
N: ToString,
L: Into<Link>,
{
self.hal = self.hal.with_link(name, link);
self
}
#[must_use]
pub fn maybe_with_link<N, L>(mut self, name: N, link: Option<L>) -> Self
where
N: ToString,
L: Into<Link>,
{
self.hal = self.hal.maybe_with_link(name, link);
self
}
#[must_use]
pub fn with_embedded<N, H>(mut self, name: N, value: H) -> Self
where
N: ToString,
H: Into<Hal>,
{
self.hal = self.hal.with_embedded(name, value);
self
}
#[must_use]
pub fn maybe_with_embedded<N, H>(mut self, name: N, value: Option<H>) -> Self
where
N: ToString,
H: Into<Hal>,
{
self.hal = self.hal.maybe_with_embedded(name, value);
self
}
#[must_use]
pub fn with_template<N, T>(mut self, name: N, value: T) -> Self
where
N: ToString,
T: Into<Template>,
{
self.hal = self.hal.with_template(name, value);
self
}
#[must_use]
pub fn maybe_with_template<N, T>(mut self, name: N, template: Option<T>) -> Self
where
N: ToString,
T: Into<Template>,
{
self.hal = self.hal.maybe_with_template(name, template);
self
}
#[must_use]
pub fn with_status_code<S>(mut self, status_code: S) -> Self
where
S: Into<StatusCode>,
{
self.status_code = status_code.into();
self
}
#[must_use]
#[allow(clippy::needless_pass_by_value)] pub fn with_header<H>(mut self, header: H) -> Self
where
H: headers_core::Header,
{
let mut values = vec![];
header.encode(&mut values);
for value in values {
self.headers.append(H::name(), value);
}
self
}
}
impl Deref for HalResponse {
type Target = Hal;
fn deref(&self) -> &Self::Target {
&self.hal
}
}
#[must_use]
pub fn new<V>(value: V) -> HalResponse
where
V: Serialize,
{
HalResponse {
hal: Hal::new(value),
status_code: StatusCode::OK,
headers: HeaderMap::default(),
}
}