Skip to main content

feather/internals/
runtime_extensions.rs

1use feather_runtime::http::Response;
2
3/// The **Finalizer API** allows you to send data and terminate
4/// the middleware chain in a single, expressive call.
5pub trait Finalizer {
6    /// Instantly send the Text by returning `end!`
7    fn finish_text(&mut self, text: impl Into<String>) -> crate::Outcome;
8    /// Instantly send the Html by returning `end!`
9    fn finish_html(&mut self, data: impl Into<String>) -> crate::Outcome;
10    /// Instantly send the Bytes by returning `end!`
11    fn finish_bytes(&mut self, data: impl Into<Vec<u8>>) -> crate::Outcome;
12    #[cfg(feature = "json")]
13    /// Instantly send the JSON by returning `end!`
14    fn finish_json<T: serde::Serialize>(&mut self, data: &T) -> crate::Outcome;
15}
16
17impl Finalizer for Response {
18    fn finish_text(&mut self, text: impl Into<String>) -> crate::Outcome {
19        self.send_text(text);
20        Ok(crate::middlewares::MiddlewareResult::End)
21    }
22
23    fn finish_html(&mut self, data: impl Into<String>) -> crate::Outcome {
24        self.send_html(data);
25        Ok(crate::middlewares::MiddlewareResult::End)
26    }
27
28    fn finish_bytes(&mut self, data: impl Into<Vec<u8>>) -> crate::Outcome {
29        self.send_bytes(data);
30        Ok(crate::middlewares::MiddlewareResult::End)
31    }
32
33    #[cfg(feature = "json")]
34    fn finish_json<T: serde::Serialize>(&mut self, data: &T) -> crate::Outcome {
35        self.send_json(data);
36        Ok(crate::middlewares::MiddlewareResult::End)
37    }
38}