rst_renderer/
lib.rs

1#![warn(clippy::pedantic)]
2
3mod html;
4
5use std::io::Write;
6
7use anyhow::{Error, anyhow};
8use document_tree::Document;
9
10pub use crate::html::render_html;
11pub use schemars::generate::SchemaSettings;
12
13/// Render a document tree as JSON.
14///
15/// # Errors
16/// Returns an error if serialization fails.
17pub fn render_json<W>(document: &Document, stream: W) -> Result<(), Error>
18where
19    W: Write,
20{
21    serde_json::to_writer(stream, &document)?;
22    Ok(())
23}
24
25#[expect(clippy::missing_panics_doc, reason = "infallible")]
26/// Render the JSON schema for [`document_tree::Document`].
27pub fn render_json_schema_document<W>(stream: W, settings: SchemaSettings, pretty: bool)
28where
29    W: Write,
30{
31    let generator = settings.into_generator();
32    let schema = generator.into_root_schema_for::<document_tree::Document>();
33    let w = if pretty {
34        serde_json::to_writer_pretty
35    } else {
36        serde_json::to_writer
37    };
38    w(stream, &schema).unwrap();
39}
40
41/// Render a document tree as XML.
42///
43/// # Errors
44/// Returns an error if serialization fails.
45pub fn render_xml<W>(document: &Document, stream: W) -> Result<(), Error>
46where
47    W: Write,
48{
49    serde_xml_rs::to_writer(stream, &document)
50        .map_err(|e| anyhow!("Failed to serialize XML: {e}"))?;
51    Ok(())
52}