use super::{
CheckRevisionId, FailureClass, FindingCause, InvocationProfile, ProfiledTrial, ReplayCapsule,
ReplayPosture, SubjectRevisionId, TargetBinding, TrialId, TrialProfile,
};
pub use crate::identity::{encode_bytes, encode_length};
impl TrialProfile {
#[must_use]
pub const fn slot(self) -> u8 {
match self {
Self::Unprofiled => 0,
}
}
}
impl FailureClass {
#[must_use]
pub const fn slot(self) -> u8 {
match self {
Self::RefusedByCheck => 0,
Self::PropertyDisagreement => 1,
Self::OracleDisagreement => 2,
Self::SubjectPanic => 3,
Self::BudgetExhausted => 4,
}
}
}
impl ReplayPosture {
#[must_use]
pub const fn slot(self) -> u8 {
match self {
Self::ExactDerived => 0,
Self::DeclaredByAuthor => 1,
Self::UnavailableBecauseUntracked => 2,
}
}
}
#[must_use]
pub fn trial_preimage(profiled: ProfiledTrial) -> Vec<u8> {
let mut bytes = Vec::new();
encode_bytes(profiled.key().address().as_bytes(), &mut bytes);
bytes.push(profiled.profile().slot());
bytes
}
#[must_use]
pub fn execution_key_preimage(
trial: TrialId,
subject: SubjectRevisionId,
check: CheckRevisionId,
invocation: InvocationProfile,
target: &TargetBinding,
) -> Vec<u8> {
let mut bytes = Vec::new();
encode_bytes(trial.address().as_bytes(), &mut bytes);
encode_bytes(subject.address().as_bytes(), &mut bytes);
encode_bytes(check.address().as_bytes(), &mut bytes);
bytes.extend_from_slice(&invocation.cases().cases().to_be_bytes());
bytes.extend_from_slice(&invocation.bytes().bytes().to_be_bytes());
bytes.extend_from_slice(&invocation.time().nanoseconds().to_be_bytes());
encode_bytes(target.target().spelling().as_bytes(), &mut bytes);
encode_bytes(target.toolchain().spelling().as_bytes(), &mut bytes);
bytes
}
#[must_use]
pub fn replay_capsule_preimage(capsule: &ReplayCapsule) -> Vec<u8> {
let mut bytes = Vec::new();
encode_bytes(capsule.key().address().as_bytes(), &mut bytes);
encode_bytes(capsule.input(), &mut bytes);
encode_bytes(capsule.fingerprint().address().as_bytes(), &mut bytes);
encode_bytes(capsule.generation().name().as_bytes(), &mut bytes);
bytes.extend_from_slice(&capsule.generation().version().to_be_bytes());
encode_bytes(capsule.minimization().name().as_bytes(), &mut bytes);
bytes.extend_from_slice(&capsule.minimization().version().to_be_bytes());
encode_bytes(capsule.schema().address().as_bytes(), &mut bytes);
bytes.push(capsule.posture().slot());
bytes
}
#[must_use]
pub fn fingerprint_preimage(trial: TrialId, cause: FindingCause, class: FailureClass) -> Vec<u8> {
let mut bytes = Vec::new();
encode_bytes(trial.address().as_bytes(), &mut bytes);
encode_bytes(cause.family().as_bytes(), &mut bytes);
encode_bytes(cause.local().as_bytes(), &mut bytes);
bytes.push(class.slot());
bytes
}