use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ArticleId(String);
impl ArticleId {
pub fn new(value: String) -> Result<Self> {
Self::validate(&value)?;
Ok(Self(value))
}
pub(crate) fn new_unchecked(value: String) -> Self {
Self(value)
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
pub fn starts_with(&self, prefix: &str) -> bool {
self.0.starts_with(prefix)
}
pub fn ends_with(&self, suffix: &str) -> bool {
self.0.ends_with(suffix)
}
fn validate(value: &str) -> Result<()> {
if value.is_empty() {
return Err(crate::error::AllSourceError::InvalidInput(
"Article ID cannot be empty".to_string(),
));
}
if value.len() > 256 {
return Err(crate::error::AllSourceError::InvalidInput(format!(
"Article ID cannot exceed 256 characters, got {}",
value.len()
)));
}
if !value
.chars()
.all(|c| c.is_alphanumeric() || c == '-' || c == '_')
{
return Err(crate::error::AllSourceError::InvalidInput(format!(
"Article ID '{value}' contains invalid characters. Only alphanumeric, hyphens, and underscores allowed"
)));
}
Ok(())
}
}
impl fmt::Display for ArticleId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl TryFrom<&str> for ArticleId {
type Error = crate::error::AllSourceError;
fn try_from(value: &str) -> Result<Self> {
ArticleId::new(value.to_string())
}
}
impl TryFrom<String> for ArticleId {
type Error = crate::error::AllSourceError;
fn try_from(value: String) -> Result<Self> {
ArticleId::new(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_valid_article_ids() {
let article_id = ArticleId::new("article123".to_string());
assert!(article_id.is_ok());
assert_eq!(article_id.unwrap().as_str(), "article123");
let article_id = ArticleId::new("my-awesome-article".to_string());
assert!(article_id.is_ok());
let article_id = ArticleId::new("my_awesome_article".to_string());
assert!(article_id.is_ok());
let article_id = ArticleId::new("MyAwesomeArticle".to_string());
assert!(article_id.is_ok());
let article_id = ArticleId::new("how-to-scale-to-1M-users_2024".to_string());
assert!(article_id.is_ok());
}
#[test]
fn test_reject_empty_article_id() {
let result = ArticleId::new(String::new());
assert!(result.is_err());
if let Err(e) = result {
assert!(e.to_string().contains("cannot be empty"));
}
}
#[test]
fn test_reject_too_long_article_id() {
let long_id = "a".repeat(257);
let result = ArticleId::new(long_id);
assert!(result.is_err());
if let Err(e) = result {
assert!(e.to_string().contains("cannot exceed 256 characters"));
}
}
#[test]
fn test_accept_max_length_article_id() {
let max_id = "a".repeat(256);
let result = ArticleId::new(max_id);
assert!(result.is_ok());
}
#[test]
fn test_reject_invalid_characters() {
let result = ArticleId::new("article 123".to_string());
assert!(result.is_err());
let result = ArticleId::new("article@123".to_string());
assert!(result.is_err());
let result = ArticleId::new("article.123".to_string());
assert!(result.is_err());
let result = ArticleId::new("article/123".to_string());
assert!(result.is_err());
let result = ArticleId::new("article?query=1".to_string());
assert!(result.is_err());
}
#[test]
fn test_display_trait() {
let article_id = ArticleId::new("test-article".to_string()).unwrap();
assert_eq!(format!("{article_id}"), "test-article");
}
#[test]
fn test_try_from_str() {
let article_id: Result<ArticleId> = "valid-article".try_into();
assert!(article_id.is_ok());
assert_eq!(article_id.unwrap().as_str(), "valid-article");
let invalid: Result<ArticleId> = "".try_into();
assert!(invalid.is_err());
}
#[test]
fn test_try_from_string() {
let article_id: Result<ArticleId> = "valid-article".to_string().try_into();
assert!(article_id.is_ok());
let invalid: Result<ArticleId> = String::new().try_into();
assert!(invalid.is_err());
}
#[test]
fn test_into_inner() {
let article_id = ArticleId::new("test-article".to_string()).unwrap();
let inner = article_id.into_inner();
assert_eq!(inner, "test-article");
}
#[test]
fn test_starts_with() {
let article_id = ArticleId::new("kubernetes-tutorial".to_string()).unwrap();
assert!(article_id.starts_with("kubernetes"));
assert!(!article_id.starts_with("docker"));
}
#[test]
fn test_ends_with() {
let article_id = ArticleId::new("kubernetes-tutorial".to_string()).unwrap();
assert!(article_id.ends_with("tutorial"));
assert!(!article_id.ends_with("guide"));
}
#[test]
fn test_equality() {
let id1 = ArticleId::new("article-a".to_string()).unwrap();
let id2 = ArticleId::new("article-a".to_string()).unwrap();
let id3 = ArticleId::new("article-b".to_string()).unwrap();
assert_eq!(id1, id2);
assert_ne!(id1, id3);
}
#[test]
fn test_cloning() {
let id1 = ArticleId::new("article".to_string()).unwrap();
let id2 = id1.clone();
assert_eq!(id1, id2);
}
#[test]
fn test_hash_consistency() {
use std::collections::HashSet;
let id1 = ArticleId::new("article".to_string()).unwrap();
let id2 = ArticleId::new("article".to_string()).unwrap();
let mut set = HashSet::new();
set.insert(id1);
assert!(set.contains(&id2));
}
#[test]
fn test_serde_serialization() {
let article_id = ArticleId::new("test-article".to_string()).unwrap();
let json = serde_json::to_string(&article_id).unwrap();
assert_eq!(json, "\"test-article\"");
let deserialized: ArticleId = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, article_id);
}
#[test]
fn test_new_unchecked() {
let article_id = ArticleId::new_unchecked("invalid chars!@#".to_string());
assert_eq!(article_id.as_str(), "invalid chars!@#");
}
}