#[cfg(test)]
mod tests;
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();
if input.authority != request.authority().digest() {
return Err(IcSnapshotUploadAssociationError::AuthorityMismatch);
}
if input.mutation_attempt != request.mutation_attempt() {
return Err(IcSnapshotUploadAssociationError::AttemptMismatch);
}
if &input.context != request.plan().context() {
return Err(IcSnapshotUploadAssociationError::ContextMismatch);
}
if input.target != request.payload().target() {
return Err(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),
}