1//! The refusal codes that a client sees, and their HTTP status.2//!3//! A refusal comes before admission (spec section 12). It always carries a code4//! from this table, so a client can branch on the code and not on the message.56use std::fmt;78use inf3r_json::{Object, Value};910/// The client's reason for refusing a request.11 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]12pubenumCode {13/// The name is not in the catalog.14UnknownModel,15/// The model does not declare the capability.16UnknownCapability,17/// The model declares the capability. The host does not serve it.18CapabilityMissing,19/// The kind is not permitted. The mime or the role is incorrect.20InvalidPart,21/// An inline part is more than 1 MiB.22PayloadTooLarge,23/// The queue is full.24QueueFull,25/// The ledger cannot reserve the bytes.26NoRoom,27/// The plugin failed to start or to load.28LoadFailed,29/// The plugin stopped during the job.30PluginCrashed,31/// The manifest does not agree with the hardware probe.32Mismatch,33/// The plugin did not obey the protocol.34Protocol,35/// The job exceeded the deadline.36Timeout,37/// Two plugins serve one name.38AmbiguousModel,39 }4041implCode {42/// The word on the wire.43pubfnas_str(self) -> &'static str {44matchself {45Code::UnknownModel => "unknown_model",46Code::UnknownCapability => "unknown_capability",47Code::CapabilityMissing => "capability_missing",48Code::InvalidPart => "invalid_part",49Code::PayloadTooLarge => "payload_too_large",50Code::QueueFull => "queue_full",51Code::NoRoom => "no_room",52Code::LoadFailed => "load_failed",53Code::PluginCrashed => "plugin_crashed",54Code::Mismatch => "mismatch",55Code::Protocol => "protocol",56Code::Timeout => "timeout",57Code::AmbiguousModel => "ambiguous_model",58 }59 }6061/// The HTTP status of this refusal.62pubfnstatus(self) -> u16 {63matchself {64Code::UnknownModel => 404,65Code::UnknownCapability | Code::CapabilityMissing | Code::InvalidPart => 400,66Code::PayloadTooLarge => 413,67Code::QueueFull => 429,68Code::NoRoom | Code::LoadFailed => 503,69Code::PluginCrashed => 502,70Code::Timeout => 504,71Code::Mismatch | Code::Protocol | Code::AmbiguousModel => 500,72 }73 }7475/// Whether the same request may work later.76///77/// A refusal that the operator must fix is not retryable: a retry of an78/// unknown model is another unknown model.79pubfnretryable(self) -> bool {80 matches!(81self,82Code::QueueFull | Code::NoRoom | Code::LoadFailed | Code::PluginCrashed | Code::Timeout83 )84 }8586/// Whether the reply carries `Retry-After`.87pubfnhas_retry_after(self) -> bool {88 matches!(self, Code::QueueFull | Code::NoRoom)89 }9091/// The client code for a wire failure.92///93/// A socket fault is not a client's fault, so it maps to `protocol` and94/// keeps its detail. The wire codes that do have a client meaning keep it.95pubfnfrom_wire(code: inf3r_proto::body::ErrorCode) -> Code {96use inf3r_proto::body::ErrorCodeasWire;97match code {98Wire::UnknownModel => Code::UnknownModel,99Wire::Capability => Code::UnknownCapability,100Wire::Part => Code::InvalidPart,101Wire::Size => Code::PayloadTooLarge,102Wire::Busy => Code::QueueFull,103Wire::Timeout => Code::Timeout,104Wire::Dependency => Code::LoadFailed,105Wire::Version | Wire::Frame | Wire::Stream | Wire::Credit | Wire::Internal => {106Code::Protocol107 }108Wire::Cancelled => Code::Timeout,109 }110 }111 }112113impl fmt::DisplayforCode {114fnfmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {115 f.write_str(self.as_str())116 }117 }118119/// A refusal, with the request that caused it.120 #[derive(Clone, PartialEq, Eq, Debug)]121pubstructRefusal {122/// The code.123pub code: Code,124/// One sentence for a human.125pub message: String,126/// The identifier that the client sent.127pub request_id: String,128 }129130implRefusal {131/// A refusal with an empty message.132pubfnnew(code: Code, request_id: implInto<String>) -> Refusal {133Refusal {134 code,135 message: code.as_str().to_string(),136 request_id: request_id.into(),137 }138 }139140/// The same refusal with a message.141pubfnwith(self, message: implInto<String>) -> Refusal {142Refusal {143 message: message.into(),144 ..self145 }146 }147148/// The JSON body of rule 2 of section 12.149pubfnto_json(&self) -> Value {150Object::new().put("error", self.body()).build()151 }152153fnbody(&self) -> Value {154Object::new()155 .put("code", Value::str(self.code.as_str()))156 .put("message", Value::str(self.message.clone()))157 .put("retryable", Value::Bool(self.code.retryable()))158 .put("request_id", Value::str(self.request_id.clone()))159 .build()160 }161 }