pub mod cli;
use serde_json::Value;
pub use cli::CliTransport;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransportErrorKind {
Unavailable,
Refused,
Failed,
NotFound,
Timeout,
Protocol,
Unauthorized,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TransportError {
pub kind: TransportErrorKind,
pub message: String,
pub code: Option<i32>,
pub body: Option<Value>,
}
impl TransportError {
pub fn new(kind: TransportErrorKind, message: impl Into<String>) -> TransportError {
TransportError {
kind,
message: message.into(),
code: None,
body: None,
}
}
pub fn unavailable(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::Unavailable, message)
}
pub fn refused(message: impl Into<String>, body: Option<Value>) -> TransportError {
TransportError {
body,
..TransportError::new(TransportErrorKind::Refused, message)
}
}
pub fn failed(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::Failed, message)
}
pub fn not_found(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::NotFound, message)
}
pub fn protocol(message: impl Into<String>) -> TransportError {
TransportError::new(TransportErrorKind::Protocol, message)
}
pub fn with_code(mut self, code: i32) -> TransportError {
self.code = Some(code);
self
}
pub fn with_body(mut self, body: Value) -> TransportError {
self.body = Some(body);
self
}
pub fn is_refused(&self) -> bool {
self.kind == TransportErrorKind::Refused
}
pub fn headline(&self) -> &'static str {
match self.kind {
TransportErrorKind::Unavailable => "unavailable",
TransportErrorKind::Refused => "refused",
TransportErrorKind::Failed => "failed",
TransportErrorKind::NotFound => "not found",
TransportErrorKind::Timeout => "timed out",
TransportErrorKind::Protocol => "unexpected answer",
TransportErrorKind::Unauthorized => "not authorized",
}
}
pub fn reasons(&self) -> Vec<String> {
let Some(body) = &self.body else {
return Vec::new();
};
let mut out: Vec<String> = body
.get("delete_blockers")
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(Value::as_str)
.map(str::to_string)
.collect()
})
.unwrap_or_default();
if let Some(e) = body.get("error").and_then(Value::as_str) {
if !e.is_empty() && !out.iter().any(|o| o == e) {
out.push(e.to_string());
}
}
out
}
}
impl std::fmt::Display for TransportError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.headline(), self.message)
}
}
impl std::error::Error for TransportError {}
pub trait ConsoleTransport: Send + Sync {
fn host_profile(&self) -> Result<Value, TransportError>;
fn engines_status(&self, probe: bool) -> Result<Value, TransportError>;
fn models_catalog(
&self,
q: &str,
engine: Option<&str>,
fits_only: bool,
) -> Result<Value, TransportError>;
fn models_installed(&self, provider: Option<&str>) -> Result<Value, TransportError>;
fn start_download(&self, provider: &str, artifact: &str) -> Result<Value, TransportError>;
fn delete_model(
&self,
provider: &str,
artifact: &str,
force: bool,
) -> Result<Value, TransportError>;
fn engine_install(&self, id: &str, dry_run: bool) -> Result<Value, TransportError>;
fn job(&self, id: &str) -> Result<Value, TransportError>;
fn cancel_job(&self, id: &str) -> Result<Value, TransportError>;
fn host_label(&self) -> String {
"this machine".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn reasons_collect_blockers_and_error() {
let e = TransportError::refused(
"model is loaded",
Some(json!({"delete_blockers": ["loaded"], "error": "unload it first"})),
);
assert!(e.is_refused());
assert_eq!(e.reasons(), vec!["loaded", "unload it first"]);
assert_eq!(e.to_string(), "refused: model is loaded");
assert!(TransportError::failed("x").reasons().is_empty());
}
}