use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ::jumo_derive::Jumo)]
#[jumo(kind = "struct", domain = "Reporting", module = "Reporting.Protocol")]
#[serde(deny_unknown_fields)]
pub struct AgentUplinkState {
pub enabled: bool,
pub kind: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub target: Option<String>,
pub source: String,
pub output_write_failing: bool,
}
#[cfg(test)]
mod state_tests {
use super::*;
#[test]
fn a_local_file_output_state_omits_the_target() {
let state = AgentUplinkState {
enabled: true,
kind: "file".to_string(),
target: None,
source: "local".to_string(),
output_write_failing: false,
};
let json = serde_json::to_string(&state).expect("encode");
assert!(!json.contains("target"), "{json}");
let back: AgentUplinkState = serde_json::from_str(&json).expect("decode");
assert_eq!(back, state);
}
#[test]
fn a_grant_driven_tcp_state_carries_the_target() {
let state = AgentUplinkState {
enabled: true,
kind: "tcp".to_string(),
target: Some("10.0.1.9:9000".to_string()),
source: "grant".to_string(),
output_write_failing: true,
};
let json = serde_json::to_string(&state).expect("encode");
let back: AgentUplinkState = serde_json::from_str(&json).expect("decode");
assert_eq!(back, state);
assert!(
serde_json::from_str::<AgentUplinkState>(
r#"{"enabled":true,"kind":"tcp",
"source":"local","output_write_failing":false,"extra":1}"#
)
.is_err()
);
}
}