aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-localhttp / src / status.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use std::fmt; |
| 17 | |
| 18 | /// A response status code (100 to 999). |
| 19 | /// |
| 20 | /// `Display` prints `"<code> <reason>"`, the same text the `http` crate's |
| 21 | /// `StatusCode` prints (http-1.5.0 src/status.rs:219-227), so error messages |
| 22 | /// that format a status keep their wording after migrating from reqwest. |
| 23 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 24 | pub struct StatusCode(u16); |
| 25 | |
| 26 | impl StatusCode { |
| 27 | /// Accepts 100 to 999, the range a three digit status line can carry. |
| 28 | pub fn from_u16(code: u16) -> Option<StatusCode> { |
| 29 | (100..=999).contains(&code).then_some(StatusCode(code)) |
| 30 | } |
| 31 | |
| 32 | pub fn as_u16(self) -> u16 { |
| 33 | self.0 |
| 34 | } |
| 35 | |
| 36 | /// 200 to 299. |
| 37 | pub fn is_success(self) -> bool { |
| 38 | (200..300).contains(&self.0) |
| 39 | } |
| 40 | |
| 41 | /// 100 to 199. |
| 42 | pub fn is_informational(self) -> bool { |
| 43 | (100..200).contains(&self.0) |
| 44 | } |
| 45 | |
| 46 | /// The reason phrase table of the `http` crate (http-1.5.0 src/status.rs:329-524). |
| 47 | pub fn canonical_reason(self) -> Option<&'static str> { |
| 48 | Some(match self.0 { |
| 49 | 100 => "Continue", |
| 50 | 101 => "Switching Protocols", |
| 51 | 102 => "Processing", |
| 52 | 103 => "Early Hints", |
| 53 | 200 => "OK", |
| 54 | 201 => "Created", |
| 55 | 202 => "Accepted", |
| 56 | 203 => "Non Authoritative Information", |
| 57 | 204 => "No Content", |
| 58 | 205 => "Reset Content", |
| 59 | 206 => "Partial Content", |
| 60 | 207 => "Multi-Status", |
| 61 | 208 => "Already Reported", |
| 62 | 226 => "IM Used", |
| 63 | 300 => "Multiple Choices", |
| 64 | 301 => "Moved Permanently", |
| 65 | 302 => "Found", |
| 66 | 303 => "See Other", |
| 67 | 304 => "Not Modified", |
| 68 | 305 => "Use Proxy", |
| 69 | 307 => "Temporary Redirect", |
| 70 | 308 => "Permanent Redirect", |
| 71 | 400 => "Bad Request", |
| 72 | 401 => "Unauthorized", |
| 73 | 402 => "Payment Required", |
| 74 | 403 => "Forbidden", |
| 75 | 404 => "Not Found", |
| 76 | 405 => "Method Not Allowed", |
| 77 | 406 => "Not Acceptable", |
| 78 | 407 => "Proxy Authentication Required", |
| 79 | 408 => "Request Timeout", |
| 80 | 409 => "Conflict", |
| 81 | 410 => "Gone", |
| 82 | 411 => "Length Required", |
| 83 | 412 => "Precondition Failed", |
| 84 | 413 => "Payload Too Large", |
| 85 | 414 => "URI Too Long", |
| 86 | 415 => "Unsupported Media Type", |
| 87 | 416 => "Range Not Satisfiable", |
| 88 | 417 => "Expectation Failed", |
| 89 | 418 => "I'm a teapot", |
| 90 | 421 => "Misdirected Request", |
| 91 | 422 => "Unprocessable Entity", |
| 92 | 423 => "Locked", |
| 93 | 424 => "Failed Dependency", |
| 94 | 425 => "Too Early", |
| 95 | 426 => "Upgrade Required", |
| 96 | 428 => "Precondition Required", |
| 97 | 429 => "Too Many Requests", |
| 98 | 431 => "Request Header Fields Too Large", |
| 99 | 451 => "Unavailable For Legal Reasons", |
| 100 | 500 => "Internal Server Error", |
| 101 | 501 => "Not Implemented", |
| 102 | 502 => "Bad Gateway", |
| 103 | 503 => "Service Unavailable", |
| 104 | 504 => "Gateway Timeout", |
| 105 | 505 => "HTTP Version Not Supported", |
| 106 | 506 => "Variant Also Negotiates", |
| 107 | 507 => "Insufficient Storage", |
| 108 | 508 => "Loop Detected", |
| 109 | 510 => "Not Extended", |
| 110 | 511 => "Network Authentication Required", |
| 111 | _ => return None, |
| 112 | }) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl fmt::Display for StatusCode { |
| 117 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 118 | write!( |
| 119 | f, |
| 120 | "{} {}", |
| 121 | self.0, |
| 122 | self.canonical_reason().unwrap_or("<unknown status code>") |
| 123 | ) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[cfg(test)] |
| 128 | mod tests { |
| 129 | use super::*; |
| 130 | |
| 131 | #[test] |
| 132 | fn display_matches_the_http_crate_wording() { |
| 133 | assert_eq!(StatusCode(404).to_string(), "404 Not Found"); |
| 134 | assert_eq!(StatusCode(500).to_string(), "500 Internal Server Error"); |
| 135 | assert_eq!(StatusCode(299).to_string(), "299 <unknown status code>"); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn success_is_exactly_the_2xx_range() { |
| 140 | assert!(!StatusCode(199).is_success()); |
| 141 | assert!(StatusCode(200).is_success()); |
| 142 | assert!(StatusCode(299).is_success()); |
| 143 | assert!(!StatusCode(300).is_success()); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn from_u16_rejects_codes_outside_three_digits() { |
| 148 | assert!(StatusCode::from_u16(99).is_none()); |
| 149 | assert!(StatusCode::from_u16(1000).is_none()); |
| 150 | assert_eq!(StatusCode::from_u16(100).map(StatusCode::as_u16), Some(100)); |
| 151 | } |
| 152 | } |