pub struct Document<S = Draft> { /* private fields */ }Expand description
The document aggregate root with compile-time state tracking.
The generic parameter S determines which operations are available:
§Typestate Safety
The _state field is private and zero-sized. There is no way to
construct a Document<Validated> except through validate().
§Examples
use hwpforge_core::document::Document;
use hwpforge_core::Metadata;
let doc = Document::with_metadata(Metadata {
title: Some("Report".to_string()),
..Metadata::default()
});
assert!(doc.is_empty());Implementations§
Source§impl<S> Document<S>
impl<S> Document<S>
Source§impl Document<Draft>
impl Document<Draft>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty draft document with default metadata.
§Examples
use hwpforge_core::document::Document;
let doc = Document::new();
assert!(doc.is_empty());Sourcepub fn with_metadata(metadata: Metadata) -> Self
pub fn with_metadata(metadata: Metadata) -> Self
Creates a new draft document with the given metadata.
§Examples
use hwpforge_core::document::Document;
use hwpforge_core::Metadata;
let doc = Document::with_metadata(Metadata {
title: Some("Test".to_string()),
..Metadata::default()
});
assert_eq!(doc.metadata().title.as_deref(), Some("Test"));Sourcepub fn add_section(&mut self, section: Section)
pub fn add_section(&mut self, section: Section)
Appends a section to the draft document.
§Examples
use hwpforge_core::document::Document;
use hwpforge_core::section::Section;
use hwpforge_core::PageSettings;
let mut doc = Document::new();
doc.add_section(Section::new(PageSettings::a4()));
assert_eq!(doc.section_count(), 1);Sourcepub fn set_metadata(&mut self, metadata: Metadata)
pub fn set_metadata(&mut self, metadata: Metadata)
Sets the document metadata.
Sourcepub fn metadata_mut(&mut self) -> &mut Metadata
pub fn metadata_mut(&mut self) -> &mut Metadata
Returns a mutable reference to the metadata.
Sourcepub fn sections_mut(&mut self) -> &mut [Section]
pub fn sections_mut(&mut self) -> &mut [Section]
Returns a mutable slice of sections.
Sourcepub fn validate(self) -> CoreResult<Document<Validated>>
pub fn validate(self) -> CoreResult<Document<Validated>>
Validates the document structure and transitions to Validated.
Consumes self (move semantics). On success, returns a
Document<Validated>. On failure, returns a CoreError.
§Errors
Returns CoreError::Validation if the document violates any
structural invariant (empty sections, empty paragraphs, etc.).
§Examples
use hwpforge_core::document::Document;
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 = doc.validate().unwrap();
assert_eq!(validated.section_count(), 1);use hwpforge_core::document::Document;
let doc = Document::new(); // empty
assert!(doc.validate().is_err());Trait Implementations§
Source§impl<'de> Deserialize<'de> for Document<Draft>
impl<'de> Deserialize<'de> for Document<Draft>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<S> JsonSchema for Document<S>
impl<S> JsonSchema for Document<S>
Source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreimpl<S> Eq for Document<S>
Auto Trait Implementations§
impl<S> Freeze for Document<S>
impl<S> RefUnwindSafe for Document<S>where
S: RefUnwindSafe,
impl<S> Send for Document<S>where
S: Send,
impl<S> Sync for Document<S>where
S: Sync,
impl<S> Unpin for Document<S>where
S: Unpin,
impl<S> UnwindSafe for Document<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.