doido_controller/
respond.rs1use crate::axum::body::Body;
9use crate::axum::response::Response;
10use http::StatusCode;
11
12#[derive(Clone, Copy, PartialEq, Eq, Debug)]
14pub enum Format {
15 Html,
16 Json,
17 Any,
18}
19
20pub struct RespondTo {
22 format: Format,
23 response: Option<Response>,
24}
25
26impl RespondTo {
27 pub fn new(format: Format) -> Self {
28 Self {
29 format,
30 response: None,
31 }
32 }
33
34 pub fn html(mut self, f: impl FnOnce() -> Response) -> Self {
36 if self.response.is_none() && matches!(self.format, Format::Html | Format::Any) {
37 self.response = Some(f());
38 }
39 self
40 }
41
42 pub fn json(mut self, f: impl FnOnce() -> Response) -> Self {
44 if self.response.is_none() && matches!(self.format, Format::Json | Format::Any) {
45 self.response = Some(f());
46 }
47 self
48 }
49
50 pub fn finish(self) -> Response {
52 self.response.unwrap_or_else(|| {
53 Response::builder()
54 .status(StatusCode::NOT_ACCEPTABLE)
55 .body(Body::empty())
56 .expect("valid 406 response")
57 })
58 }
59}