chuchi/api/
response.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::{
	extractor::Extractor,
	extractor_extract, extractor_prepare, extractor_validate,
	header::{values::IntoHeaderName, HeaderValue, HeaderValues, StatusCode},
	state::StateRefCell,
};

use std::{convert::Infallible, fmt};

#[derive(Debug, Clone)]
pub struct ResponseSettings {
	pub(crate) headers: HeaderValues,
	pub(crate) status: StatusCode,
}

impl ResponseSettings {
	#[doc(hidden)]
	pub fn new() -> Self {
		Self {
			headers: HeaderValues::new(),
			status: StatusCode::OK,
		}
	}

	#[doc(hidden)]
	pub fn new_for_state() -> StateRefCell<Self> {
		StateRefCell::new(Self::new())
	}

	pub fn headers_mut(&mut self) -> &mut HeaderValues {
		&mut self.headers
	}

	/// Sets a header value.
	///
	/// ## Note
	/// Only ASCII characters are allowed, use
	/// `self.headers_mut().encode_value()` to allow any character.
	///
	/// ## Panics
	/// If the value is not a valid `HeaderValue`.
	pub fn header<K, V>(&mut self, key: K, val: V) -> &mut Self
	where
		K: IntoHeaderName,
		V: TryInto<HeaderValue>,
		V::Error: fmt::Debug,
	{
		self.headers.insert(key, val);
		self
	}

	pub fn status(&mut self, status: StatusCode) -> &mut Self {
		self.status = status;
		self
	}
}

impl<'a, R> Extractor<'a, R> for &'a mut ResponseSettings {
	type Error = Infallible;
	type Prepared = ();

	extractor_validate!(|validate| {
		assert!(
			validate.state.validate::<StateRefCell<ResponseSettings>>(),
			"ResponseSettings not in state"
		);
	});

	extractor_prepare!();

	extractor_extract!(|extract| {
		Ok(extract
			.state
			.get::<StateRefCell<ResponseSettings>>()
			.unwrap()
			.get_mut())
	});
}