use thiserror::Error;
pub type RecommendResult<T> = Result<T, RecommendError>;
#[derive(Debug, Error)]
pub enum RecommendError {
#[error("User not found: {0}")]
UserNotFound(uuid::Uuid),
#[error("Content not found: {0}")]
ContentNotFound(uuid::Uuid),
#[error("Insufficient data: {0}")]
InsufficientData(String),
#[error("Invalid rating: {0}")]
InvalidRating(f32),
#[error("Invalid similarity score: {0}")]
InvalidSimilarity(f32),
#[error("Matrix computation error: {0}")]
MatrixError(String),
#[error("Profile error: {0}")]
ProfileError(String),
#[error("History tracking error: {0}")]
HistoryError(String),
#[error("Trending detection error: {0}")]
TrendingError(String),
#[error("Personalization error: {0}")]
PersonalizationError(String),
#[error("Diversity enforcement error: {0}")]
DiversityError(String),
#[error("Ranking error: {0}")]
RankingError(String),
#[error("Explanation generation error: {0}")]
ExplanationError(String),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("Serialization error: {0}")]
SerializationError(#[from] serde_json::Error),
#[error("Rate limited: {0}")]
RateLimited(String),
#[cfg(feature = "onnx")]
#[error("ML error: {0}")]
Ml(#[from] oximedia_ml::MlError),
#[error("{0}")]
Other(String),
}
impl RecommendError {
#[must_use]
pub fn insufficient_data(msg: impl Into<String>) -> Self {
Self::InsufficientData(msg.into())
}
#[must_use]
pub fn matrix_error(msg: impl Into<String>) -> Self {
Self::MatrixError(msg.into())
}
#[must_use]
pub fn profile_error(msg: impl Into<String>) -> Self {
Self::ProfileError(msg.into())
}
#[must_use]
pub fn history_error(msg: impl Into<String>) -> Self {
Self::HistoryError(msg.into())
}
#[must_use]
pub fn trending_error(msg: impl Into<String>) -> Self {
Self::TrendingError(msg.into())
}
#[must_use]
pub fn personalization_error(msg: impl Into<String>) -> Self {
Self::PersonalizationError(msg.into())
}
#[must_use]
pub fn diversity_error(msg: impl Into<String>) -> Self {
Self::DiversityError(msg.into())
}
#[must_use]
pub fn ranking_error(msg: impl Into<String>) -> Self {
Self::RankingError(msg.into())
}
#[must_use]
pub fn explanation_error(msg: impl Into<String>) -> Self {
Self::ExplanationError(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use uuid::Uuid;
#[test]
fn test_user_not_found_error() {
let id = Uuid::new_v4();
let error = RecommendError::UserNotFound(id);
assert!(error.to_string().contains("User not found"));
}
#[test]
fn test_insufficient_data_error() {
let error = RecommendError::insufficient_data("Not enough ratings");
assert!(error.to_string().contains("Insufficient data"));
}
#[test]
fn test_invalid_rating_error() {
let error = RecommendError::InvalidRating(-1.0);
assert!(error.to_string().contains("Invalid rating"));
}
}