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 RenderStats {
    pub headings: i32,
    pub paragraphs: i32,
    pub code_blocks: i32,
    pub block_quotes: i32,
    pub lists: i32,
    pub list_items: i32,
    pub tables: i32,
    pub links: i32,
    pub thematic_breaks: i32,
    pub warnings: Vec<String>,
}

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

    /// Load RenderStats 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 RenderStats 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 RenderStats 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 RenderStats 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 {
            headings: value.get("headings").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
            paragraphs: value
                .get("paragraphs")
                .and_then(|v| v.as_i64())
                .unwrap_or(0) as i32,
            code_blocks: value
                .get("codeBlocks")
                .and_then(|v| v.as_i64())
                .unwrap_or(0) as i32,
            block_quotes: value
                .get("blockQuotes")
                .and_then(|v| v.as_i64())
                .unwrap_or(0) as i32,
            lists: value.get("lists").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
            list_items: value.get("listItems").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
            tables: value.get("tables").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
            links: value.get("links").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
            thematic_breaks: value
                .get("thematicBreaks")
                .and_then(|v| v.as_i64())
                .unwrap_or(0) as i32,
            warnings: value
                .get("warnings")
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(|s| s.to_string()))
                        .collect()
                })
                .unwrap_or_default(),
        }
    }

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

    /// Serialize RenderStats 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
        result.insert(
            "headings".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.headings)),
        );
        result.insert(
            "paragraphs".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.paragraphs)),
        );
        result.insert(
            "codeBlocks".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.code_blocks)),
        );
        result.insert(
            "blockQuotes".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.block_quotes)),
        );
        result.insert(
            "lists".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.lists)),
        );
        result.insert(
            "listItems".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.list_items)),
        );
        result.insert(
            "tables".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.tables)),
        );
        result.insert(
            "links".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.links)),
        );
        result.insert(
            "thematicBreaks".to_string(),
            serde_json::Value::Number(serde_json::Number::from(self.thematic_breaks)),
        );
        result.insert(
            "warnings".to_string(),
            serde_json::to_value(&self.warnings).unwrap_or(serde_json::Value::Null),
        );
        ctx.process_dict(serde_json::Value::Object(result))
    }

    /// Serialize RenderStats 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 RenderStats 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 `RenderStats` 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 RenderStats {
    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 RenderStats {
    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()))
    }
}