use qubit_fs::error::FsError;
use qubit_fs::error::FsErrorKind;
use qubit_fs::error::FsOperation;
use qubit_fs::metadata::FileSystemCapability;
use qubit_fs::path::Path;
#[track_caller]
#[allow(dead_code)]
pub(crate) fn assert_error(
error: &FsError,
kind: FsErrorKind,
operation: FsOperation,
path: Option<&Path>,
provider: Option<&str>,
capability: Option<FileSystemCapability>,
) {
assert_eq!(kind, error.kind(), "filesystem error kind must match");
assert_eq!(operation, error.operation(), "filesystem error operation must match",);
assert_eq!(path, error.path(), "filesystem error path must match");
assert!(
error.provider().is_none() || error.provider() == provider,
"filesystem error provider must be absent or match the configured provider",
);
assert_eq!(
capability,
error.required_capability(),
"filesystem error required capability must match",
);
}
#[track_caller]
#[allow(dead_code)]
pub(crate) fn assert_error_with_target(
error: &FsError,
kind: FsErrorKind,
operation: FsOperation,
path: Option<&Path>,
target: Option<&Path>,
provider: Option<&str>,
capability: Option<FileSystemCapability>,
) {
assert_error(error, kind, operation, path, provider, capability);
assert_eq!(target, error.target(), "filesystem error target must match");
}
#[track_caller]
#[allow(dead_code)]
pub(crate) fn assert_error_with_source_or_target(
error: &FsError,
kind: FsErrorKind,
operation: FsOperation,
source: &Path,
target: &Path,
provider: Option<&str>,
capability: Option<FileSystemCapability>,
) {
assert_eq!(kind, error.kind(), "filesystem error kind must match");
assert_eq!(operation, error.operation(), "filesystem error operation must match");
assert!(
matches!(error.path(), Some(path) if path == source || path == target),
"filesystem error path must identify the source or target failure location",
);
assert_eq!(Some(target), error.target(), "filesystem error target must match");
assert!(
error.provider().is_none() || error.provider() == provider,
"filesystem error provider must be absent or match the configured provider",
);
assert_eq!(
capability,
error.required_capability(),
"filesystem error required capability must match"
);
}
#[track_caller]
#[allow(dead_code)]
pub(crate) fn assert_unsupported_error(
error: &FsError,
kind: FsErrorKind,
operation: FsOperation,
path: Option<&Path>,
provider: Option<&str>,
capability: Option<FileSystemCapability>,
) {
assert_eq!(kind, error.kind(), "filesystem error kind must match");
assert_eq!(operation, error.operation(), "filesystem error operation must match",);
assert_eq!(path, error.path(), "filesystem error path must match");
assert_eq!(
capability,
error.required_capability(),
"filesystem error required capability must match",
);
assert!(
error.provider().is_none() || error.provider() == provider,
"filesystem error provider must be absent or match the configured provider",
);
}
pub(crate) fn verify_missing_error(
error: FsError,
path: &Path,
provider: &str,
check: crate::ContractCheckId,
) -> Result<(), crate::ContractFailure> {
let valid = error.kind() == FsErrorKind::NotFound
&& error.operation() == FsOperation::Stat
&& error.path() == Some(path)
&& error.target().is_none()
&& error.required_capability().is_none()
&& error.provider().is_none_or(|actual| actual == provider);
if valid {
Ok(())
} else {
Err(
crate::ContractFailure::with_source("missing-resource error context differs from the contract", error)
.at(check),
)
}
}
pub(crate) fn verify_fs_error(
error: FsError,
kind: FsErrorKind,
operation: FsOperation,
path: &Path,
provider: &str,
capability: Option<FileSystemCapability>,
check: crate::ContractCheckId,
) -> Result<(), crate::ContractFailure> {
let valid = error.kind() == kind
&& error.operation() == operation
&& error.path() == Some(path)
&& error.required_capability() == capability
&& error.provider().is_none_or(|actual| actual == provider);
if valid {
Ok(())
} else {
Err(crate::ContractFailure::with_source(format!("{check}: filesystem error context mismatch"), error).at(check))
}
}
pub(crate) fn verify_condition(
condition: bool,
check: crate::ContractCheckId,
message: &str,
) -> Result<(), crate::ContractFailure> {
if condition {
Ok(())
} else {
Err(crate::ContractFailure::message_only(format!("{check}: {message}")).at(check))
}
}