#[cfg(test)]
mod tests;
use super::response_identity::{ResponseIdentity, ResponseIdentityMismatch};
use crate::model::{
attempt_journal::AttemptJournalRecord,
ic_mutation::IcMutationAcknowledgement,
ic_snapshot_upload::{
IcSnapshotUploadAttempt, IcSnapshotUploadAttemptError, IcSnapshotUploadError,
IcSnapshotUploadReply,
},
};
use thiserror::Error;
#[derive(Debug)]
pub struct IcSnapshotUploadAcknowledgementView<'request, 'source> {
acknowledgement: &'request IcMutationAcknowledgement,
reply: IcSnapshotUploadReply<'request, 'source>,
}
impl<'request, 'source> IcSnapshotUploadAcknowledgementView<'request, 'source> {
#[must_use]
pub const fn acknowledgement(&self) -> &'request IcMutationAcknowledgement {
self.acknowledgement
}
#[must_use]
pub const fn reply(&self) -> &IcSnapshotUploadReply<'request, 'source> {
&self.reply
}
}
pub fn validate_acknowledgement<'request, 'source>(
request: &IcSnapshotUploadAttempt<'request, 'source>,
journal: &AttemptJournalRecord,
acknowledgement: &'request IcMutationAcknowledgement,
) -> Result<IcSnapshotUploadAcknowledgementView<'request, 'source>, IcSnapshotUploadAssociationError>
{
request.validate_journal(journal)?;
let input = acknowledgement.input();
ResponseIdentity {
authority: &input.authority,
mutation_attempt: input.mutation_attempt,
context: &input.context,
target: &input.target,
}
.compare(&ResponseIdentity {
authority: &request.authority().digest(),
mutation_attempt: request.mutation_attempt(),
context: request.plan().context(),
target: request.payload().target(),
})
.map_err(|mismatch| match mismatch {
ResponseIdentityMismatch::Authority => IcSnapshotUploadAssociationError::AuthorityMismatch,
ResponseIdentityMismatch::Attempt => IcSnapshotUploadAssociationError::AttemptMismatch,
ResponseIdentityMismatch::Context => IcSnapshotUploadAssociationError::ContextMismatch,
ResponseIdentityMismatch::Target => IcSnapshotUploadAssociationError::TargetMismatch,
})?;
let reply = IcSnapshotUploadReply::decode(request.payload(), &input.reply)?;
Ok(IcSnapshotUploadAcknowledgementView {
acknowledgement,
reply,
})
}
#[derive(Debug, Error)]
pub enum IcSnapshotUploadAssociationError {
#[error(transparent)]
Reservation(#[from] IcSnapshotUploadAttemptError),
#[error("snapshot upload acknowledgement authority mismatch")]
AuthorityMismatch,
#[error("snapshot upload acknowledgement attempt mismatch")]
AttemptMismatch,
#[error("snapshot upload acknowledgement context mismatch")]
ContextMismatch,
#[error("snapshot upload acknowledgement target mismatch")]
TargetMismatch,
#[error(transparent)]
Reply(#[from] IcSnapshotUploadError),
}