use crate::ids::{check_id, check_unique};
use crate::{Error, Hash32, Result, Timestamp};
use serde::{Deserialize, Serialize};
pub const MAX_REVOKED: usize = 4096;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Freshness {
pub app: String,
pub channel: String,
pub latest_seq: u64,
pub manifest_hash: Hash32,
#[serde(default)]
pub revoked: Vec<String>,
#[serde(default)]
pub rollout_cap_bp: Option<u16>,
pub issued: Timestamp,
pub expires: Timestamp,
}
impl Freshness {
pub fn validate(&self) -> Result<()> {
check_id("app", &self.app)?;
check_id("channel", &self.channel)?;
if self.expires <= self.issued {
return Err(Error::invalid("freshness", "expires must be after issued"));
}
if self.revoked.len() > MAX_REVOKED {
return Err(Error::invalid("freshness", format!("more than {MAX_REVOKED} revocations")));
}
for id in &self.revoked {
check_id("revoked update id", id)?;
}
check_unique("revoked", &self.revoked)?;
if self.rollout_cap_bp.is_some_and(|c| c > 10_000) {
return Err(Error::invalid("freshness", "rollout cap above 10000 basis points"));
}
Ok(())
}
}