ic_backup/policy/ic_snapshot_transfer_read/
mod.rs1use super::response_identity::{ResponseIdentity, ResponseIdentityMismatch};
4use crate::model::{
5 attempt_journal::AttemptJournalRecord,
6 ic_snapshot_data::{IcSnapshotDataError, IcSnapshotDataReply},
7 ic_snapshot_metadata::{IcSnapshotMetadataError, IcSnapshotMetadataReply},
8 ic_snapshot_transfer_read::{
9 IcSnapshotTransferReadError, IcSnapshotTransferReadPayload, IcSnapshotTransferReadRequest,
10 IcSnapshotTransferReadResponse,
11 },
12};
13use thiserror::Error;
14
15#[derive(Debug)]
17pub enum IcSnapshotTransferReadReply<'request, 'metadata> {
18 Metadata(IcSnapshotMetadataReply<'request>),
20 Data(IcSnapshotDataReply<'request, 'metadata>),
22}
23
24#[derive(Debug)]
26pub struct IcSnapshotTransferReadView<'request, 'metadata> {
27 response: &'request IcSnapshotTransferReadResponse,
28 reply: IcSnapshotTransferReadReply<'request, 'metadata>,
29}
30
31impl<'request, 'metadata> IcSnapshotTransferReadView<'request, 'metadata> {
32 #[must_use]
34 pub const fn response(&self) -> &'request IcSnapshotTransferReadResponse {
35 self.response
36 }
37 #[must_use]
39 pub const fn reply(&self) -> &IcSnapshotTransferReadReply<'request, 'metadata> {
40 &self.reply
41 }
42}
43
44pub fn validate_response<'request, 'metadata>(
53 request: &IcSnapshotTransferReadRequest<'request, 'metadata>,
54 journal: &AttemptJournalRecord,
55 response: &'request IcSnapshotTransferReadResponse,
56) -> Result<IcSnapshotTransferReadView<'request, 'metadata>, IcSnapshotTransferReadAssociationError>
57{
58 request.validate_journal(journal)?;
59 let input = response.input();
60 ResponseIdentity::from(input)
61 .compare(&ResponseIdentity {
62 authority: &request.authority().digest(),
63 mutation_attempt: request.mutation_attempt(),
64 context: request.plan().context(),
65 target: request.payload().target(),
66 })
67 .map_err(|mismatch| match mismatch {
68 ResponseIdentityMismatch::Authority => {
69 IcSnapshotTransferReadAssociationError::AuthorityMismatch
70 }
71 ResponseIdentityMismatch::Attempt => {
72 IcSnapshotTransferReadAssociationError::AttemptMismatch
73 }
74 ResponseIdentityMismatch::Context => {
75 IcSnapshotTransferReadAssociationError::ContextMismatch
76 }
77 ResponseIdentityMismatch::Target => {
78 IcSnapshotTransferReadAssociationError::TargetMismatch
79 }
80 })?;
81 let reply = match request.payload() {
82 IcSnapshotTransferReadPayload::Metadata(payload) => IcSnapshotTransferReadReply::Metadata(
83 IcSnapshotMetadataReply::decode(payload, &input.reply)?,
84 ),
85 IcSnapshotTransferReadPayload::Data(payload) => {
86 IcSnapshotTransferReadReply::Data(IcSnapshotDataReply::decode(payload, &input.reply)?)
87 }
88 };
89 Ok(IcSnapshotTransferReadView { response, reply })
90}
91
92#[derive(Debug, Error)]
94pub enum IcSnapshotTransferReadAssociationError {
95 #[error(transparent)]
97 Reservation(#[from] IcSnapshotTransferReadError),
98 #[error("snapshot transfer read response authority differs")]
100 AuthorityMismatch,
101 #[error("snapshot transfer read response attempt differs")]
103 AttemptMismatch,
104 #[error("snapshot transfer read response context differs")]
106 ContextMismatch,
107 #[error("snapshot transfer read response target differs")]
109 TargetMismatch,
110 #[error(transparent)]
112 Metadata(#[from] IcSnapshotMetadataError),
113 #[error(transparent)]
115 Data(#[from] IcSnapshotDataError),
116}
117
118#[cfg(test)]
119mod tests;