use thiserror::Error;
use crate::PathHash;
#[derive(Debug, Error)]
#[error("Invalid slug: {value}")]
pub struct InvalidSlugError {
value: String,
}
impl InvalidSlugError {
pub(crate) fn new(value: impl Into<String>) -> Self {
Self {
value: value.into(),
}
}
pub fn value(&self) -> &str {
&self.value
}
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ModpkgError {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Reader(#[from] ltk_io_ext::ReaderError),
#[error("Malformed modpkg")]
Malformed(#[source] Box<dyn std::error::Error + Send + Sync>),
#[error("Invalid modpkg header size: {header_size}, actual size: {actual_size}")]
InvalidHeaderSize { header_size: u32, actual_size: u64 },
#[error("Missing metadata chunk")]
MissingMetadata,
#[error("Missing base layer")]
MissingBaseLayer,
#[error("Invalid modpkg compression type: {0}")]
InvalidCompressionType(u8),
#[error("Invalid modpkg license type: {0}")]
InvalidLicenseType(u8),
#[error("Invalid modpkg magic: {0}")]
InvalidMagic(u64),
#[error("Unsupported modpkg format version: {0}")]
UnsupportedFormatVersion(u32),
#[error("Inconsistent duplicate chunk: {0}")]
ChunksInconsistent(PathHash),
#[error("Chunk not found: {0}")]
MissingChunk(PathHash),
#[error("Invalid meta chunk: must not belong to any layer or wad")]
InvalidMetaChunk,
#[error(transparent)]
MsgpackDecode(#[from] rmp_serde::decode::Error),
#[error(transparent)]
MsgpackEncode(#[from] rmp_serde::encode::Error),
}
impl From<binrw::Error> for ModpkgError {
fn from(error: binrw::Error) -> Self {
Self::Malformed(Box::new(error))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pass_through_display_carries_the_cause() {
let error = ModpkgError::from(std::io::Error::new(
std::io::ErrorKind::NotFound,
"the file is gone",
));
assert!(error.to_string().contains("the file is gone"), "{error}");
}
#[test]
fn contextual_display_does_not_embed_its_source() {
let error = ModpkgError::Malformed(Box::new(std::io::Error::other("inner detail")));
let source = std::error::Error::source(&error).unwrap().to_string();
assert!(!error.to_string().contains(&source), "{error}");
}
}