crates/run/core/src/refusal.rs 4.1 KiB 118 lines Raw
1 //! The reasons that the core refuses a job.2 //!3 //! This vocabulary describes a decision of the core. It is not the client's4 //! HTTP table and it is not the wire's `ErrorCode`: the web turns a core code5 //! into the client code of one API, and the plugin's wire code is translated6 //! here on the way in. Keeping the two apart is what lets the core stay a7 //! library with no HTTP and no JSON.8 9 use std::fmt;10 11 /// The reason that the core refused a request or ended a job.12 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]13 pub enum RefusalCode {14     /// The name is not in the catalog.15     UnknownModel,16     /// The model does not declare the capability.17     UnknownCapability,18     /// The model declares the capability. The host does not serve it.19     CapabilityMissing,20     /// The kind is not permitted. The mime or the role is incorrect.21     InvalidPart,22     /// An inline part is more than the body limit.23     PayloadTooLarge,24     /// The queue is full.25     QueueFull,26     /// The ledger cannot reserve the bytes.27     NoRoom,28     /// The plugin failed to start or to load.29     LoadFailed,30     /// The plugin stopped during the job.31     PluginCrashed,32     /// The manifest does not agree with the hardware probe.33     Mismatch,34     /// The plugin did not obey the protocol.35     Protocol,36     /// The job exceeded its deadline.37     Timeout,38     /// Two plugins serve one name.39     AmbiguousModel,40 }41 42 impl RefusalCode {43     /// The word that the core writes and the web copies.44     pub fn as_str(self) -> &'static str {45         match self {46             RefusalCode::UnknownModel => "unknown_model",47             RefusalCode::UnknownCapability => "unknown_capability",48             RefusalCode::CapabilityMissing => "capability_missing",49             RefusalCode::InvalidPart => "invalid_part",50             RefusalCode::PayloadTooLarge => "payload_too_large",51             RefusalCode::QueueFull => "queue_full",52             RefusalCode::NoRoom => "no_room",53             RefusalCode::LoadFailed => "load_failed",54             RefusalCode::PluginCrashed => "plugin_crashed",55             RefusalCode::Mismatch => "mismatch",56             RefusalCode::Protocol => "protocol",57             RefusalCode::Timeout => "timeout",58             RefusalCode::AmbiguousModel => "ambiguous_model",59         }60     }61 62     /// The core code for a wire failure.63     ///64     /// A socket fault is not the client's fault, so it maps to `protocol` and65     /// keeps its detail. The wire codes that do have a client meaning keep it.66     pub fn from_wire(code: inf3r_proto::body::ErrorCode) -> RefusalCode {67         use inf3r_proto::body::ErrorCode as Wire;68         match code {69             Wire::UnknownModel => RefusalCode::UnknownModel,70             Wire::Capability => RefusalCode::UnknownCapability,71             Wire::Part => RefusalCode::InvalidPart,72             Wire::Size => RefusalCode::PayloadTooLarge,73             Wire::Busy => RefusalCode::QueueFull,74             Wire::Timeout => RefusalCode::Timeout,75             Wire::Dependency => RefusalCode::LoadFailed,76             Wire::Version | Wire::Frame | Wire::Stream | Wire::Credit | Wire::Internal => {77                 RefusalCode::Protocol78             }79             Wire::Cancelled => RefusalCode::Timeout,80         }81     }82 }83 84 impl fmt::Display for RefusalCode {85     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {86         f.write_str(self.as_str())87     }88 }89 90 /// A refusal with the request that caused it.91 #[derive(Clone, PartialEq, Eq, Debug)]92 pub struct Refusal {93     /// The core code.94     pub code: RefusalCode,95     /// One sentence for a human.96     pub message: String,97     /// The identifier that the client sent.98     pub request_id: String,99 }100 101 impl Refusal {102     /// A refusal with the code's word as its first message.103     pub fn new(code: RefusalCode, request_id: impl Into<String>) -> Refusal {104         Refusal {105             code,106             message: code.as_str().to_string(),107             request_id: request_id.into(),108         }109     }110 111     /// The same refusal with a message.112     pub fn with(self, message: impl Into<String>) -> Refusal {113         Refusal {114             message: message.into(),115             ..self116         }117     }118 }