1use serde::{Deserialize, Serialize};
9pub use subc_config::{CanonicalError, Digest, InputVector, Plan};
10
11pub const CONSENT_V1: &str = "consent/v1";
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14enum ConsentProtocol {
15 #[serde(rename = "consent/v1")]
16 V1,
17}
18
19crate::config::open_string_enum! {
20 ConsentDecision { Approved => "approved", Declined => "declined" }
22}
23impl ConsentDecision {
24 pub fn is_approved(&self) -> bool {
25 matches!(self, Self::Approved)
26 }
27}
28
29#[non_exhaustive]
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct ConsentRequest {
33 protocol: ConsentProtocol,
34 pub plan_digest: Digest,
35 pub base_revision: u64,
36 pub input_vector: InputVector,
37 pub plan: Plan,
38}
39impl ConsentRequest {
40 pub fn new(plan: Plan) -> Result<Self, CanonicalError> {
43 Ok(Self {
44 protocol: ConsentProtocol::V1,
45 plan_digest: plan.digest()?,
46 base_revision: plan.base_revision,
47 input_vector: plan.input_vector.clone(),
48 plan,
49 })
50 }
51
52 pub fn is_consistent(&self) -> bool {
56 self.plan
57 .digest()
58 .is_ok_and(|digest| digest == self.plan_digest)
59 && self.base_revision == self.plan.base_revision
60 && self.input_vector == self.plan.input_vector
61 }
62}
63
64#[non_exhaustive]
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68pub struct ConsentResponse {
69 protocol: ConsentProtocol,
70 pub plan_digest: Digest,
71 pub base_revision: u64,
72 pub input_vector: InputVector,
73 pub decision: ConsentDecision,
74}
75impl ConsentResponse {
76 pub fn new(request: &ConsentRequest, decision: ConsentDecision) -> Self {
77 Self {
78 protocol: ConsentProtocol::V1,
79 plan_digest: request.plan_digest.clone(),
80 base_revision: request.base_revision,
81 input_vector: request.input_vector.clone(),
82 decision,
83 }
84 }
85
86 pub fn approves(&self, request: &ConsentRequest) -> bool {
89 self.decision.is_approved()
90 && self.plan_digest == request.plan_digest
91 && self.base_revision == request.base_revision
92 && self.input_vector == request.input_vector
93 }
94}