use minijinja::{Error, ErrorKind};
use crate::localization::{self, keys};
pub(super) enum GlobExpansionFailure {
InvalidPattern(Error),
BaseCanonicalization(Error),
Utf8Conversion(Error),
CapabilityRootIo(Error),
GlobEntryProcessing(Error),
}
impl GlobExpansionFailure {
pub(super) const fn outcome(&self) -> &'static str {
match self {
Self::InvalidPattern(_) => "invalid_pattern",
Self::BaseCanonicalization(_) => "base_canonicalization_failure",
Self::Utf8Conversion(_) => "utf8_conversion_failure",
Self::CapabilityRootIo(_) => "capability_root_io_failure",
Self::GlobEntryProcessing(_) => "glob_entry_processing_failure",
}
}
pub(super) fn into_error(self) -> Error {
match self {
Self::InvalidPattern(error)
| Self::BaseCanonicalization(error)
| Self::Utf8Conversion(error)
| Self::CapabilityRootIo(error)
| Self::GlobEntryProcessing(error) => error,
}
}
}
#[derive(Debug)]
pub(super) struct GlobErrorContext {
pub(super) pattern: String,
pub(super) error_char: char,
pub(super) position: usize,
pub(super) error_type: GlobErrorType,
}
#[derive(Debug)]
pub(super) enum GlobErrorType {
UnmatchedBrace,
InvalidPattern,
IoError,
}
pub(super) fn create_glob_error(context: &GlobErrorContext, details: Option<String>) -> Error {
match context.error_type {
GlobErrorType::UnmatchedBrace => Error::new(
ErrorKind::SyntaxError,
localization::message(keys::MANIFEST_GLOB_UNMATCHED_BRACE)
.with_arg("pattern", &context.pattern)
.with_arg("character", context.error_char)
.with_arg("position", context.position)
.to_string(),
),
GlobErrorType::InvalidPattern => {
let detail = details.unwrap_or_else(|| {
localization::message(keys::MANIFEST_GLOB_UNKNOWN_PATTERN_ERROR).to_string()
});
Error::new(
ErrorKind::SyntaxError,
localization::message(keys::MANIFEST_GLOB_INVALID_PATTERN)
.with_arg("pattern", &context.pattern)
.with_arg("detail", detail)
.to_string(),
)
}
GlobErrorType::IoError => {
let detail = details.unwrap_or_else(|| {
localization::message(keys::MANIFEST_GLOB_UNKNOWN_IO_ERROR).to_string()
});
Error::new(
ErrorKind::InvalidOperation,
localization::message(keys::MANIFEST_GLOB_IO_FAILED)
.with_arg("pattern", &context.pattern)
.with_arg("detail", detail)
.to_string(),
)
}
}
}
pub(super) fn create_unmatched_brace_error(context: &GlobErrorContext) -> Error {
create_glob_error(context, None)
}