use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
pub const MODE_PATH: &str = "/data/api/v1/mode";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModeRecord {
#[serde(default)]
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(
rename = "resourceCount",
alias = "resource_count",
default,
skip_serializing_if = "Option::is_none"
)]
pub resource_count: Option<i64>,
#[serde(flatten)]
pub extra: BTreeMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ModeCreate {
pub name: String,
pub title: String,
pub description: String,
}
#[derive(Debug, Clone, Default, Deserialize)]
pub(crate) struct GatewayModeProbe {
#[serde(rename = "deploymentMode", default)]
pub(crate) deployment_mode: Option<String>,
}
pub fn normalize_deployment_mode(raw: Option<String>) -> Option<String> {
raw.filter(|value| !value.trim().is_empty())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalize_table() {
assert_eq!(normalize_deployment_mode(None), None);
assert_eq!(normalize_deployment_mode(Some(String::new())), None);
assert_eq!(normalize_deployment_mode(Some(" ".to_string())), None);
assert_eq!(normalize_deployment_mode(Some("\t\n".to_string())), None);
assert_eq!(
normalize_deployment_mode(Some("production".to_string())),
Some("production".to_string())
);
assert_eq!(
normalize_deployment_mode(Some(" Production ".to_string())),
Some(" Production ".to_string())
);
}
#[test]
fn probe_reads_null_absent_and_string() {
let null: GatewayModeProbe =
serde_json::from_str(r#"{"deploymentMode":null,"name":"x"}"#).expect("null");
assert_eq!(null.deployment_mode, None);
let absent: GatewayModeProbe = serde_json::from_str(r#"{"name":"x"}"#).expect("absent");
assert_eq!(absent.deployment_mode, None);
let empty: GatewayModeProbe =
serde_json::from_str(r#"{"deploymentMode":""}"#).expect("empty");
assert_eq!(empty.deployment_mode.as_deref(), Some(""));
}
#[test]
fn captured_mode_body_parses() {
let record: ModeRecord = serde_json::from_str(
"{\n \"name\": \"production\",\n \"title\": \"Production\",\n \"description\": \"ign-deploy-rig\",\n \"resourceCount\": 0\n}",
)
.expect("C-mode parses");
assert_eq!(record.name, "production");
assert_eq!(record.title.as_deref(), Some("Production"));
assert_eq!(record.resource_count, Some(0));
assert!(record.extra.is_empty());
}
#[test]
fn create_body_field_order_is_the_wire_order() {
let body = serde_json::to_string(&ModeCreate {
name: "production".to_string(),
title: "Production".to_string(),
description: "x".to_string(),
})
.expect("serializes");
assert_eq!(
body,
r#"{"name":"production","title":"Production","description":"x"}"#
);
}
}