use serde::{Deserialize, Serialize};
pub const CHANNEL_VERSION: &str = "1";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum ChildMessage {
Ready,
Metric {
name: String,
value: f64,
},
ActionReply {
action: String,
body: String,
#[serde(skip_serializing_if = "Option::is_none", default)]
id: Option<u64>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum ShepherdMessage {
Shutdown,
Action {
name: String,
#[serde(skip_serializing_if = "Option::is_none", default)]
params: Option<String>,
id: u64,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ready_wire_fixture_round_trips() {
let fixture = r#"{"kind":"ready"}"#;
assert_eq!(
serde_json::from_str::<ChildMessage>(fixture).unwrap(),
ChildMessage::Ready
);
assert_eq!(
serde_json::to_string(&ChildMessage::Ready).unwrap(),
fixture
);
}
#[test]
fn metric_wire_fixture_round_trips() {
let fixture = r#"{"kind":"metric","name":"rps","value":42.0}"#;
let msg = ChildMessage::Metric {
name: "rps".to_string(),
value: 42.0,
};
assert_eq!(serde_json::from_str::<ChildMessage>(fixture).unwrap(), msg);
assert_eq!(serde_json::to_string(&msg).unwrap(), fixture);
}
#[test]
fn an_action_reply_without_an_id_round_trips() {
let fixture = r#"{"kind":"action-reply","action":"gc","body":"ok"}"#;
let msg = ChildMessage::ActionReply {
action: "gc".to_string(),
body: "ok".to_string(),
id: None,
};
assert_eq!(serde_json::from_str::<ChildMessage>(fixture).unwrap(), msg);
assert_eq!(serde_json::to_string(&msg).unwrap(), fixture);
}
#[test]
fn an_action_reply_with_an_echoed_id_round_trips() {
let fixture = r#"{"kind":"action-reply","action":"gc","body":"ok","id":7}"#;
let msg = ChildMessage::ActionReply {
action: "gc".to_string(),
body: "ok".to_string(),
id: Some(7),
};
assert_eq!(serde_json::from_str::<ChildMessage>(fixture).unwrap(), msg);
assert_eq!(serde_json::to_string(&msg).unwrap(), fixture);
}
#[test]
fn shutdown_wire_fixture_round_trips() {
let fixture = r#"{"kind":"shutdown"}"#;
assert_eq!(
serde_json::from_str::<ShepherdMessage>(fixture).unwrap(),
ShepherdMessage::Shutdown
);
assert_eq!(
serde_json::to_string(&ShepherdMessage::Shutdown).unwrap(),
fixture
);
}
#[test]
fn an_action_carries_its_id_with_or_without_params() {
let bare = r#"{"kind":"action","name":"gc","id":7}"#;
let bare_msg = ShepherdMessage::Action {
name: "gc".to_string(),
params: None,
id: 7,
};
assert_eq!(serde_json::to_string(&bare_msg).unwrap(), bare);
assert_eq!(
serde_json::from_str::<ShepherdMessage>(bare).unwrap(),
bare_msg
);
let with_params = r#"{"kind":"action","name":"set-log-level","params":"debug","id":8}"#;
let with_params_msg = ShepherdMessage::Action {
name: "set-log-level".to_string(),
params: Some("debug".to_string()),
id: 8,
};
assert_eq!(
serde_json::to_string(&with_params_msg).unwrap(),
with_params
);
assert_eq!(
serde_json::from_str::<ShepherdMessage>(with_params).unwrap(),
with_params_msg
);
}
}