quilldown 1.5.0

Convert GitHub-Flavored Markdown into high-fidelity Word .docx documents.
Documentation
// <auto-generated by typra-emitter>
// Code generated by Typra emitter; DO NOT EDIT.

#![allow(
    unused_imports,
    dead_code,
    non_camel_case_types,
    unused_variables,
    unexpected_cfgs,
    clippy::all
)]

use super::context::{LoadContext, SaveContext};

#[derive(Debug, Clone, Default, PartialEq)]
pub struct RenderOptions {
    pub strict: Option<bool>,
    pub image_dpi: Option<f64>,
    pub embed_svg: Option<bool>,
    pub svg_light_mode: Option<bool>,
    pub max_image_width_px: Option<i32>,
    pub base_dir: Option<String>,
    pub highlight_code: Option<bool>,
    pub theme: Option<String>,
    pub page_size: Option<String>,
    pub orientation: Option<String>,
    pub margin: Option<f64>,
    pub page_numbers: Option<bool>,
    pub table_of_contents: Option<bool>,
    pub language: Option<String>,
    pub allow_remote_images: Option<bool>,
    pub captions: Option<bool>,
}

impl RenderOptions {
    /// Create a new RenderOptions with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Load RenderOptions from a JSON string.
    pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
        let value: serde_json::Value = serde_json::from_str(json)?;
        Self::validate_input_at(&value, "")
            .map_err(|message| <serde_json::Error as serde::de::Error>::custom(message))?;
        Ok(Self::load_from_value(&value, ctx))
    }

    /// Load RenderOptions from a YAML string.
    pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
        let value: serde_json::Value = serde_yaml::from_str(yaml)?;
        Self::validate_input_at(&value, "")
            .map_err(|message| <serde_yaml::Error as serde::de::Error>::custom(message))?;
        Ok(Self::load_from_value(&value, ctx))
    }

    /// Load RenderOptions from an already-parsed JSON value, returning an error
    /// instead of panicking on invalid input. Fallible companion to
    /// `load_from_value` with the same validation policy as `from_json`.
    pub fn try_load_from_value(
        value: &serde_json::Value,
        ctx: &LoadContext,
    ) -> Result<Self, serde_json::Error> {
        Self::validate_input_at(value, "")
            .map_err(|message| <serde_json::Error as serde::de::Error>::custom(message))?;
        Ok(Self::load_from_value(value, ctx))
    }

    /// Load RenderOptions from a `serde_json::Value`.
    ///
    /// Calls `ctx.process_input` before field extraction.
    pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
        let value = ctx.process_input(value.clone());
        if let Err(message) = Self::validate_input_at(&value, "") {
            panic!("{}", message);
        }
        Self {
            strict: value.get("strict").and_then(|v| v.as_bool()),
            image_dpi: value.get("image_dpi").and_then(|v| v.as_f64()),
            embed_svg: value.get("embed_svg").and_then(|v| v.as_bool()),
            svg_light_mode: value.get("svg_light_mode").and_then(|v| v.as_bool()),
            max_image_width_px: value
                .get("max_image_width_px")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            base_dir: value
                .get("base_dir")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            highlight_code: value.get("highlight_code").and_then(|v| v.as_bool()),
            theme: value
                .get("theme")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            page_size: value
                .get("page_size")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            orientation: value
                .get("orientation")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            margin: value.get("margin").and_then(|v| v.as_f64()),
            page_numbers: value.get("page_numbers").and_then(|v| v.as_bool()),
            table_of_contents: value.get("table_of_contents").and_then(|v| v.as_bool()),
            language: value
                .get("language")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            allow_remote_images: value.get("allow_remote_images").and_then(|v| v.as_bool()),
            captions: value.get("captions").and_then(|v| v.as_bool()),
        }
    }

    pub(crate) fn validate_input_at(value: &serde_json::Value, path: &str) -> Result<(), String> {
        Ok(())
    }

    /// Serialize RenderOptions to a `serde_json::Value`.
    ///
    /// Calls `ctx.process_dict` after serialization.
    pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
        let mut result = serde_json::Map::new();
        // Write base fields
        if let Some(val) = self.strict.as_ref() {
            result.insert("strict".to_string(), serde_json::Value::Bool(*val));
        }
        if let Some(val) = self.image_dpi.as_ref() {
            result.insert(
                "image_dpi".to_string(),
                serde_json::Number::from_f64(*val as f64)
                    .map(serde_json::Value::Number)
                    .unwrap_or(serde_json::Value::Null),
            );
        }
        if let Some(val) = self.embed_svg.as_ref() {
            result.insert("embed_svg".to_string(), serde_json::Value::Bool(*val));
        }
        if let Some(val) = self.svg_light_mode.as_ref() {
            result.insert("svg_light_mode".to_string(), serde_json::Value::Bool(*val));
        }
        if let Some(val) = self.max_image_width_px.as_ref() {
            result.insert(
                "max_image_width_px".to_string(),
                serde_json::Value::Number(serde_json::Number::from(*val)),
            );
        }
        if let Some(val) = self.base_dir.as_ref() {
            result.insert(
                "base_dir".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.highlight_code.as_ref() {
            result.insert("highlight_code".to_string(), serde_json::Value::Bool(*val));
        }
        if let Some(val) = self.theme.as_ref() {
            result.insert("theme".to_string(), serde_json::Value::String(val.clone()));
        }
        if let Some(val) = self.page_size.as_ref() {
            result.insert(
                "page_size".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.orientation.as_ref() {
            result.insert(
                "orientation".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.margin.as_ref() {
            result.insert(
                "margin".to_string(),
                serde_json::Number::from_f64(*val as f64)
                    .map(serde_json::Value::Number)
                    .unwrap_or(serde_json::Value::Null),
            );
        }
        if let Some(val) = self.page_numbers.as_ref() {
            result.insert("page_numbers".to_string(), serde_json::Value::Bool(*val));
        }
        if let Some(val) = self.table_of_contents.as_ref() {
            result.insert(
                "table_of_contents".to_string(),
                serde_json::Value::Bool(*val),
            );
        }
        if let Some(val) = self.language.as_ref() {
            result.insert(
                "language".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.allow_remote_images.as_ref() {
            result.insert(
                "allow_remote_images".to_string(),
                serde_json::Value::Bool(*val),
            );
        }
        if let Some(val) = self.captions.as_ref() {
            result.insert("captions".to_string(), serde_json::Value::Bool(*val));
        }
        ctx.process_dict(serde_json::Value::Object(result))
    }

    /// Serialize RenderOptions to a JSON string.
    pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(&self.to_value(ctx))
    }

    /// Serialize RenderOptions to a YAML string.
    pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
        serde_yaml::to_string(&self.to_value(ctx))
    }
}

// Serde for `RenderOptions` delegates to the canonical to_value/load_from_value
// logic so its serde wire form always equals the canonical to_value/load_from_value form. Uses a default (no-op) context — no ${env:}/${file:}
// resolution here — leaving the context-aware LoadContext/SaveContext API intact.
#[cfg(feature = "serde")]
impl serde::Serialize for RenderOptions {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serde::Serialize::serialize(&self.to_value(&SaveContext::default()), serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for RenderOptions {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = <serde_json::Value as serde::Deserialize>::deserialize(deserializer)?;
        Self::validate_input_at(&value, "").map_err(serde::de::Error::custom)?;
        Ok(Self::load_from_value(&value, &LoadContext::default()))
    }
}