use_api_deprecation/
lib.rs1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use core::{fmt, str::FromStr};
5use std::error::Error;
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub enum ApiPrimitiveError {
10 Empty,
12 Invalid,
14 Unknown,
16}
17
18impl fmt::Display for ApiPrimitiveError {
19 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 Self::Empty => formatter.write_str("API primitive value cannot be empty"),
22 Self::Invalid => formatter.write_str("invalid API primitive value"),
23 Self::Unknown => formatter.write_str("unknown API primitive label"),
24 }
25 }
26}
27
28impl Error for ApiPrimitiveError {}
29
30fn validate_api_text(value: &str) -> Result<&str, ApiPrimitiveError> {
31 let trimmed = value.trim();
32 if trimmed.is_empty() {
33 return Err(ApiPrimitiveError::Empty);
34 }
35 if trimmed.chars().any(char::is_control) {
36 return Err(ApiPrimitiveError::Invalid);
37 }
38 Ok(trimmed)
39}
40
41macro_rules! text_newtype {
42 ($name:ident) => {
43 #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
44 pub struct $name(String);
45
46 impl $name {
47 pub fn new(value: impl AsRef<str>) -> Result<Self, ApiPrimitiveError> {
53 validate_api_text(value.as_ref()).map(|value| Self(value.to_owned()))
54 }
55
56 pub fn parse(value: impl AsRef<str>) -> Result<Self, ApiPrimitiveError> {
62 Self::new(value)
63 }
64
65 #[must_use]
67 pub fn as_str(&self) -> &str {
68 &self.0
69 }
70
71 #[must_use]
73 pub fn into_string(self) -> String {
74 self.0
75 }
76 }
77
78 impl AsRef<str> for $name {
79 fn as_ref(&self) -> &str {
80 self.as_str()
81 }
82 }
83
84 impl fmt::Display for $name {
85 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
86 formatter.write_str(self.as_str())
87 }
88 }
89
90 impl FromStr for $name {
91 type Err = ApiPrimitiveError;
92
93 fn from_str(value: &str) -> Result<Self, Self::Err> {
94 Self::new(value)
95 }
96 }
97
98 impl TryFrom<&str> for $name {
99 type Error = ApiPrimitiveError;
100
101 fn try_from(value: &str) -> Result<Self, Self::Error> {
102 Self::new(value)
103 }
104 }
105 };
106}
107
108text_newtype!(SunsetDate);
109text_newtype!(ReplacementEndpoint);
110text_newtype!(MigrationNote);
111text_newtype!(DeprecationWarning);
112
113#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
115pub enum DeprecationStatus {
116 Active,
118 Deprecated,
120 Sunset,
122}
123
124impl DeprecationStatus {
125 #[must_use]
127 pub const fn as_str(self) -> &'static str {
128 match self {
129 Self::Active => "active",
130 Self::Deprecated => "deprecated",
131 Self::Sunset => "sunset",
132 }
133 }
134}
135
136impl Default for DeprecationStatus {
137 fn default() -> Self {
138 Self::Active
139 }
140}
141
142impl fmt::Display for DeprecationStatus {
143 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
144 formatter.write_str(self.as_str())
145 }
146}
147
148impl FromStr for DeprecationStatus {
149 type Err = ApiPrimitiveError;
150
151 fn from_str(value: &str) -> Result<Self, Self::Err> {
152 let trimmed = value.trim();
153 if trimmed.is_empty() {
154 return Err(ApiPrimitiveError::Empty);
155 }
156 let normalized = trimmed.to_ascii_lowercase().replace('_', "-");
157 match normalized.as_str() {
158 "active" => Ok(Self::Active),
159 "deprecated" => Ok(Self::Deprecated),
160 "sunset" => Ok(Self::Sunset),
161 _ => Err(ApiPrimitiveError::Unknown),
162 }
163 }
164}
165
166#[derive(Clone, Debug, Eq, PartialEq)]
168pub struct PrimitiveMetadata {
169 name: SunsetDate,
170 kind: DeprecationStatus,
171}
172
173impl PrimitiveMetadata {
174 #[must_use]
176 pub const fn new(name: SunsetDate, kind: DeprecationStatus) -> Self {
177 Self { name, kind }
178 }
179
180 #[must_use]
182 pub const fn name(&self) -> &SunsetDate {
183 &self.name
184 }
185
186 #[must_use]
188 pub const fn kind(&self) -> DeprecationStatus {
189 self.kind
190 }
191}
192
193impl DeprecationStatus {
194 #[must_use]
196 pub const fn is_active(self) -> bool {
197 matches!(self, Self::Active)
198 }
199
200 #[must_use]
202 pub const fn is_deprecated(self) -> bool {
203 matches!(self, Self::Deprecated)
204 }
205
206 #[must_use]
208 pub const fn is_sunset(self) -> bool {
209 matches!(self, Self::Sunset)
210 }
211}
212
213#[cfg(test)]
214mod tests {
215 use super::*;
216
217 #[test]
218 fn parses_and_displays_text() -> Result<(), ApiPrimitiveError> {
219 let value = SunsetDate::new("2026-05-25")?;
220
221 assert_eq!(value.as_str(), "2026-05-25");
222 assert_eq!(value.to_string(), "2026-05-25");
223 assert_eq!("2026-05-25".parse::<SunsetDate>()?, value);
224 Ok(())
225 }
226
227 #[test]
228 fn rejects_empty_text() {
229 assert_eq!(SunsetDate::new(""), Err(ApiPrimitiveError::Empty));
230 }
231
232 #[test]
233 fn parses_and_displays_labels() -> Result<(), ApiPrimitiveError> {
234 let kind = "active".parse::<DeprecationStatus>()?;
235
236 assert_eq!(kind, DeprecationStatus::Active);
237 assert_eq!(kind.to_string(), "active");
238 Ok(())
239 }
240
241 #[test]
242 fn creates_metadata() -> Result<(), ApiPrimitiveError> {
243 let metadata =
244 PrimitiveMetadata::new(SunsetDate::new("2026-05-25")?, DeprecationStatus::default());
245
246 assert_eq!(metadata.name().as_str(), "2026-05-25");
247 assert_eq!(metadata.kind(), DeprecationStatus::default());
248 Ok(())
249 }
250}