use serde::{Deserialize, Serialize};
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TrustedAppBuild {
pub identifier: String,
pub cdhash: String,
pub version: String,
pub build_time: Option<String>,
pub added_ms: u64,
}
impl TrustedAppBuild {
pub fn new(
identifier: String,
cdhash: String,
version: String,
build_time: Option<String>,
added_ms: u64,
) -> Self {
Self {
identifier,
cdhash,
version,
build_time,
added_ms,
}
}
}
macro_rules! app_op {
($tag:ident, $op:literal, $request:ident { $($r:ident: $rt:ty),* },
$reply:ident { $($s:ident: $st:ty),* }) => {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
enum $tag {
#[serde(rename = $op)]
Op,
}
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct $request {
op: $tag,
$(pub $r: $rt,)*
}
impl $request {
pub fn new($($r: $rt),*) -> Self {
Self { op: $tag::Op, $($r,)* }
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct $reply {
op: $tag,
$(pub $s: $st,)*
}
impl $reply {
pub fn new($($s: $st),*) -> Self {
Self { op: $tag::Op, $($s,)* }
}
}
};
}
app_op!(
TrustOp,
"app.trust",
AppTrustRequest { path: String },
AppTrustReply {
entry: TrustedAppBuild
}
);
app_op!(TrustedOp, "app.trusted", AppTrustedRequest {}, AppTrustedReply { entries: Vec<TrustedAppBuild> });
app_op!(
UntrustOp,
"app.untrust",
AppUntrustRequest { cdhash: String },
AppUntrustReply { removed: bool }
);
impl Default for AppTrustedRequest {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{json, to_value};
#[test]
fn app_trust_wire_shapes() {
assert_eq!(
to_value(AppTrustRequest::new("/Test.app".into())).unwrap(),
json!({"op":"app.trust", "path":"/Test.app"})
);
assert_eq!(
to_value(AppTrustedRequest::new()).unwrap(),
json!({"op":"app.trusted"})
);
assert_eq!(
to_value(AppUntrustRequest::new("abc".into())).unwrap(),
json!({"op":"app.untrust", "cdhash":"abc"})
);
let entry = TrustedAppBuild::new(
"dev.cortexkit.alfonso".into(),
"abc".into(),
"1.0".into(),
None,
12,
);
let body = json!({"identifier":"dev.cortexkit.alfonso", "cdhash":"abc", "version":"1.0", "build_time":null, "added_ms":12});
assert_eq!(
to_value(AppTrustReply::new(entry.clone())).unwrap(),
json!({"op":"app.trust", "entry":body})
);
assert_eq!(
to_value(AppTrustedReply::new(vec![entry])).unwrap(),
json!({"op":"app.trusted", "entries":[body]})
);
assert_eq!(
to_value(AppUntrustReply::new(true)).unwrap(),
json!({"op":"app.untrust", "removed":true})
);
assert!(serde_json::from_value::<AppTrustRequest>(
json!({"op":"app.untrust", "path":"/Test.app"})
)
.is_err());
assert!(serde_json::from_value::<AppTrustedRequest>(json!({})).is_err());
}
}