use crate::Conn;
use trillium_http::{Body, Error, Headers, KnownHeaderName, Status, Version};
pub trait ConnExt {
fn set_followup(&mut self, conn: Conn) -> &mut Self;
fn followup(&self) -> Option<&Conn>;
fn take_followup(&mut self) -> Option<Conn>;
fn error(&self) -> Option<&Error>;
fn set_error(&mut self, error: Error) -> &mut Self;
fn take_error(&mut self) -> Option<Error>;
fn halt(&mut self) -> &mut Self;
fn set_halted(&mut self, halted: bool) -> &mut Self;
fn is_halted(&self) -> bool;
fn set_response_body(&mut self, body: impl Into<Body>) -> &mut Self;
#[must_use]
fn with_response_body(self, body: impl Into<Body>) -> Self
where
Self: Sized;
fn set_status(&mut self, status: Status) -> &mut Self;
#[must_use]
fn with_status(self, status: Status) -> Self
where
Self: Sized;
fn response_headers_mut(&mut self) -> &mut Headers;
fn set_response_headers(&mut self, response_headers: Headers) -> &mut Self;
fn response_trailers_mut(&mut self) -> Option<&mut Headers>;
fn set_response_trailers(&mut self, response_trailers: Headers) -> &mut Self;
}
impl ConnExt for Conn {
fn set_followup(&mut self, conn: Conn) -> &mut Self {
self.followup = Some(Box::new(conn));
self
}
fn followup(&self) -> Option<&Conn> {
self.followup.as_deref()
}
fn take_followup(&mut self) -> Option<Conn> {
self.followup.take().map(|b| *b)
}
fn error(&self) -> Option<&Error> {
self.error.as_ref()
}
fn set_error(&mut self, error: Error) -> &mut Self {
self.error = Some(error);
self
}
fn take_error(&mut self) -> Option<Error> {
self.error.take()
}
fn halt(&mut self) -> &mut Self {
self.halted = true;
self
}
fn set_halted(&mut self, halted: bool) -> &mut Self {
self.halted = halted;
self
}
fn is_halted(&self) -> bool {
self.halted
}
fn set_response_body(&mut self, body: impl Into<Body>) -> &mut Self {
let body: Body = body.into().without_chunked_framing();
if let Some(len) = body.len() {
self.response_headers_mut()
.insert(KnownHeaderName::ContentLength, len.to_string())
.remove(KnownHeaderName::TransferEncoding);
} else {
self.response_headers_mut()
.remove(KnownHeaderName::ContentLength);
if self.http_version == Version::Http1_1 {
self.response_headers_mut()
.insert(KnownHeaderName::TransferEncoding, "chunked");
}
}
drop(self.take_response_body());
self.body_override = Some(body);
self
}
fn with_response_body(mut self, body: impl Into<Body>) -> Self {
self.set_response_body(body);
self
}
fn set_status(&mut self, status: Status) -> &mut Self {
self.status = Some(status);
self
}
fn with_status(mut self, status: Status) -> Self {
self.status = Some(status);
self
}
fn response_headers_mut(&mut self) -> &mut Headers {
&mut self.response_headers
}
fn set_response_headers(&mut self, response_headers: Headers) -> &mut Self {
self.response_headers = response_headers;
self
}
fn response_trailers_mut(&mut self) -> Option<&mut Headers> {
self.response_trailers.as_mut()
}
fn set_response_trailers(&mut self, response_trailers: Headers) -> &mut Self {
self.response_trailers = Some(response_trailers);
self
}
}