federation refinement

This commit is contained in:
2026-05-09 13:53:45 +02:00
parent df71748897
commit 470b29c9e1
56 changed files with 1513 additions and 544 deletions

View File

@@ -1,50 +1,28 @@
use crate::errors::DomainError;
use uuid::Uuid;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MovieId(Uuid);
macro_rules! uuid_id {
($name:ident) => {
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct $name(Uuid);
impl MovieId {
pub fn generate() -> Self {
Self(Uuid::new_v4())
}
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
pub fn value(&self) -> Uuid {
self.0
}
impl $name {
pub fn generate() -> Self {
Self(Uuid::new_v4())
}
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
pub fn value(&self) -> Uuid {
self.0
}
}
};
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReviewId(Uuid);
impl ReviewId {
pub fn generate() -> Self {
Self(Uuid::new_v4())
}
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
pub fn value(&self) -> Uuid {
self.0
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UserId(Uuid);
impl UserId {
pub fn generate() -> Self {
Self(Uuid::new_v4())
}
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
pub fn value(&self) -> Uuid {
self.0
}
}
uuid_id!(MovieId);
uuid_id!(ReviewId);
uuid_id!(UserId);
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ExternalMetadataId(String);
@@ -90,15 +68,17 @@ impl PosterPath {
pub struct MovieTitle(String);
impl MovieTitle {
const MAX_LENGTH: usize = 255;
pub fn new(title: String) -> Result<Self, DomainError> {
let trimmed = title.trim();
if trimmed.is_empty() {
Err(DomainError::ValidationError(
"Movie title cannot be empty".into(),
))
} else if trimmed.len() > 255 {
} else if trimmed.len() > Self::MAX_LENGTH {
Err(DomainError::ValidationError(
"Movie title exceeds 255 characters".into(),
format!("Movie title exceeds {} characters", Self::MAX_LENGTH).into(),
))
} else {
Ok(Self(trimmed.to_string()))
@@ -114,11 +94,13 @@ impl MovieTitle {
pub struct Comment(String);
impl Comment {
const MAX_LENGTH: usize = 10_000;
pub fn new(comment: String) -> Result<Self, DomainError> {
let trimmed = comment.trim();
if trimmed.len() > 10_000 {
if trimmed.len() > Self::MAX_LENGTH {
Err(DomainError::ValidationError(
"Comment exceeds 10,000 characters".into(),
format!("Comment exceeds {} characters", Self::MAX_LENGTH).into(),
))
} else {
Ok(Self(trimmed.to_string()))
@@ -189,6 +171,35 @@ impl Email {
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Username(String);
impl Username {
const MIN_LENGTH: usize = 2;
const MAX_LENGTH: usize = 30;
/// Accepts 230 chars: lowercase letters, digits, underscores, hyphens.
/// Lowercases input automatically.
pub fn new(raw: String) -> Result<Self, DomainError> {
let s = raw.trim().to_lowercase();
if s.len() < Self::MIN_LENGTH || s.len() > Self::MAX_LENGTH {
return Err(DomainError::ValidationError(
format!("Username must be {}{} characters", Self::MIN_LENGTH, Self::MAX_LENGTH).into(),
));
}
if !s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') {
return Err(DomainError::ValidationError(
"Username may only contain letters, digits, underscores, and hyphens".into(),
));
}
Ok(Self(s))
}
pub fn value(&self) -> &str {
&self.0
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PasswordHash(String);