use kindling_provider::ProviderError;
use kindling_store::StoreError;
use kindling_types::{Id, ValidationError};
#[derive(Debug, thiserror::Error)]
pub enum ServiceError {
#[error(transparent)]
Store(#[from] StoreError),
#[error(transparent)]
Provider(#[from] ProviderError),
#[error("json (de)serialization error: {0}")]
Json(#[from] serde_json::Error),
#[error("validation failed: {}", format_validation(.0))]
Validation(Vec<ValidationError>),
#[error("conflict: {0}")]
Conflict(String),
#[error("capsule {0} not found")]
NotFound(Id),
#[error("capsule {0} is already closed")]
AlreadyClosed(Id),
}
pub type ServiceResult<T> = Result<T, ServiceError>;
fn format_validation(errors: &[ValidationError]) -> String {
errors
.iter()
.map(|e| e.message.as_str())
.collect::<Vec<_>>()
.join(", ")
}