133 lines
4.0 KiB
Rust
133 lines
4.0 KiB
Rust
//! Typed failures returned by the input layer.
|
|
|
|
use std::{io, path::PathBuf, string::FromUtf8Error};
|
|
|
|
use thiserror::Error;
|
|
|
|
use crate::SourceId;
|
|
|
|
/// Failure while discovering or reading one input document.
|
|
#[derive(Debug, Error)]
|
|
pub enum InputError {
|
|
/// Reading standard input failed.
|
|
#[error("failed to read standard input")]
|
|
StdinIo {
|
|
/// Underlying operating-system error.
|
|
#[source]
|
|
source: io::Error,
|
|
},
|
|
/// The requested path does not identify a file or directory.
|
|
#[error("path `{path}` is neither a file nor a directory")]
|
|
MissingPath {
|
|
/// Requested path.
|
|
path: PathBuf,
|
|
},
|
|
|
|
/// A filesystem operation failed.
|
|
#[error("failed to {action} `{path}`")]
|
|
Io {
|
|
/// Operation being attempted.
|
|
action: &'static str,
|
|
/// Affected filesystem path.
|
|
path: PathBuf,
|
|
/// Underlying operating-system error.
|
|
#[source]
|
|
source: io::Error,
|
|
},
|
|
|
|
/// Recursive directory traversal failed for one entry.
|
|
#[error("failed while walking an input directory")]
|
|
Walk {
|
|
/// Underlying traversal error.
|
|
#[source]
|
|
source: walkdir::Error,
|
|
},
|
|
|
|
/// A ZIP container or member could not be decoded.
|
|
#[error("failed to read ZIP data from `{archive}`")]
|
|
Zip {
|
|
/// Outer archive path.
|
|
archive: PathBuf,
|
|
/// Underlying ZIP format error.
|
|
#[source]
|
|
source: zip::result::ZipError,
|
|
},
|
|
|
|
/// A source exceeded a configured byte or count limit.
|
|
#[error("{source_id} uses {actual} {resource}, exceeding the limit of {limit}")]
|
|
LimitExceeded {
|
|
/// Source that violated the limit.
|
|
source_id: SourceId,
|
|
/// Human-readable resource unit.
|
|
resource: &'static str,
|
|
/// Observed value, or `u64::MAX` after arithmetic overflow.
|
|
actual: u64,
|
|
/// Configured maximum.
|
|
limit: u64,
|
|
},
|
|
|
|
/// Archive expansion is disproportionate to compressed size.
|
|
#[error("archive member {source_id} exceeds the configured {limit}:1 compression ratio")]
|
|
CompressionRatio {
|
|
/// Archive member identity.
|
|
source_id: SourceId,
|
|
/// Maximum allowed ratio.
|
|
limit: u64,
|
|
},
|
|
|
|
/// An archive member name is unsafe to treat as a relative identity.
|
|
#[error("unsafe archive member path `{member}` in `{archive}`")]
|
|
UnsafeArchiveMember {
|
|
/// Outer archive path.
|
|
archive: PathBuf,
|
|
/// Untrusted member name from the archive.
|
|
member: String,
|
|
},
|
|
|
|
/// Encrypted members are unsupported.
|
|
#[error("archive member {source_id} is encrypted")]
|
|
EncryptedMember {
|
|
/// Encrypted archive member identity.
|
|
source_id: SourceId,
|
|
},
|
|
|
|
/// Input contained a NUL byte and is treated as binary.
|
|
#[error("input {source_id} contains a NUL byte")]
|
|
BinaryContent {
|
|
/// Source treated as binary.
|
|
source_id: SourceId,
|
|
},
|
|
|
|
/// Input was not strict UTF-8.
|
|
#[error("input {source_id} is not valid UTF-8")]
|
|
InvalidUtf8 {
|
|
/// Source that failed decoding.
|
|
source_id: SourceId,
|
|
/// Underlying UTF-8 conversion error.
|
|
#[source]
|
|
source: FromUtf8Error,
|
|
},
|
|
}
|
|
|
|
impl InputError {
|
|
/// Return the exact document identity when the failure occurred after discovery.
|
|
pub const fn source_id(&self) -> Option<&SourceId> {
|
|
match self {
|
|
Self::LimitExceeded { source_id, .. }
|
|
| Self::CompressionRatio { source_id, .. }
|
|
| Self::EncryptedMember { source_id }
|
|
| Self::BinaryContent { source_id }
|
|
| Self::InvalidUtf8 { source_id, .. } => Some(source_id),
|
|
Self::StdinIo { .. }
|
|
| Self::MissingPath { .. }
|
|
| Self::Io { .. }
|
|
| Self::Walk { .. }
|
|
| Self::Zip { .. }
|
|
| Self::UnsafeArchiveMember { .. } => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Result type used by the input layer.
|
|
pub type InputResult<T> = Result<T, InputError>;
|