use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BadOfdException {
#[error("错误OFD结构和文件格式: {message}")]
CorruptedStructure {
message: String,
},
#[error("OFD解析失败: {message}")]
ParseFailed {
message: String,
},
#[error("文件不存在: {path}")]
FileNotFound {
path: String,
},
#[error(transparent)]
Other(#[from] easyofd_core::OfdError),
}
impl BadOfdException {
pub fn corrupted(message: impl Into<String>) -> Self {
Self::CorruptedStructure {
message: message.into(),
}
}
pub fn parse_failed(message: impl Into<String>) -> Self {
Self::ParseFailed {
message: message.into(),
}
}
pub fn file_not_found(path: impl Into<String>) -> Self {
Self::FileNotFound { path: path.into() }
}
}
impl From<BadOfdException> for easyofd_core::OfdError {
fn from(e: BadOfdException) -> Self {
match e {
BadOfdException::CorruptedStructure { message }
| BadOfdException::ParseFailed { message } => {
easyofd_core::OfdError::InvalidDocument(message)
}
BadOfdException::FileNotFound { path } => easyofd_core::OfdError::Zip(path),
BadOfdException::Other(inner) => inner,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_corrupted_structure() {
let e = BadOfdException::corrupted("missing DocRoot");
assert!(e.to_string().contains("missing DocRoot"));
}
#[test]
fn test_parse_failed() {
let e = BadOfdException::parse_failed("invalid XML");
assert!(e.to_string().contains("invalid XML"));
}
#[test]
fn test_file_not_found() {
let e = BadOfdException::file_not_found("/Doc_0/missing.xml");
assert!(e.to_string().contains("/Doc_0/missing.xml"));
}
#[test]
fn test_from_ofd_error() {
let ofd_err = easyofd_core::OfdError::Zip("bad zip".into());
let bad: BadOfdException = ofd_err.into();
assert!(matches!(bad, BadOfdException::Other(_)));
}
#[test]
fn test_into_ofd_error() {
let bad = BadOfdException::corrupted("test");
let ofd_err: easyofd_core::OfdError = bad.into();
assert!(matches!(
ofd_err,
easyofd_core::OfdError::InvalidDocument(_)
));
}
}