| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
mod evidence; |
| 42 |
mod manifest; |
| 43 |
mod provenance; |
| 44 |
mod record; |
| 45 |
|
| 46 |
pub use evidence::{GateRecord, Scope, Verdict}; |
| 47 |
pub use manifest::{BundleDigest, Manifest, ManifestEntry}; |
| 48 |
pub use provenance::Provenance; |
| 49 |
pub use record::{ArtifactRecord, CONTRACT_VERSION, EvidenceRecord}; |
| 50 |
|
| 51 |
|
| 52 |
#[derive(Debug)] |
| 53 |
pub enum ContractError { |
| 54 |
|
| 55 |
NotASha256(String), |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
NotAGitSha(String), |
| 60 |
|
| 61 |
UnsafePath(String), |
| 62 |
|
| 63 |
DuplicatePath(String), |
| 64 |
|
| 65 |
|
| 66 |
EmptyManifest, |
| 67 |
|
| 68 |
MalformedManifestLine(usize), |
| 69 |
|
| 70 |
MissingProvenance(&'static str), |
| 71 |
|
| 72 |
DigestMismatch { claimed: String, computed: String }, |
| 73 |
|
| 74 |
EnvironmentEvidenceFromBuilder { gate: String, env: String }, |
| 75 |
|
| 76 |
UnknownContractVersion(u32), |
| 77 |
|
| 78 |
Malformed(serde_json::Error), |
| 79 |
} |
| 80 |
|
| 81 |
impl std::fmt::Display for ContractError { |
| 82 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 83 |
match self { |
| 84 |
ContractError::NotASha256(s) => { |
| 85 |
write!(f, "`{s}` is not a sha256 (64 lowercase hex characters)") |
| 86 |
} |
| 87 |
ContractError::NotAGitSha(s) => { |
| 88 |
write!( |
| 89 |
f, |
| 90 |
"`{s}` is not a full git sha (40 lowercase hex characters)" |
| 91 |
) |
| 92 |
} |
| 93 |
ContractError::UnsafePath(p) => write!( |
| 94 |
f, |
| 95 |
"manifest path `{p}` must be relative, forward-slashed, and stay inside the bundle" |
| 96 |
), |
| 97 |
ContractError::DuplicatePath(p) => { |
| 98 |
write!(f, "manifest lists `{p}` twice, so it describes two bundles") |
| 99 |
} |
| 100 |
ContractError::EmptyManifest => write!(f, "manifest lists no files"), |
| 101 |
ContractError::MalformedManifestLine(n) => { |
| 102 |
write!(f, "manifest line {n} is not `<sha256> <path>`") |
| 103 |
} |
| 104 |
ContractError::MissingProvenance(field) => write!(f, "`{field}` is empty"), |
| 105 |
ContractError::DigestMismatch { claimed, computed } => write!( |
| 106 |
f, |
| 107 |
"record claims digest {claimed} but its manifest hashes to {computed}" |
| 108 |
), |
| 109 |
ContractError::EnvironmentEvidenceFromBuilder { gate, env } => write!( |
| 110 |
f, |
| 111 |
"gate `{gate}` is scoped to environment `{env}`, which a build host does not have" |
| 112 |
), |
| 113 |
ContractError::UnknownContractVersion(v) => { |
| 114 |
write!( |
| 115 |
f, |
| 116 |
"contract version {v}, this reader knows {CONTRACT_VERSION}" |
| 117 |
) |
| 118 |
} |
| 119 |
ContractError::Malformed(e) => write!(f, "malformed document: {e}"), |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
impl std::error::Error for ContractError { |
| 125 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 126 |
match self { |
| 127 |
ContractError::Malformed(e) => Some(e), |
| 128 |
_ => None, |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|