use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Ver {
pub system: String,
pub operator: String,
pub build: String,
}
impl Ver {
pub fn new(
system: impl Into<String>,
operator: impl Into<String>,
build: impl Into<String>,
) -> Self {
Self {
system: system.into(),
operator: operator.into(),
build: build.into(),
}
}
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ver_construction() {
let v = Ver::new("clinlat", "sofa_resp", "0.2.0");
assert_eq!(v.system, "clinlat");
assert_eq!(v.operator, "sofa_resp");
assert_eq!(v.build, "0.2.0");
}
#[test]
fn test_ver_equality() {
let v1 = Ver::new("clinlat", "sofa_resp", "0.2.0");
let v2 = Ver::new("clinlat", "sofa_resp", "0.2.0");
assert_eq!(v1, v2);
}
#[test]
fn test_ver_inequality() {
let v1 = Ver::new("clinlat", "sofa_resp", "0.2.0");
let v2 = Ver::new("clinlat", "sofa_resp", "0.3.0");
assert_ne!(v1, v2);
}
#[test]
fn test_ver_to_json() {
let v = Ver::new("clinlat", "sofa_resp", "0.2.0");
let json = v.to_json().expect("serialization failed");
assert!(json.contains("\"system\":\"clinlat\""));
assert!(json.contains("\"operator\":\"sofa_resp\""));
assert!(json.contains("\"build\":\"0.2.0\""));
}
#[test]
fn test_ver_from_json() {
let json = r#"{"system":"clinlat","operator":"sofa_resp","build":"0.2.0"}"#;
let v = Ver::from_json(json).expect("deserialization failed");
assert_eq!(v.system, "clinlat");
assert_eq!(v.operator, "sofa_resp");
assert_eq!(v.build, "0.2.0");
}
#[test]
fn test_ver_json_round_trip() {
let original = Ver::new("clinlat", "kdigo_aki", "0.2.1");
let json = original.to_json().expect("serialization failed");
let restored = Ver::from_json(&json).expect("deserialization failed");
assert_eq!(original, restored);
}
#[test]
fn test_ver_hash() {
let v1 = Ver::new("clinlat", "sofa_resp", "0.2.0");
let v2 = Ver::new("clinlat", "sofa_resp", "0.2.0");
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(v1);
set.insert(v2);
assert_eq!(set.len(), 1); }
}