Module document

Module document 

Source
Expand description

The Document type with typestate pattern.

Document<S> is the aggregate root of the Core DOM. It uses the typestate pattern to enforce document lifecycle at compile time:

  • Draft – mutable, can add/remove sections
  • Validated – immutable structure, safe for serialization/export

The transition Draft -> Validated is one-way via Document::validate(), which consumes the draft (move semantics prevent reuse).

§Design Decisions

  • Typestate, not enum – invalid operations are compile errors (not runtime panics). See Appendix D in the detailed plan.
  • Deserialize always to Draft – serialized data may be modified externally; re-validation is mandatory.
  • No Styled state in Phase 1 – deferred to Phase 2 when Blueprint (StyleRegistry) is available.

§Examples

use hwpforge_core::document::{Document, Draft, Validated};
use hwpforge_core::section::Section;
use hwpforge_core::paragraph::Paragraph;
use hwpforge_core::run::Run;
use hwpforge_core::PageSettings;
use hwpforge_foundation::{CharShapeIndex, ParaShapeIndex};

let mut doc = Document::new();
doc.add_section(Section::with_paragraphs(
    vec![Paragraph::with_runs(
        vec![Run::text("Hello", CharShapeIndex::new(0))],
        ParaShapeIndex::new(0),
    )],
    PageSettings::a4(),
));

let validated: Document<Validated> = doc.validate().unwrap();
assert_eq!(validated.section_count(), 1);
// A validated document cannot add sections:
use hwpforge_core::document::{Document, Validated};
use hwpforge_core::section::Section;
use hwpforge_core::PageSettings;

let mut validated = get_validated();
validated.add_section(Section::new(PageSettings::a4()));
// ERROR: no method named `add_section` found for `Document<Validated>`

Structs§

Document
The document aggregate root with compile-time state tracking.
Draft
Marker type: the document is a mutable draft.
Validated
Marker type: the document has passed structural validation.