#[cfg(feature = "chumsky")]
use chumsky::{Parser, prelude::just};
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, strum::EnumIs)]
#[expect(
clippy::module_name_repetitions,
reason = "the type is used outside this module"
)]
pub enum SearchCategory {
All,
People,
Places,
Events,
Groups,
Wiki,
Destinations,
Classifieds,
}
#[cfg(feature = "chumsky")]
#[must_use]
#[expect(
clippy::module_name_repetitions,
reason = "the parser is used outside this module"
)]
pub fn search_category_parser<'src>()
-> impl Parser<'src, &'src str, SearchCategory, chumsky::extra::Err<chumsky::error::Rich<'src, char>>>
{
just("all")
.to(SearchCategory::All)
.or(just("people").to(SearchCategory::People))
.or(just("places").to(SearchCategory::Places))
.or(just("events").to(SearchCategory::Events))
.or(just("groups").to(SearchCategory::Groups))
.or(just("wiki").to(SearchCategory::Wiki))
.or(just("destinations").to(SearchCategory::Destinations))
.or(just("classifieds").to(SearchCategory::Classifieds))
}
impl std::fmt::Display for SearchCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::All => write!(f, "all"),
Self::People => write!(f, "people"),
Self::Places => write!(f, "places"),
Self::Events => write!(f, "events"),
Self::Groups => write!(f, "groups"),
Self::Wiki => write!(f, "wiki"),
Self::Destinations => write!(f, "destinations"),
Self::Classifieds => write!(f, "classifieds"),
}
}
}
#[derive(Debug, Clone)]
#[expect(
clippy::module_name_repetitions,
reason = "the type is used outside this module"
)]
pub struct SearchCategoryParseError {
value: String,
}
impl std::fmt::Display for SearchCategoryParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Could not parse as SearchCategory: {}", self.value)
}
}
impl std::str::FromStr for SearchCategory {
type Err = SearchCategoryParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"all" => Ok(Self::All),
"people" => Ok(Self::People),
"places" => Ok(Self::Places),
"events" => Ok(Self::Events),
"groups" => Ok(Self::Groups),
"wiki" => Ok(Self::Wiki),
"destinations" => Ok(Self::Destinations),
"classifieds" => Ok(Self::Classifieds),
_ => Err(SearchCategoryParseError {
value: s.to_owned(),
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct EventId(pub u32);
impl EventId {
#[must_use]
pub const fn new(id: u32) -> Self {
Self(id)
}
#[must_use]
pub const fn get(self) -> u32 {
self.0
}
}
impl std::fmt::Display for EventId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(feature = "chumsky")]
#[must_use]
pub fn event_id_parser<'src>()
-> impl Parser<'src, &'src str, EventId, chumsky::extra::Err<chumsky::error::Rich<'src, char>>> {
crate::utils::u32_parser().map(EventId)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ClassifiedCategory {
#[default]
AnyCategory,
Shopping,
LandRental,
PropertyRental,
SpecialAttraction,
NewProducts,
Employment,
Wanted,
Service,
Personal,
Unknown(u32),
}
impl ClassifiedCategory {
#[must_use]
pub const fn from_u32(value: u32) -> Self {
match value {
0 => Self::AnyCategory,
1 => Self::Shopping,
2 => Self::LandRental,
3 => Self::PropertyRental,
4 => Self::SpecialAttraction,
5 => Self::NewProducts,
6 => Self::Employment,
7 => Self::Wanted,
8 => Self::Service,
9 => Self::Personal,
other => Self::Unknown(other),
}
}
#[must_use]
pub const fn to_u32(self) -> u32 {
match self {
Self::AnyCategory => 0,
Self::Shopping => 1,
Self::LandRental => 2,
Self::PropertyRental => 3,
Self::SpecialAttraction => 4,
Self::NewProducts => 5,
Self::Employment => 6,
Self::Wanted => 7,
Self::Service => 8,
Self::Personal => 9,
Self::Unknown(value) => value,
}
}
}
impl std::fmt::Display for ClassifiedCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AnyCategory => write!(f, "any"),
Self::Shopping => write!(f, "shopping"),
Self::LandRental => write!(f, "land rental"),
Self::PropertyRental => write!(f, "property rental"),
Self::SpecialAttraction => write!(f, "special attraction"),
Self::NewProducts => write!(f, "new products"),
Self::Employment => write!(f, "employment"),
Self::Wanted => write!(f, "wanted"),
Self::Service => write!(f, "service"),
Self::Personal => write!(f, "personal"),
Self::Unknown(value) => write!(f, "{value}"),
}
}
}
#[derive(Debug, Clone)]
pub struct ClassifiedCategoryParseError {
value: String,
}
impl std::fmt::Display for ClassifiedCategoryParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Could not parse as ClassifiedCategory: {}", self.value)
}
}
impl std::str::FromStr for ClassifiedCategory {
type Err = ClassifiedCategoryParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"any" => Ok(Self::AnyCategory),
"shopping" => Ok(Self::Shopping),
"land rental" => Ok(Self::LandRental),
"property rental" => Ok(Self::PropertyRental),
"special attraction" => Ok(Self::SpecialAttraction),
"new products" => Ok(Self::NewProducts),
"employment" => Ok(Self::Employment),
"wanted" => Ok(Self::Wanted),
"service" => Ok(Self::Service),
"personal" => Ok(Self::Personal),
_ => Err(ClassifiedCategoryParseError {
value: s.to_owned(),
}),
}
}
}
#[cfg(feature = "chumsky")]
#[must_use]
pub fn classified_category_parser<'src>() -> impl Parser<
'src,
&'src str,
ClassifiedCategory,
chumsky::extra::Err<chumsky::error::Rich<'src, char>>,
> {
just("any")
.to(ClassifiedCategory::AnyCategory)
.or(just("shopping").to(ClassifiedCategory::Shopping))
.or(just("land rental").to(ClassifiedCategory::LandRental))
.or(just("property rental").to(ClassifiedCategory::PropertyRental))
.or(just("special attraction").to(ClassifiedCategory::SpecialAttraction))
.or(just("new products").to(ClassifiedCategory::NewProducts))
.or(just("employment").to(ClassifiedCategory::Employment))
.or(just("wanted").to(ClassifiedCategory::Wanted))
.or(just("service").to(ClassifiedCategory::Service))
.or(just("personal").to(ClassifiedCategory::Personal))
}