@@ -1,4 +1,8 @@ |
| 1 | +use std::fmt; |
| 2 | + |
| 1 | 3 | pub const PROTOCOL_VERSION: u16 = 1; |
| 4 | +pub const DEFAULT_RUNTIME_SUBDIR: &str = "garwarp"; |
| 5 | +pub const DEFAULT_CONTROL_SOCKET: &str = "control.sock"; |
| 2 | 6 | |
| 3 | 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 4 | 8 | pub enum HealthStatus { |
@@ -8,12 +12,54 @@ pub enum HealthStatus { |
| 8 | 12 | Stopping, |
| 9 | 13 | } |
| 10 | 14 | |
| 11 | | -#[derive(Debug, Clone)] |
| 15 | +impl HealthStatus { |
| 16 | + #[must_use] |
| 17 | + pub fn as_str(self) -> &'static str { |
| 18 | + match self { |
| 19 | + Self::Starting => "starting", |
| 20 | + Self::Healthy => "healthy", |
| 21 | + Self::Degraded => "degraded", |
| 22 | + Self::Stopping => "stopping", |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + fn parse(input: &str) -> Option<Self> { |
| 27 | + match input { |
| 28 | + "starting" => Some(Self::Starting), |
| 29 | + "healthy" => Some(Self::Healthy), |
| 30 | + "degraded" => Some(Self::Degraded), |
| 31 | + "stopping" => Some(Self::Stopping), |
| 32 | + _ => None, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | 38 | pub enum ControlRequest { |
| 13 | 39 | Status, |
| 40 | + Stop, |
| 14 | 41 | } |
| 15 | 42 | |
| 16 | | -#[derive(Debug, Clone)] |
| 43 | +impl ControlRequest { |
| 44 | + #[must_use] |
| 45 | + pub fn as_line(&self) -> &'static str { |
| 46 | + match self { |
| 47 | + Self::Status => "status", |
| 48 | + Self::Stop => "stop", |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + #[must_use] |
| 53 | + pub fn parse_line(input: &str) -> Option<Self> { |
| 54 | + match input.trim() { |
| 55 | + "status" => Some(Self::Status), |
| 56 | + "stop" => Some(Self::Stop), |
| 57 | + _ => None, |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 17 | 63 | pub struct StatusResponse { |
| 18 | 64 | pub protocol_version: u16, |
| 19 | 65 | pub health: HealthStatus, |
@@ -31,9 +77,154 @@ impl StatusResponse { |
| 31 | 77 | } |
| 32 | 78 | } |
| 33 | 79 | |
| 80 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 81 | +pub enum ControlResponse { |
| 82 | + Status(StatusResponse), |
| 83 | + AckStopping, |
| 84 | + Error { reason: String }, |
| 85 | +} |
| 86 | + |
| 87 | +impl ControlResponse { |
| 88 | + #[must_use] |
| 89 | + pub fn to_line(&self) -> String { |
| 90 | + match self { |
| 91 | + Self::Status(status) => format!( |
| 92 | + "status protocol={} health={} in_flight={}\n", |
| 93 | + status.protocol_version, |
| 94 | + status.health.as_str(), |
| 95 | + status.in_flight_requests |
| 96 | + ), |
| 97 | + Self::AckStopping => "ack stopping\n".to_string(), |
| 98 | + Self::Error { reason } => format!("error reason={}\n", reason), |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + pub fn parse_line(input: &str) -> Result<Self, ParseError> { |
| 103 | + let trimmed = input.trim(); |
| 104 | + let mut parts = trimmed.split_whitespace(); |
| 105 | + |
| 106 | + match parts.next() { |
| 107 | + Some("status") => { |
| 108 | + let mut protocol_version = None; |
| 109 | + let mut health = None; |
| 110 | + let mut in_flight_requests = None; |
| 111 | + |
| 112 | + for part in parts { |
| 113 | + let (key, value) = part |
| 114 | + .split_once('=') |
| 115 | + .ok_or(ParseError::InvalidField(part.to_string()))?; |
| 116 | + match key { |
| 117 | + "protocol" => { |
| 118 | + protocol_version = Some( |
| 119 | + value |
| 120 | + .parse::<u16>() |
| 121 | + .map_err(|_| ParseError::InvalidField(part.to_string()))?, |
| 122 | + ); |
| 123 | + } |
| 124 | + "health" => { |
| 125 | + health = HealthStatus::parse(value); |
| 126 | + if health.is_none() { |
| 127 | + return Err(ParseError::InvalidField(part.to_string())); |
| 128 | + } |
| 129 | + } |
| 130 | + "in_flight" => { |
| 131 | + in_flight_requests = Some( |
| 132 | + value |
| 133 | + .parse::<usize>() |
| 134 | + .map_err(|_| ParseError::InvalidField(part.to_string()))?, |
| 135 | + ); |
| 136 | + } |
| 137 | + _ => return Err(ParseError::InvalidField(part.to_string())), |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + let status = StatusResponse { |
| 142 | + protocol_version: protocol_version |
| 143 | + .ok_or(ParseError::MissingField("protocol"))?, |
| 144 | + health: health.ok_or(ParseError::MissingField("health"))?, |
| 145 | + in_flight_requests: in_flight_requests |
| 146 | + .ok_or(ParseError::MissingField("in_flight"))?, |
| 147 | + }; |
| 148 | + Ok(Self::Status(status)) |
| 149 | + } |
| 150 | + Some("ack") => match parts.next() { |
| 151 | + Some("stopping") => Ok(Self::AckStopping), |
| 152 | + Some(other) => Err(ParseError::UnknownToken(other.to_string())), |
| 153 | + None => Err(ParseError::MissingField("ack")), |
| 154 | + }, |
| 155 | + Some("error") => match parts.next() { |
| 156 | + Some(reason_field) => { |
| 157 | + let (key, value) = reason_field |
| 158 | + .split_once('=') |
| 159 | + .ok_or(ParseError::InvalidField(reason_field.to_string()))?; |
| 160 | + if key != "reason" { |
| 161 | + return Err(ParseError::InvalidField(reason_field.to_string())); |
| 162 | + } |
| 163 | + Ok(Self::Error { |
| 164 | + reason: value.to_string(), |
| 165 | + }) |
| 166 | + } |
| 167 | + None => Err(ParseError::MissingField("reason")), |
| 168 | + }, |
| 169 | + Some(other) => Err(ParseError::UnknownToken(other.to_string())), |
| 170 | + None => Err(ParseError::Empty), |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 176 | +pub enum ParseError { |
| 177 | + Empty, |
| 178 | + MissingField(&'static str), |
| 179 | + InvalidField(String), |
| 180 | + UnknownToken(String), |
| 181 | +} |
| 182 | + |
| 183 | +impl fmt::Display for ParseError { |
| 184 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 185 | + match self { |
| 186 | + Self::Empty => write!(f, "empty input"), |
| 187 | + Self::MissingField(field) => write!(f, "missing field: {field}"), |
| 188 | + Self::InvalidField(field) => write!(f, "invalid field: {field}"), |
| 189 | + Self::UnknownToken(token) => write!(f, "unknown token: {token}"), |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +impl std::error::Error for ParseError {} |
| 195 | + |
| 34 | 196 | #[cfg(test)] |
| 35 | 197 | mod tests { |
| 36 | | - use super::{HealthStatus, PROTOCOL_VERSION, StatusResponse}; |
| 198 | + use super::{ControlRequest, ControlResponse, HealthStatus, PROTOCOL_VERSION, StatusResponse}; |
| 199 | + |
| 200 | + #[test] |
| 201 | + fn request_parse_roundtrip() { |
| 202 | + for request in [ControlRequest::Status, ControlRequest::Stop] { |
| 203 | + let line = request.as_line(); |
| 204 | + let parsed = ControlRequest::parse_line(line); |
| 205 | + assert_eq!(parsed, Some(request)); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + #[test] |
| 210 | + fn response_status_roundtrip() { |
| 211 | + let response = ControlResponse::Status(StatusResponse { |
| 212 | + protocol_version: PROTOCOL_VERSION, |
| 213 | + health: HealthStatus::Healthy, |
| 214 | + in_flight_requests: 7, |
| 215 | + }); |
| 216 | + let line = response.to_line(); |
| 217 | + let parsed = ControlResponse::parse_line(&line).expect("response should parse"); |
| 218 | + assert_eq!(parsed, response); |
| 219 | + } |
| 220 | + |
| 221 | + #[test] |
| 222 | + fn response_ack_roundtrip() { |
| 223 | + let response = ControlResponse::AckStopping; |
| 224 | + let line = response.to_line(); |
| 225 | + let parsed = ControlResponse::parse_line(&line).expect("response should parse"); |
| 226 | + assert_eq!(parsed, response); |
| 227 | + } |
| 37 | 228 | |
| 38 | 229 | #[test] |
| 39 | 230 | fn healthy_response_uses_protocol_version() { |
@@ -42,4 +233,10 @@ mod tests { |
| 42 | 233 | assert_eq!(response.health, HealthStatus::Healthy); |
| 43 | 234 | assert_eq!(response.in_flight_requests, 0); |
| 44 | 235 | } |
| 236 | + |
| 237 | + #[test] |
| 238 | + fn malformed_status_is_rejected() { |
| 239 | + let parsed = ControlResponse::parse_line("status protocol=one health=healthy in_flight=0"); |
| 240 | + assert!(parsed.is_err()); |
| 241 | + } |
| 45 | 242 | } |