#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MachineResource {
pub id: uuid::Uuid,
#[serde(rename = "type")]
pub resource_type: String,
pub attributes: MachineAttributes,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MachineAttributes {
pub fingerprint: String,
pub cores: Option<i32>,
pub memory: Option<i64>,
pub disk: Option<i64>,
pub ip: Option<String>,
pub hostname: Option<String>,
pub platform: Option<String>,
pub name: Option<String>,
pub heartbeat_status: HeartbeatStatus,
pub last_heartbeat_at: Option<chrono::DateTime<chrono::Utc>>,
pub next_heartbeat_at: Option<chrono::DateTime<chrono::Utc>>,
pub last_check_out_at: Option<chrono::DateTime<chrono::Utc>>,
pub metadata: serde_json::Value,
pub created: chrono::DateTime<chrono::Utc>,
pub updated: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HeartbeatStatus {
NotStarted,
Alive,
Dead,
Resurrected,
Unknown(String),
}
impl<'de> serde::Deserialize<'de> for HeartbeatStatus {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(match s.as_str() {
"NOT_STARTED" => HeartbeatStatus::NotStarted,
"ALIVE" => HeartbeatStatus::Alive,
"DEAD" => HeartbeatStatus::Dead,
"RESURRECTED" => HeartbeatStatus::Resurrected,
other => HeartbeatStatus::Unknown(other.to_string()),
})
}
}
impl serde::Serialize for HeartbeatStatus {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let s = match self {
HeartbeatStatus::NotStarted => "NOT_STARTED",
HeartbeatStatus::Alive => "ALIVE",
HeartbeatStatus::Dead => "DEAD",
HeartbeatStatus::Resurrected => "RESURRECTED",
HeartbeatStatus::Unknown(s) => s,
};
serializer.serialize_str(s)
}
}
#[cfg(test)]
mod heartbeat_status_tests {
use super::*;
#[test]
fn deserializes_all_4_known_wire_strings() {
let cases = [
("\"NOT_STARTED\"", HeartbeatStatus::NotStarted),
("\"ALIVE\"", HeartbeatStatus::Alive),
("\"DEAD\"", HeartbeatStatus::Dead),
("\"RESURRECTED\"", HeartbeatStatus::Resurrected),
];
for (wire, expected) in cases {
let parsed: HeartbeatStatus = serde_json::from_str(wire).unwrap();
assert_eq!(parsed, expected, "wire value {wire}");
}
}
#[test]
fn deserializes_unknown_value_to_unknown_variant() {
let parsed: HeartbeatStatus = serde_json::from_str("\"FUTURE_STATE\"").unwrap();
assert_eq!(parsed, HeartbeatStatus::Unknown("FUTURE_STATE".to_string()));
}
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ComponentResource {
pub id: uuid::Uuid,
#[serde(rename = "type")]
pub resource_type: String,
pub attributes: ComponentAttributes,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ComponentAttributes {
pub fingerprint: String,
pub name: String,
pub machine_id: uuid::Uuid,
pub metadata: serde_json::Value,
pub created: chrono::DateTime<chrono::Utc>,
pub updated: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ProcessResource {
pub id: uuid::Uuid,
#[serde(rename = "type")]
pub resource_type: String,
pub attributes: ProcessAttributes,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ProcessAttributes {
pub pid: String,
pub machine_id: uuid::Uuid,
pub last_heartbeat_at: chrono::DateTime<chrono::Utc>,
pub metadata: serde_json::Value,
pub created: chrono::DateTime<chrono::Utc>,
pub updated: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pid(pub(crate) String);
impl Pid {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<u32> for Pid {
fn from(pid: u32) -> Self {
Pid(pid.to_string())
}
}
impl From<i32> for Pid {
fn from(pid: i32) -> Self {
Pid(pid.to_string())
}
}
impl From<String> for Pid {
fn from(pid: String) -> Self {
Pid(pid)
}
}
impl serde::Serialize for Pid {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.0)
}
}
impl<'de> serde::Deserialize<'de> for Pid {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
String::deserialize(deserializer).map(Pid)
}
}
#[cfg(test)]
mod pid_tests {
use super::*;
#[test]
fn from_u32_stringifies() {
let pid: Pid = 1234u32.into();
assert_eq!(pid.as_str(), "1234");
}
#[test]
fn from_i32_stringifies() {
let pid: Pid = 1234i32.into();
assert_eq!(pid.as_str(), "1234");
}
#[test]
fn round_trips_through_serde_as_a_json_string() {
let pid: Pid = 1234u32.into();
let json = serde_json::to_string(&pid).unwrap();
assert_eq!(json, "\"1234\"");
let parsed: Pid = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, pid);
}
}