use serde::{Deserialize, Serialize};
pub use subc_config::{CanonicalError, Digest, InputVector, Plan};
pub const CONSENT_V1: &str = "consent/v1";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
enum ConsentProtocol {
#[serde(rename = "consent/v1")]
V1,
}
crate::config::open_string_enum! {
ConsentDecision { Approved => "approved", Declined => "declined" }
}
impl ConsentDecision {
pub fn is_approved(&self) -> bool {
matches!(self, Self::Approved)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConsentRequest {
protocol: ConsentProtocol,
pub plan_digest: Digest,
pub base_revision: u64,
pub input_vector: InputVector,
pub plan: Plan,
}
impl ConsentRequest {
pub fn new(plan: Plan) -> Result<Self, CanonicalError> {
Ok(Self {
protocol: ConsentProtocol::V1,
plan_digest: plan.digest()?,
base_revision: plan.base_revision,
input_vector: plan.input_vector.clone(),
plan,
})
}
pub fn is_consistent(&self) -> bool {
self.plan
.digest()
.is_ok_and(|digest| digest == self.plan_digest)
&& self.base_revision == self.plan.base_revision
&& self.input_vector == self.plan.input_vector
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConsentResponse {
protocol: ConsentProtocol,
pub plan_digest: Digest,
pub base_revision: u64,
pub input_vector: InputVector,
pub decision: ConsentDecision,
}
impl ConsentResponse {
pub fn new(request: &ConsentRequest, decision: ConsentDecision) -> Self {
Self {
protocol: ConsentProtocol::V1,
plan_digest: request.plan_digest.clone(),
base_revision: request.base_revision,
input_vector: request.input_vector.clone(),
decision,
}
}
pub fn approves(&self, request: &ConsentRequest) -> bool {
self.decision.is_approved()
&& self.plan_digest == request.plan_digest
&& self.base_revision == request.base_revision
&& self.input_vector == request.input_vector
}
}