use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Manifest {
pub format_version: u32,
pub arch: String,
pub install_mode: String,
pub package: Package,
#[serde(default)]
pub uninstall_options: Vec<UninstallOption>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UninstallOption {
pub id: String,
pub label: String,
#[serde(default)]
pub default: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Package {
pub name: String,
pub display_name: String,
pub version: String,
pub identifier: String,
pub publisher: String,
pub description: String,
pub homepage: Option<String>,
pub license: Option<String>,
}
impl Manifest {
pub fn to_bytes(&self) -> Vec<u8> {
serde_json::to_vec(self).expect("manifest serializes")
}
pub fn parse(bytes: &[u8]) -> Result<Manifest, serde_json::Error> {
serde_json::from_slice(bytes)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> Manifest {
Manifest {
format_version: crate::FORMAT_VERSION,
arch: "x86_64".to_string(),
install_mode: "per-user".to_string(),
package: Package {
name: "hello".to_string(),
display_name: "Embala Hello".to_string(),
version: "0.1.0".to_string(),
identifier: "ca.hrndz.embala.hello".to_string(),
publisher: "Carlos Hernandez".to_string(),
description: "test".to_string(),
homepage: Some("https://example.com".to_string()),
license: None,
},
uninstall_options: vec![UninstallOption {
id: "purge-data".to_string(),
label: "Delete all data".to_string(),
default: false,
}],
}
}
#[test]
fn round_trips_through_bytes() {
let m = sample();
let parsed = Manifest::parse(&m.to_bytes()).expect("valid manifest");
assert_eq!(parsed, m);
}
#[test]
fn serialization_is_deterministic() {
assert_eq!(sample().to_bytes(), sample().to_bytes());
}
#[test]
fn json_without_uninstall_options_parses() {
let json = br#"{"format_version":2,"arch":"x86_64","install_mode":"per-user","package":{"name":"hello","display_name":"Embala Hello","version":"0.1.0","identifier":"ca.hrndz.embala.hello","publisher":"Carlos Hernandez","description":"test","homepage":null,"license":null}}"#;
let parsed = Manifest::parse(json).expect("old sidecar must still parse");
assert!(parsed.uninstall_options.is_empty());
}
}