use std::fmt;
use serde::{Deserialize, Deserializer, Serialize};
use crate::error::DomainError;
use super::slug;
const MAX_LEN: usize = 512;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct UnavailabilityReason(String);
impl UnavailabilityReason {
pub fn new(raw: impl AsRef<str>) -> Result<Self, DomainError> {
slug::text("unavailability_reason", raw.as_ref(), MAX_LEN).map(Self)
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_inner(self) -> String {
self.0
}
}
impl fmt::Display for UnavailabilityReason {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0)
}
}
impl TryFrom<&str> for UnavailabilityReason {
type Error = DomainError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl TryFrom<String> for UnavailabilityReason {
type Error = DomainError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl<'de> Deserialize<'de> for UnavailabilityReason {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
}
}