sark_core/request/
view.rs1use std::ops;
2
3use sark_http::request::body;
4
5use crate::request::{Head, frame, frame::proof};
6
7pub struct View<'req, 'd: 'req, P = proof::Maybe> {
8 pub(in crate::request) head: Head<&'req [u8], P>,
9 body: body::Ref<'req, 'd>,
10}
11
12impl<'req, 'd: 'req, P: proof::Proof> View<'req, 'd, P> {
13 #[doc(hidden)]
14 pub const fn from_head(head: Head<&'req [u8], P>, body: body::Ref<'req, 'd>) -> Self {
15 Self { head, body }
16 }
17
18 pub fn try_path(&self, range: ops::Range<usize>) -> Result<&'req [u8], frame::Error> {
19 try_slice(self.head.path(), range)
20 }
21
22 pub fn try_frame_at(&self, range: ops::Range<usize>) -> Result<&'req [u8], frame::Error> {
23 try_slice(self.head.owner(), range)
24 }
25
26 pub const fn body(&self) -> body::Ref<'req, 'd> {
27 self.body
28 }
29
30 pub const fn body_len(&self) -> usize {
31 self.body.len()
32 }
33}
34
35fn try_slice(bytes: &[u8], range: ops::Range<usize>) -> Result<&[u8], frame::Error> {
36 let start = range.start;
37 let end = range.end;
38 let capacity = bytes.len();
39 if start > end {
40 return Err(frame::Error::Order {
41 start,
42 end,
43 capacity,
44 });
45 }
46 bytes.get(range).ok_or(frame::Error::Bounds {
47 start,
48 end,
49 capacity,
50 })
51}