use thiserror::Error;
use crate::format::FormatId;
pub type Result<T> = std::result::Result<T, TranslationError>;
#[derive(Debug, Error)]
pub enum TranslationError {
#[error("invalid JSON: {0}")]
InvalidJson(#[from] serde_json::Error),
#[error("expected {expected} at {path}")]
InvalidType {
path: String,
expected: &'static str,
},
#[error("translation from {from} to {to} is not supported")]
UnsupportedTranslation { from: FormatId, to: FormatId },
#[error("lossy conversion rejected: {0}")]
LossyConversion(String),
#[error("unknown field rejected at {path}")]
UnknownField { path: String },
#[error("invalid value at {path}: {message}")]
InvalidValue { path: String, message: String },
#[error("{0}")]
Other(String),
}
impl TranslationError {
pub const fn kind(&self) -> &'static str {
match self {
Self::InvalidJson(_) => "InvalidJson",
Self::InvalidType { .. } => "InvalidType",
Self::UnsupportedTranslation { .. } => "UnsupportedTranslation",
Self::LossyConversion(_) => "LossyConversion",
Self::UnknownField { .. } => "UnknownField",
Self::InvalidValue { .. } => "InvalidValue",
Self::Other(_) => "Other",
}
}
pub fn unsupported_role(path: impl Into<String>, value: &str) -> Self {
Self::InvalidValue {
path: path.into(),
message: format!(
"Invalid value: {value:?}. Supported message roles are \
system, developer, user, assistant, tool."
),
}
}
}