use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum DiagnosticLevel {
Warning,
Error,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ConfigDiagnostic {
pub level: DiagnosticLevel,
pub code: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub component: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub field: Option<String>,
pub message: String,
}
pub fn http_error_message(status: u16, message: &str) -> Result<String, String> {
if !(400..=599).contains(&status) {
return Err("plugin HTTP error status must be between 400 and 599".into());
}
Ok(format!("HTTP {status}: {message}"))
}
pub fn parse_http_error_message(message: &str) -> Option<(u16, &str)> {
let suffix = message.strip_prefix("HTTP ")?;
let (code, body) = suffix.split_once(": ")?;
if code.len() != 3 || !code.bytes().all(|byte| byte.is_ascii_digit()) {
return None;
}
let status = code.parse().ok()?;
(400..=599).contains(&status).then_some((status, body))
}
#[cfg(test)]
#[path = "../tests/unit/plugin_http_error_tests.rs"]
mod http_error_tests;