nexus_web/rest/
response.rs1use crate::http::ResponseReader;
4
5pub struct RestResponse<'a> {
10 pub(crate) status: u16,
11 pub(crate) body_len: usize,
12 pub(crate) resp_reader: &'a ResponseReader,
13 pub(crate) chunked_body: Option<Vec<u8>>,
15}
16
17impl<'a> RestResponse<'a> {
18 pub fn new(status: u16, body_len: usize, resp_reader: &'a ResponseReader) -> Self {
23 Self {
24 status,
25 body_len,
26 resp_reader,
27 chunked_body: None,
28 }
29 }
30
31 pub fn new_chunked(status: u16, body: Vec<u8>, resp_reader: &'a ResponseReader) -> Self {
33 let body_len = body.len();
34 Self {
35 status,
36 body_len,
37 resp_reader,
38 chunked_body: Some(body),
39 }
40 }
41
42 pub fn status(&self) -> u16 {
44 self.status
45 }
46
47 pub fn header(&self, name: &str) -> Option<&str> {
49 self.resp_reader.header(name)
50 }
51
52 pub fn body(&self) -> &[u8] {
54 if let Some(ref chunked) = self.chunked_body {
55 return chunked;
56 }
57 let remainder = self.resp_reader.remainder();
58 &remainder[..self.body_len.min(remainder.len())]
59 }
60
61 pub fn body_str(&self) -> Result<&str, std::str::Utf8Error> {
63 std::str::from_utf8(self.body())
64 }
65
66 pub fn body_len(&self) -> usize {
68 self.body_len
69 }
70
71 #[cfg(feature = "bytes")]
82 pub fn body_to_bytes(&self) -> bytes::Bytes {
83 bytes::Bytes::copy_from_slice(self.body())
84 }
85
86 pub fn header_count(&self) -> usize {
88 self.resp_reader.header_count()
89 }
90}
91
92impl std::fmt::Debug for RestResponse<'_> {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 f.debug_struct("RestResponse")
95 .field("status", &self.status)
96 .field("body_len", &self.body_len)
97 .finish()
98 }
99}