use std::str::FromStr;
use snafu::{Snafu, ensure};
use crate::utils::ExampleData;
pub const EVENT_TITLE_MAX_LENGTH: usize = 255;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, derive_more::Display)]
#[cfg_attr(
feature = "diesel",
derive(
opentalk_diesel_newtype::DieselNewtype,
diesel::expression::AsExpression,
diesel::deserialize::FromSqlRow
)
)]
#[cfg_attr(
feature = "diesel",
diesel(sql_type = diesel::sql_types::Text)
)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde_with::DeserializeFromStr)
)]
pub struct EventTitle(String);
impl EventTitle {
pub fn from_str_lossy(s: &str) -> Self {
Self(s.chars().take(EVENT_TITLE_MAX_LENGTH).collect())
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn sanitized_for_filename(&self, max_length: usize) -> String {
fn is_allowed_char(c: char) -> bool {
c.is_alphanumeric() || ['.', '_', '-', ' '].contains(&c)
}
fn to_valid_char(c: char) -> char {
if is_allowed_char(c) { c } else { '_' }
}
self.0.chars().take(max_length).map(to_valid_char).collect()
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[cfg(feature = "utoipa")]
mod impl_utoipa {
use serde_json::json;
use utoipa::{
PartialSchema, ToSchema,
openapi::{ObjectBuilder, RefOr, Schema, Type},
};
use super::{EVENT_TITLE_MAX_LENGTH, EventTitle};
use crate::utils::ExampleData as _;
impl PartialSchema for EventTitle {
fn schema() -> RefOr<Schema> {
ObjectBuilder::new()
.schema_type(Type::String)
.description(Some("The title of an event"))
.max_length(Some(EVENT_TITLE_MAX_LENGTH))
.examples([json!(EventTitle::example_data())])
.into()
}
}
impl ToSchema for EventTitle {
fn schemas(schemas: &mut Vec<(String, RefOr<Schema>)>) {
schemas.push((Self::name().into(), Self::schema()));
}
}
}
impl ExampleData for EventTitle {
fn example_data() -> Self {
Self("Team Event".to_string())
}
}
#[derive(Debug, Snafu)]
pub enum ParseEventTitleError {
#[snafu(display("Event title must not be longer than {max_length} characters"))]
TooLong {
max_length: usize,
},
}
impl FromStr for EventTitle {
type Err = ParseEventTitleError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
ensure!(
s.len() <= EVENT_TITLE_MAX_LENGTH,
TooLongSnafu {
max_length: EVENT_TITLE_MAX_LENGTH
}
);
Ok(Self(s.to_string()))
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::{EventTitle, ParseEventTitleError};
use crate::events::event_title::EVENT_TITLE_MAX_LENGTH;
#[test]
fn parse() {
assert_eq!(
"hello".parse::<EventTitle>().unwrap(),
EventTitle("hello".to_string())
);
assert_eq!(
"".parse::<EventTitle>().unwrap(),
EventTitle("".to_string())
);
assert_eq!(
"_".parse::<EventTitle>().unwrap(),
EventTitle("_".to_string())
);
assert_eq!(
"hello world".parse::<EventTitle>().unwrap(),
EventTitle("hello world".to_string())
);
assert_eq!(
"🚀".parse::<EventTitle>().unwrap(),
EventTitle("🚀".to_string())
);
let longest: String = "x".repeat(255);
assert_eq!(longest.parse::<EventTitle>().unwrap(), EventTitle(longest));
}
#[test]
fn parse_invalid() {
let too_long: String = "x".repeat(256);
assert!(matches!(
too_long.parse::<EventTitle>(),
Err(ParseEventTitleError::TooLong { max_length: 255 })
));
}
#[test]
fn from_str_lossy() {
assert_eq!(
EventTitle::from_str_lossy("hello"),
EventTitle("hello".to_string())
);
let too_long: String = "x".repeat(EVENT_TITLE_MAX_LENGTH + 1);
let title = EventTitle::from_str_lossy(&too_long);
assert_eq!(title.0.len(), EVENT_TITLE_MAX_LENGTH);
}
}