#[derive(Debug)]
pub enum HwpError {
InvalidFile(String),
PageOutOfRange(u32),
RenderError(String),
ResourceLimit(String),
InvalidField(String),
}
impl From<crate::parser::ParseError> for HwpError {
fn from(e: crate::parser::ParseError) -> Self {
HwpError::InvalidFile(format!("{e}"))
}
}
impl From<crate::parser::hwpx::HwpxError> for HwpError {
fn from(e: crate::parser::hwpx::HwpxError) -> Self {
HwpError::InvalidFile(format!("{e}"))
}
}
impl From<crate::serializer::SerializeError> for HwpError {
fn from(e: crate::serializer::SerializeError) -> Self {
HwpError::RenderError(format!("{e}"))
}
}
impl std::fmt::Display for HwpError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HwpError::InvalidFile(msg) => write!(f, "유효하지 않은 파일: {}", msg),
HwpError::PageOutOfRange(n) => write!(f, "페이지 {}을(를) 찾을 수 없습니다", n),
HwpError::RenderError(msg) => write!(f, "렌더링 오류: {}", msg),
HwpError::ResourceLimit(msg) => write!(f, "자원 상한 초과: {}", msg),
HwpError::InvalidField(msg) => write!(f, "필드 오류: {}", msg),
}
}
}
impl std::error::Error for HwpError {}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::ParseError;
#[test]
fn parse_error_to_hwp_error_uses_display_not_debug() {
let pe = ParseError::UnsupportedFormat {
code: "UNSUPPORTED_HWP3",
format: "HWP 3.0",
hint: "다시 저장해주세요.",
};
let he: HwpError = pe.into();
let msg = format!("{he}");
assert!(
msg.contains("HWP 3.0"),
"HWP 3.0 must appear in display: {msg}"
);
assert!(
msg.contains("UNSUPPORTED_HWP3"),
"error code must appear: {msg}"
);
assert!(msg.contains("다시 저장해주세요"), "hint must appear: {msg}");
assert!(!msg.contains("UnsupportedFormat"), "Debug leaked: {msg}");
assert!(!msg.contains("{ format"), "Debug leaked: {msg}");
}
}