Skip to main content

Crate docspec_oxa_writer

Crate docspec_oxa_writer 

Source
Expand description

DocSpec event stream to oxa.dev JSON writer.

This crate provides a streaming OxaWriter that implements EventSink to convert DocSpec event streams into oxa.dev JSON format.

§Design

The writer emits JSON tokens directly to the underlying Write as events arrive using docspec-json for streaming JSON output. Memory usage is constant regardless of document size.

§Supported Events

  • StartDocument / EndDocument — document root
  • StartParagraph / EndParagraph — paragraph blocks
  • Text — inline text content

§Example

use docspec_core::{Event, EventSink, Result};
use docspec_oxa_writer::OxaWriter;

let mut buf = Vec::<u8>::new();
let mut writer = OxaWriter::new(&mut buf);
writer.handle_event(Event::StartDocument { id: None, language: None, metadata: None })?;
writer.handle_event(Event::StartParagraph { alignment: None, id: None })?;
writer.handle_event(Event::Text {
    content: "Hello".to_string(),
})?;
writer.handle_event(Event::EndParagraph)?;
writer.handle_event(Event::EndDocument)?;
writer.finish()?;
let json = String::from_utf8(buf).map_err(|err| docspec_core::Error::Other {
    message: err.to_string(),
})?;
assert_eq!(
    json,
    r#"{"type":"Document","children":[{"type":"Paragraph","children":[{"type":"Text","value":"Hello"}]}]}"#
);

Structs§

OxaWriter
Streaming writer that converts a DocSpec event stream into oxa.dev JSON.