s2_common/types/
resources.rs1use std::{fmt::Debug, num::NonZeroUsize, ops::Deref, str::FromStr};
2
3use compact_str::{CompactString, ToCompactString};
4
5#[derive(Debug, Default, Clone, PartialEq, Eq)]
6pub struct Page<T> {
7 pub values: Vec<T>,
8 pub has_more: bool,
9}
10
11impl<T> Page<T> {
12 pub fn new_empty() -> Self {
13 Self {
14 values: Vec::new(),
15 has_more: false,
16 }
17 }
18
19 pub fn new(values: impl Into<Vec<T>>, has_more: bool) -> Self {
20 Self {
21 values: values.into(),
22 has_more,
23 }
24 }
25}
26
27#[derive(Debug, Clone, Copy)]
28pub struct ListLimit(NonZeroUsize);
29
30impl ListLimit {
31 pub const MAX: ListLimit = Self(NonZeroUsize::new(1000).unwrap());
32
33 pub fn get(&self) -> NonZeroUsize {
34 self.0
35 }
36
37 pub fn as_usize(&self) -> usize {
38 self.0.get()
39 }
40}
41
42impl Default for ListLimit {
43 fn default() -> Self {
44 Self::MAX
45 }
46}
47
48impl From<usize> for ListLimit {
49 fn from(value: usize) -> Self {
50 NonZeroUsize::new(value)
51 .and_then(|n| (n <= Self::MAX.0).then_some(Self(n)))
52 .unwrap_or_default()
53 }
54}
55
56impl From<ListLimit> for usize {
57 fn from(value: ListLimit) -> Self {
58 value.as_usize()
59 }
60}
61
62#[derive(Debug, Clone, Default)]
63pub struct ListItemsRequestParts<P, S> {
64 pub prefix: P,
65 pub start_after: S,
66 pub limit: ListLimit,
67}
68
69#[derive(Debug, Clone, Default)]
70pub struct ListItemsRequest<P, S>(ListItemsRequestParts<P, S>)
71where
72 P: Default,
73 S: Default;
74
75impl<P, S> ListItemsRequest<P, S>
76where
77 P: Default,
78 S: Default,
79{
80 pub fn parts(&self) -> &ListItemsRequestParts<P, S> {
81 &self.0
82 }
83}
84
85impl<P, S> From<ListItemsRequest<P, S>> for ListItemsRequestParts<P, S>
86where
87 P: Default,
88 S: Default,
89{
90 fn from(ListItemsRequest(parts): ListItemsRequest<P, S>) -> Self {
91 parts
92 }
93}
94
95#[derive(Debug, Clone, thiserror::Error)]
96#[error("`start_after` must be greater than or equal to the `prefix`")]
97pub struct StartAfterLessThanPrefixError;
98
99impl<P, S> TryFrom<ListItemsRequestParts<P, S>> for ListItemsRequest<P, S>
100where
101 P: Deref<Target = str> + Default,
102 S: Deref<Target = str> + Default,
103{
104 type Error = StartAfterLessThanPrefixError;
105
106 fn try_from(parts: ListItemsRequestParts<P, S>) -> Result<Self, Self::Error> {
107 let start_after: &str = &parts.start_after;
108 let prefix: &str = &parts.prefix;
109
110 if !start_after.is_empty() && !prefix.is_empty() && start_after < prefix {
111 return Err(StartAfterLessThanPrefixError);
112 }
113
114 Ok(Self(parts))
115 }
116}
117
118pub static REQUEST_TOKEN_HEADER: http::HeaderName =
119 http::HeaderName::from_static("s2-request-token");
120
121pub const MAX_REQUEST_TOKEN_LENGTH: usize = 36;
122
123#[derive(Debug, PartialEq, Eq, thiserror::Error)]
124#[error("request token was longer than {MAX_REQUEST_TOKEN_LENGTH} bytes in length: {0}")]
125pub struct RequestTokenTooLongError(pub usize);
126
127#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
128pub struct RequestToken(CompactString);
129
130#[cfg(feature = "utoipa")]
131impl utoipa::PartialSchema for RequestToken {
132 fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
133 utoipa::openapi::Object::builder()
134 .schema_type(utoipa::openapi::Type::String)
135 .max_length(Some(MAX_REQUEST_TOKEN_LENGTH))
136 .into()
137 }
138}
139
140#[cfg(feature = "utoipa")]
141impl utoipa::ToSchema for RequestToken {}
142
143impl serde::Serialize for RequestToken {
144 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
145 where
146 S: serde::Serializer,
147 {
148 serializer.serialize_str(&self.0)
149 }
150}
151
152impl<'de> serde::Deserialize<'de> for RequestToken {
153 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
154 where
155 D: serde::Deserializer<'de>,
156 {
157 let s = CompactString::deserialize(deserializer)?;
158 RequestToken::try_from(s).map_err(serde::de::Error::custom)
159 }
160}
161
162impl std::fmt::Display for RequestToken {
163 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164 write!(f, "{}", self.0)
165 }
166}
167
168impl TryFrom<CompactString> for RequestToken {
169 type Error = RequestTokenTooLongError;
170
171 fn try_from(input: CompactString) -> Result<Self, Self::Error> {
172 if input.len() > MAX_REQUEST_TOKEN_LENGTH {
173 return Err(RequestTokenTooLongError(input.len()));
174 }
175 Ok(RequestToken(input))
176 }
177}
178
179impl FromStr for RequestToken {
180 type Err = RequestTokenTooLongError;
181
182 fn from_str(s: &str) -> Result<Self, Self::Err> {
183 s.to_compact_string().try_into()
184 }
185}
186
187impl From<RequestToken> for CompactString {
188 fn from(token: RequestToken) -> Self {
189 token.0
190 }
191}
192
193impl AsRef<str> for RequestToken {
194 fn as_ref(&self) -> &str {
195 &self.0
196 }
197}
198
199impl Deref for RequestToken {
200 type Target = str;
201
202 fn deref(&self) -> &Self::Target {
203 &self.0
204 }
205}
206
207impl crate::http::ParseableHeader for RequestToken {
208 fn name() -> &'static http::HeaderName {
209 &REQUEST_TOKEN_HEADER
210 }
211}