1#[cfg(feature = "chumsky")]
4use chumsky::{Parser, prelude::just};
5
6#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, strum::EnumIs)]
8#[expect(
9 clippy::module_name_repetitions,
10 reason = "the type is used outside this module"
11)]
12pub enum SearchCategory {
13 All,
15 People,
17 Places,
19 Events,
21 Groups,
23 Wiki,
25 Destinations,
27 Classifieds,
29}
30
31#[cfg(feature = "chumsky")]
37#[must_use]
38#[expect(
39 clippy::module_name_repetitions,
40 reason = "the parser is used outside this module"
41)]
42pub fn search_category_parser<'src>()
43-> impl Parser<'src, &'src str, SearchCategory, chumsky::extra::Err<chumsky::error::Rich<'src, char>>>
44{
45 just("all")
46 .to(SearchCategory::All)
47 .or(just("people").to(SearchCategory::People))
48 .or(just("places").to(SearchCategory::Places))
49 .or(just("events").to(SearchCategory::Events))
50 .or(just("groups").to(SearchCategory::Groups))
51 .or(just("wiki").to(SearchCategory::Wiki))
52 .or(just("destinations").to(SearchCategory::Destinations))
53 .or(just("classifieds").to(SearchCategory::Classifieds))
54}
55
56impl std::fmt::Display for SearchCategory {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 match self {
59 Self::All => write!(f, "all"),
60 Self::People => write!(f, "people"),
61 Self::Places => write!(f, "places"),
62 Self::Events => write!(f, "events"),
63 Self::Groups => write!(f, "groups"),
64 Self::Wiki => write!(f, "wiki"),
65 Self::Destinations => write!(f, "destinations"),
66 Self::Classifieds => write!(f, "classifieds"),
67 }
68 }
69}
70
71#[derive(Debug, Clone)]
73#[expect(
74 clippy::module_name_repetitions,
75 reason = "the type is used outside this module"
76)]
77pub struct SearchCategoryParseError {
78 value: String,
80}
81
82impl std::fmt::Display for SearchCategoryParseError {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 write!(f, "Could not parse as SearchCategory: {}", self.value)
85 }
86}
87
88impl std::str::FromStr for SearchCategory {
89 type Err = SearchCategoryParseError;
90
91 fn from_str(s: &str) -> Result<Self, Self::Err> {
92 match s {
93 "all" => Ok(Self::All),
94 "people" => Ok(Self::People),
95 "places" => Ok(Self::Places),
96 "events" => Ok(Self::Events),
97 "groups" => Ok(Self::Groups),
98 "wiki" => Ok(Self::Wiki),
99 "destinations" => Ok(Self::Destinations),
100 "classifieds" => Ok(Self::Classifieds),
101 _ => Err(SearchCategoryParseError {
102 value: s.to_owned(),
103 }),
104 }
105 }
106}
107
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
119pub struct EventId(pub u32);
120
121impl EventId {
122 #[must_use]
124 pub const fn new(id: u32) -> Self {
125 Self(id)
126 }
127
128 #[must_use]
130 pub const fn get(self) -> u32 {
131 self.0
132 }
133}
134
135impl std::fmt::Display for EventId {
136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137 write!(f, "{}", self.0)
138 }
139}
140
141#[cfg(feature = "chumsky")]
149#[must_use]
150pub fn event_id_parser<'src>()
151-> impl Parser<'src, &'src str, EventId, chumsky::extra::Err<chumsky::error::Rich<'src, char>>> {
152 crate::utils::u32_parser().map(EventId)
153}
154
155#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
165#[non_exhaustive]
166pub enum ClassifiedCategory {
167 #[default]
169 AnyCategory,
170 Shopping,
172 LandRental,
174 PropertyRental,
176 SpecialAttraction,
178 NewProducts,
180 Employment,
182 Wanted,
184 Service,
186 Personal,
188 Unknown(u32),
191}
192
193impl ClassifiedCategory {
194 #[must_use]
196 pub const fn from_u32(value: u32) -> Self {
197 match value {
198 0 => Self::AnyCategory,
199 1 => Self::Shopping,
200 2 => Self::LandRental,
201 3 => Self::PropertyRental,
202 4 => Self::SpecialAttraction,
203 5 => Self::NewProducts,
204 6 => Self::Employment,
205 7 => Self::Wanted,
206 8 => Self::Service,
207 9 => Self::Personal,
208 other => Self::Unknown(other),
209 }
210 }
211
212 #[must_use]
214 pub const fn to_u32(self) -> u32 {
215 match self {
216 Self::AnyCategory => 0,
217 Self::Shopping => 1,
218 Self::LandRental => 2,
219 Self::PropertyRental => 3,
220 Self::SpecialAttraction => 4,
221 Self::NewProducts => 5,
222 Self::Employment => 6,
223 Self::Wanted => 7,
224 Self::Service => 8,
225 Self::Personal => 9,
226 Self::Unknown(value) => value,
227 }
228 }
229}
230
231impl std::fmt::Display for ClassifiedCategory {
232 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
233 match self {
234 Self::AnyCategory => write!(f, "any"),
235 Self::Shopping => write!(f, "shopping"),
236 Self::LandRental => write!(f, "land rental"),
237 Self::PropertyRental => write!(f, "property rental"),
238 Self::SpecialAttraction => write!(f, "special attraction"),
239 Self::NewProducts => write!(f, "new products"),
240 Self::Employment => write!(f, "employment"),
241 Self::Wanted => write!(f, "wanted"),
242 Self::Service => write!(f, "service"),
243 Self::Personal => write!(f, "personal"),
244 Self::Unknown(value) => write!(f, "{value}"),
245 }
246 }
247}
248
249#[derive(Debug, Clone)]
251pub struct ClassifiedCategoryParseError {
252 value: String,
254}
255
256impl std::fmt::Display for ClassifiedCategoryParseError {
257 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
258 write!(f, "Could not parse as ClassifiedCategory: {}", self.value)
259 }
260}
261
262impl std::str::FromStr for ClassifiedCategory {
263 type Err = ClassifiedCategoryParseError;
264
265 fn from_str(s: &str) -> Result<Self, Self::Err> {
266 match s {
267 "any" => Ok(Self::AnyCategory),
268 "shopping" => Ok(Self::Shopping),
269 "land rental" => Ok(Self::LandRental),
270 "property rental" => Ok(Self::PropertyRental),
271 "special attraction" => Ok(Self::SpecialAttraction),
272 "new products" => Ok(Self::NewProducts),
273 "employment" => Ok(Self::Employment),
274 "wanted" => Ok(Self::Wanted),
275 "service" => Ok(Self::Service),
276 "personal" => Ok(Self::Personal),
277 _ => Err(ClassifiedCategoryParseError {
278 value: s.to_owned(),
279 }),
280 }
281 }
282}
283
284#[cfg(feature = "chumsky")]
291#[must_use]
292pub fn classified_category_parser<'src>() -> impl Parser<
293 'src,
294 &'src str,
295 ClassifiedCategory,
296 chumsky::extra::Err<chumsky::error::Rich<'src, char>>,
297> {
298 just("any")
299 .to(ClassifiedCategory::AnyCategory)
300 .or(just("shopping").to(ClassifiedCategory::Shopping))
301 .or(just("land rental").to(ClassifiedCategory::LandRental))
302 .or(just("property rental").to(ClassifiedCategory::PropertyRental))
303 .or(just("special attraction").to(ClassifiedCategory::SpecialAttraction))
304 .or(just("new products").to(ClassifiedCategory::NewProducts))
305 .or(just("employment").to(ClassifiedCategory::Employment))
306 .or(just("wanted").to(ClassifiedCategory::Wanted))
307 .or(just("service").to(ClassifiedCategory::Service))
308 .or(just("personal").to(ClassifiedCategory::Personal))
309}