cratestack_axum/envelope_layer/resolver_payload.rs
1//! The payload types a route declares (cratestack#1168).
2
3use super::resolver::ResolvedRoute;
4
5/// The request and response payload types a route declares.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub(super) struct PayloadTypes {
8 pub(super) request: &'static [&'static str],
9 pub(super) response: &'static [&'static str],
10}
11
12impl ResolvedRoute {
13 /// The payload types this route accepts and answers in, narrowing what
14 /// the layer allows ([`EnvelopeLayerBuilder::payload_media_types`]) for
15 /// this route alone: an op's allowed set is the layer's set intersected
16 /// with this one. The built-in resolvers fill it from the schema's
17 /// `capabilities.request_types` / `response_types`; a custom resolver
18 /// that sets none does not narrow.
19 ///
20 /// ```
21 /// use cratestack_axum::envelope_layer::ResolvedRoute;
22 ///
23 /// let route = ResolvedRoute::new("/v1/charges", vec![]).with_payload_types(
24 /// &["application/x-www-form-urlencoded"],
25 /// &["application/json"],
26 /// );
27 /// assert_eq!(
28 /// route.payload_request_types(),
29 /// Some(&["application/x-www-form-urlencoded"][..])
30 /// );
31 /// assert_eq!(route.payload_response_types(), Some(&["application/json"][..]));
32 /// assert_eq!(ResolvedRoute::new("/v1/charges", vec![]).payload_request_types(), None);
33 /// ```
34 ///
35 /// [`EnvelopeLayerBuilder::payload_media_types`]: super::EnvelopeLayerBuilder::payload_media_types
36 #[must_use]
37 pub fn with_payload_types(
38 mut self,
39 request: &'static [&'static str],
40 response: &'static [&'static str],
41 ) -> Self {
42 self.payload_types = Some(PayloadTypes { request, response });
43 self
44 }
45
46 /// The request types set by [`with_payload_types`](Self::with_payload_types).
47 pub fn payload_request_types(&self) -> Option<&'static [&'static str]> {
48 self.payload_types.map(|types| types.request)
49 }
50
51 /// The response types set by [`with_payload_types`](Self::with_payload_types).
52 pub fn payload_response_types(&self) -> Option<&'static [&'static str]> {
53 self.payload_types.map(|types| types.response)
54 }
55
56 pub(super) fn payload_types(&self) -> Option<PayloadTypes> {
57 self.payload_types
58 }
59}