rhwp 0.9.0

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
//! HWP 문서 직렬화 모듈
//!
//! Document IR을 HWP 5.0 바이너리 파일로 변환하는 기능을 제공한다.
//! `parser` 모듈의 역방향으로 동작한다.

pub mod body_text;
pub(crate) mod budget_cursor;
pub mod byte_writer;
pub mod cfb_writer;
pub(crate) mod char_shape;
mod column_sizes;
pub mod content_loss;
pub mod control;
pub mod doc_info;
mod form_identity;
pub mod header;
pub mod hml;
pub mod hwpx;
mod hwpx_lineage_lines;
mod line_cache;
pub mod mini_cfb;
/// [#4100] 중첩 OLE CFB 의 스트림 교체 — 루트 CLSID 와 나머지 스트림을 보존한다.
pub mod ole_container;
pub mod record_writer;
mod shape_rendering;

pub use cfb_writer::{
    serialize_hwp, serialize_hwp_with_password, serialize_hwp_with_password_and_report,
    serialize_hwp_with_report,
};
pub use content_loss::{
    ContentLoss, ContentLossCode, ContentLossReason, ContentLossReport, ContentLossSubject,
    SerializedDocument, SerializedFormat,
};
pub use hml::serialize_hml;
pub use hwpx::{
    serialize_hwpx, serialize_hwpx_with_password, serialize_hwpx_with_password_and_report,
    serialize_hwpx_with_report,
};

/// 직렬화 에러 (HWP + HWPX 공용)
#[derive(Debug)]
pub enum SerializeError {
    /// CFB 생성/쓰기 실패 (HWP)
    CfbError(String),
    /// 압축 실패 (HWP/HWPX 공용)
    CompressError(String),
    /// ZIP 생성/쓰기 실패 (HWPX)
    ZipError(String),
    /// XML 생성 실패 (HWPX)
    XmlError(String),
    /// 지원하지 않는 입력 (예: HWP 소스를 HWPX 직렬화기에 넘긴 경우)
    UnsupportedInput(String),
    /// 비밀번호 보호 패키지 생성 실패
    CryptoError(String),
}

impl std::fmt::Display for SerializeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SerializeError::CfbError(e) => write!(f, "CFB 쓰기 실패: {}", e),
            SerializeError::CompressError(e) => write!(f, "압축 실패: {}", e),
            SerializeError::ZipError(e) => write!(f, "ZIP 쓰기 실패: {}", e),
            SerializeError::XmlError(e) => write!(f, "XML 쓰기 실패: {}", e),
            SerializeError::UnsupportedInput(e) => write!(f, "지원하지 않는 입력: {}", e),
            SerializeError::CryptoError(e) => write!(f, "비밀번호 암호화 실패: {}", e),
        }
    }
}

impl std::error::Error for SerializeError {}

use crate::model::document::Document;

// ---------------------------------------------------------------------------
// Trait 추상화: DocumentSerializer
// ---------------------------------------------------------------------------

/// 문서 직렬화 trait — Document IR을 바이트로 변환
pub trait DocumentSerializer {
    fn serialize(&self, doc: &Document) -> Result<Vec<u8>, SerializeError>;
}

/// HWP 5.0 바이너리 직렬화
pub struct HwpSerializer;

impl DocumentSerializer for HwpSerializer {
    fn serialize(&self, doc: &Document) -> Result<Vec<u8>, SerializeError> {
        serialize_hwp(doc)
    }
}

/// HWPX(ZIP+XML) 직렬화
pub struct HwpxSerializer;

impl DocumentSerializer for HwpxSerializer {
    fn serialize(&self, doc: &Document) -> Result<Vec<u8>, SerializeError> {
        serialize_hwpx(doc)
    }
}

/// 현재 지원 포맷(HWP)으로 직렬화
pub fn serialize_document(doc: &Document) -> Result<Vec<u8>, SerializeError> {
    HwpSerializer.serialize(doc)
}

/// 현재 지원 HWP 형식으로 직렬화하고, 성공한 산출물의 내용 손실을 값으로 돌려준다.
pub fn serialize_document_with_report(
    doc: &Document,
) -> Result<SerializedDocument, SerializeError> {
    serialize_hwp_with_report(doc)
}

#[cfg(test)]
mod tests {
    use super::*;

    struct MockSerializer;
    impl DocumentSerializer for MockSerializer {
        fn serialize(&self, _doc: &Document) -> Result<Vec<u8>, SerializeError> {
            Ok(vec![0xDE, 0xAD])
        }
    }

    #[test]
    fn test_mock_serializer() {
        let doc = Document::default();
        assert_eq!(MockSerializer.serialize(&doc).unwrap(), vec![0xDE, 0xAD]);
    }
}