use std::fmt;
use serde::Serialize;
use super::FORMAT;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorType {
InvalidOptions,
UnknownIndex,
UnknownSubset,
WidthMismatch,
TooManyValues,
RadiusTooLarge,
NotReady,
Conflict,
LogFull,
InvalidTask,
UnsupportedModel,
Internal,
}
pub const CONFLICT_IDS: usize = 100;
impl ErrorType {
pub const ALL: [Self; 12] = [
Self::InvalidOptions,
Self::UnknownIndex,
Self::UnknownSubset,
Self::WidthMismatch,
Self::TooManyValues,
Self::RadiusTooLarge,
Self::NotReady,
Self::Conflict,
Self::LogFull,
Self::InvalidTask,
Self::UnsupportedModel,
Self::Internal,
];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::InvalidOptions => "invalid_options",
Self::UnknownIndex => "unknown_index",
Self::UnknownSubset => "unknown_subset",
Self::WidthMismatch => "width_mismatch",
Self::TooManyValues => "too_many_values",
Self::RadiusTooLarge => "radius_too_large",
Self::NotReady => "not_ready",
Self::Conflict => "conflict",
Self::LogFull => "log_full",
Self::InvalidTask => "invalid_task",
Self::UnsupportedModel => "unsupported_model",
Self::Internal => "internal",
}
}
}
impl fmt::Display for ErrorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ServiceError {
pub error_type: ErrorType,
pub message: String,
pub max_radius: Option<u32>,
pub state: Option<&'static str>,
pub fraction: Option<f64>,
pub ids: Option<Vec<u64>>,
pub count: Option<u64>,
}
pub const SUPPORTED_MODELS: &[&str] = &["legit-hamming"];
#[derive(Serialize)]
struct ErrorObject<'a> {
format: u32,
error_type: &'static str,
error: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
max_radius: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
state: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
fraction: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
ids: Option<&'a [u64]>,
#[serde(skip_serializing_if = "Option::is_none")]
count: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
supported_models: Option<&'static [&'static str]>,
}
impl ServiceError {
#[must_use]
pub fn new(error_type: ErrorType, message: impl Into<String>) -> Self {
Self {
error_type,
message: message.into(),
max_radius: None,
state: None,
fraction: None,
ids: None,
count: None,
}
}
pub(crate) fn invalid(message: impl Into<String>) -> Self {
Self::new(ErrorType::InvalidOptions, message)
}
#[must_use]
pub fn to_json(&self) -> String {
serde_json::to_string(&ErrorObject {
format: FORMAT,
error_type: self.error_type.as_str(),
error: &self.message,
max_radius: self.max_radius,
state: self.state,
fraction: self.fraction,
ids: self.ids.as_deref(),
count: self.count,
supported_models: self.supported_models(),
})
.unwrap_or_default()
}
#[must_use]
pub fn fields(&self) -> Vec<(&'static str, String)> {
let mut fields = Vec::new();
if let Some(max_radius) = self.max_radius {
fields.push(("max_radius", max_radius.to_string()));
}
if let Some(state) = self.state {
fields.push(("state", state.to_owned()));
}
if let Some(fraction) = self.fraction {
fields.push(("fraction", fraction.to_string()));
}
if let Some(ids) = &self.ids {
fields.push(("ids", serde_json::to_string(ids).unwrap_or_default()));
}
if let Some(count) = self.count {
fields.push(("count", count.to_string()));
}
if let Some(models) = self.supported_models() {
fields.push(("supported_models", serde_json::to_string(models).unwrap_or_default()));
}
fields
}
fn supported_models(&self) -> Option<&'static [&'static str]> {
(self.error_type == ErrorType::UnsupportedModel).then_some(SUPPORTED_MODELS)
}
}
impl fmt::Display for ServiceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.error_type, self.message)
}
}
impl std::error::Error for ServiceError {}