use core::fmt;
use crate::digest::Digest;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CanonicalErrorKind {
NonFiniteNumber,
IntegerOutOfSafeRange,
DuplicateKey,
DepthLimitExceeded,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CanonicalError {
kind: CanonicalErrorKind,
offset: Option<usize>,
detail: String,
}
impl CanonicalError {
#[must_use]
pub const fn new(kind: CanonicalErrorKind, detail: String) -> Self {
Self {
kind,
offset: None,
detail,
}
}
#[must_use]
pub const fn at_offset(kind: CanonicalErrorKind, offset: usize, detail: String) -> Self {
Self {
kind,
offset: Some(offset),
detail,
}
}
#[must_use]
pub const fn kind(&self) -> CanonicalErrorKind {
self.kind
}
#[must_use]
pub const fn offset(&self) -> Option<usize> {
self.offset
}
#[must_use]
pub fn detail(&self) -> &str {
&self.detail
}
}
impl fmt::Display for CanonicalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.offset {
Some(offset) => write!(
f,
"canonicalization error at byte {offset}: {}",
self.detail
),
None => write!(f, "canonicalization error: {}", self.detail),
}
}
}
impl std::error::Error for CanonicalError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ParseErrorKind {
EmptyInput,
UnexpectedByte,
UnexpectedEnd,
InvalidLiteral,
InvalidNumber,
NumberOutOfRange,
IntegerOutOfSafeRange,
DuplicateKey,
DepthLimit,
LoneSurrogate,
InvalidEscape,
InvalidUnicodeEscape,
ControlCharacterInString,
InvalidUtf8,
TrailingBytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
kind: ParseErrorKind,
offset: usize,
detail: String,
}
impl ParseError {
#[must_use]
pub const fn new(kind: ParseErrorKind, offset: usize, detail: String) -> Self {
Self {
kind,
offset,
detail,
}
}
#[must_use]
pub const fn kind(&self) -> ParseErrorKind {
self.kind
}
#[must_use]
pub const fn offset(&self) -> usize {
self.offset
}
#[must_use]
pub fn detail(&self) -> &str {
&self.detail
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "parse error at byte {}: {}", self.offset, self.detail)
}
}
impl std::error::Error for ParseError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DigestErrorKind {
InvalidLength,
InvalidCharacter,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DigestError {
kind: DigestErrorKind,
detail: String,
}
impl DigestError {
#[must_use]
pub const fn new(kind: DigestErrorKind, detail: String) -> Self {
Self { kind, detail }
}
#[must_use]
pub const fn kind(&self) -> DigestErrorKind {
self.kind
}
#[must_use]
pub fn detail(&self) -> &str {
&self.detail
}
}
impl fmt::Display for DigestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "digest error: {}", self.detail)
}
}
impl std::error::Error for DigestError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PathErrorKind {
Empty,
Absolute,
BackslashSeparator,
ParentTraversal,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PathError {
kind: PathErrorKind,
declared: String,
detail: String,
}
impl PathError {
#[must_use]
pub const fn new(kind: PathErrorKind, declared: String, detail: String) -> Self {
Self {
kind,
declared,
detail,
}
}
#[must_use]
pub const fn kind(&self) -> PathErrorKind {
self.kind
}
#[must_use]
pub fn declared(&self) -> &str {
&self.declared
}
#[must_use]
pub fn detail(&self) -> &str {
&self.detail
}
}
impl fmt::Display for PathError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"invalid sandbox path {:?}: {}",
self.declared, self.detail
)
}
}
impl std::error::Error for PathError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DagError {
Schema {
task_id: Option<String>,
detail: String,
},
DuplicateTaskId {
task_id: String,
},
UnknownDependency {
task_id: String,
missing: String,
},
CollidingOutput {
path: String,
first_task: String,
second_task: String,
},
DuplicateInput {
task_id: String,
path: String,
},
InvalidPath {
task_id: String,
source: PathError,
},
Cycle {
remnants: Vec<String>,
},
}
impl fmt::Display for DagError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Schema { task_id, detail } => match task_id {
Some(task_id) => write!(f, "schema violation in task {task_id:?}: {detail}"),
None => write!(f, "schema violation: {detail}"),
},
Self::DuplicateTaskId { task_id } => {
write!(f, "duplicate task id {task_id:?}")
}
Self::UnknownDependency { task_id, missing } => {
write!(f, "task {task_id:?} depends on unknown task {missing:?}")
}
Self::CollidingOutput {
path,
first_task,
second_task,
} => write!(
f,
"output path {path:?} is claimed by both {first_task:?} and {second_task:?}"
),
Self::DuplicateInput { task_id, path } => {
write!(f, "task {task_id:?} declares input {path:?} more than once")
}
Self::InvalidPath { task_id, source } => {
write!(f, "task {task_id:?} declares an invalid path: {source}")
}
Self::Cycle { remnants } => write!(
f,
"dependency cycle: tasks left unordered are {}",
remnants.join(", ")
),
}
}
}
impl std::error::Error for DagError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidPath { source, .. } => Some(source),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ManifestError {
WrongFormat {
found: String,
},
MissingField {
field: &'static str,
},
WrongType {
field: &'static str,
expected: &'static str,
},
InvalidDigest {
field: &'static str,
source: DigestError,
},
UnknownStatus {
found: String,
},
InvalidNumber {
field: &'static str,
detail: String,
},
DuplicateTaskId {
task_id: String,
},
DuplicateArtifactPath {
task_id: String,
path: String,
},
}
impl fmt::Display for ManifestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::WrongFormat { found } => {
write!(
f,
"manifest format is {found:?}, expected \"verirun-manifest/1\""
)
}
Self::MissingField { field } => write!(f, "manifest field {field:?} is missing"),
Self::WrongType { field, expected } => {
write!(f, "manifest field {field:?} must be {expected}")
}
Self::InvalidDigest { field, source } => {
write!(
f,
"manifest field {field:?} is not a valid digest: {source}"
)
}
Self::UnknownStatus { found } => write!(f, "unknown task status {found:?}"),
Self::InvalidNumber { field, detail } => {
write!(
f,
"manifest field {field:?} holds an invalid number: {detail}"
)
}
Self::DuplicateTaskId { task_id } => {
write!(f, "manifest holds two records for task {task_id:?}")
}
Self::DuplicateArtifactPath { task_id, path } => {
write!(
f,
"manifest record for task {task_id:?} names artifact {path:?} more than once"
)
}
}
}
}
impl std::error::Error for ManifestError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidDigest { source, .. } => Some(source),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerifyError {
ManifestMissing {
digest: Digest,
},
ManifestUnreadable {
digest: Digest,
detail: String,
},
ManifestDigestMismatch {
expected: Digest,
actual: Digest,
},
NonCanonicalManifest {
detail: String,
},
MalformedManifest {
source: ManifestError,
},
RootMismatch {
stored: Digest,
recomputed: Digest,
},
BlobMissing {
digest: Digest,
task_id: String,
path: String,
},
BlobUnreadable {
digest: Digest,
task_id: String,
path: String,
detail: String,
},
BlobMismatch {
expected: Digest,
actual: Digest,
task_id: String,
path: String,
},
ArtifactSizeMismatch {
digest: Digest,
expected: u64,
actual: u64,
task_id: String,
path: String,
},
}
impl fmt::Display for VerifyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ManifestMissing { digest } => {
write!(f, "no manifest object {digest} in the store")
}
Self::ManifestUnreadable { digest, detail } => {
write!(f, "manifest object {digest} could not be read: {detail}")
}
Self::ManifestDigestMismatch { expected, actual } => write!(
f,
"manifest digest mismatch: requested {expected}, content hashes to {actual}"
),
Self::NonCanonicalManifest { detail } => {
write!(f, "manifest is not in canonical form: {detail}")
}
Self::MalformedManifest { source } => write!(f, "malformed manifest: {source}"),
Self::RootMismatch { stored, recomputed } => write!(
f,
"merkle root mismatch: stored {stored}, recomputed {recomputed}"
),
Self::BlobMissing {
digest,
task_id,
path,
} => write!(
f,
"blob {digest} referenced by task {task_id:?} at {path:?} is missing"
),
Self::BlobUnreadable {
digest,
task_id,
path,
detail,
} => write!(
f,
"blob {digest} referenced by task {task_id:?} at {path:?} could not be read: {detail}"
),
Self::BlobMismatch {
expected,
actual,
task_id,
path,
} => write!(
f,
"blob mismatch for task {task_id:?} at {path:?}: expected {expected}, content hashes to {actual}"
),
Self::ArtifactSizeMismatch {
digest,
expected,
actual,
task_id,
path,
} => write!(
f,
"artifact size mismatch for task {task_id:?} at {path:?} (blob {digest}): manifest records {expected} bytes, blob has {actual} bytes"
),
}
}
}
impl std::error::Error for VerifyError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::MalformedManifest { source } => Some(source),
_ => None,
}
}
}