satteri-plugin-api 0.5.2

Rust plugin trait, typed visitors, and runner for Sätteri
Documentation
// @generated by `cargo run -p satteri-layout-codegen`. Do not edit by hand.

use crate::generated::wire_constants::{
    OF_ALT, OF_IDENTIFIER, OF_LABEL, OF_LANG, OF_META, OF_NAME, OF_REFERENCE_TYPE, OF_TITLE,
    OF_URL, OF_VALUE,
};
#[cfg(feature = "mdx")]
use crate::js_commands::intern_mdx_jsx_attrs;
use crate::js_commands::{FieldCollector, HastFieldCollector, alloc_opt_str};
use satteri_arena::{ArenaBuilder, Hast, Mdast, StringRef};
use satteri_ast::hast::codec::encode_element_data;
#[cfg(feature = "mdx")]
use satteri_ast::mdast::codec::encode_mdx_jsx_element_data;
use satteri_ast::mdast::codec::{ColumnAlign, encode_directive_data, encode_table_data};
use satteri_ast::shared::{PROP_BOOL_FALSE, PROP_BOOL_TRUE};

/// Largest fixed-field `type_data` layout; callers stack-allocate this much.
pub(crate) const MAX_FIXED_TYPE_DATA: usize = 32;

/// Encode a node's `type_data` from an op-stream collector into the zeroed
/// stack buffer `buf`, returning the encoded length; `None` for types whose
/// variable-length codec stays hand-written.
#[allow(clippy::manual_range_patterns)]
pub(crate) fn encode_mdast_type_data_from_ops(
    c: &FieldCollector,
    node_type: u8,
    builder: &mut ArenaBuilder<Mdast>,
    buf: &mut [u8; MAX_FIXED_TYPE_DATA],
) -> Option<usize> {
    Some(match node_type {
        2 => {
            buf[0] = c.depth.unwrap_or(1);
            1
        }
        7 | 10 | 13 | 25 | 26 => {
            let r = alloc_opt_str(builder, c.strs[OF_VALUE as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            8
        }
        8 => {
            let r = alloc_opt_str(builder, c.strs[OF_LANG as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_META as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_VALUE as usize]);
            buf[16..24].copy_from_slice(&r.as_bytes());
            buf[24] = 96;
            28
        }
        9 => {
            let r = alloc_opt_str(builder, c.strs[OF_URL as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_TITLE as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_IDENTIFIER as usize]);
            buf[16..24].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_LABEL as usize]);
            buf[24..32].copy_from_slice(&r.as_bytes());
            32
        }
        15 => {
            let r = alloc_opt_str(builder, c.strs[OF_URL as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_TITLE as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            16
        }
        16 => {
            let r = alloc_opt_str(builder, c.strs[OF_URL as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_ALT as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_TITLE as usize]);
            buf[16..24].copy_from_slice(&r.as_bytes());
            24
        }
        17 => {
            let r = alloc_opt_str(builder, c.strs[OF_IDENTIFIER as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_LABEL as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            buf[16] = match c.strs[OF_REFERENCE_TYPE as usize] {
                Some("collapsed") => 1,
                Some("full") => 2,
                _ => 0,
            };
            20
        }
        18 => {
            let r = alloc_opt_str(builder, c.strs[OF_IDENTIFIER as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_LABEL as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            buf[16] = match c.strs[OF_REFERENCE_TYPE as usize] {
                Some("collapsed") => 1,
                Some("full") => 2,
                _ => 0,
            };
            let r = alloc_opt_str(builder, c.strs[OF_ALT as usize]);
            buf[20..28].copy_from_slice(&r.as_bytes());
            28
        }
        19 => {
            let r = alloc_opt_str(builder, c.strs[OF_IDENTIFIER as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_LABEL as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            16
        }
        20 => {
            let r = alloc_opt_str(builder, c.strs[OF_IDENTIFIER as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_LABEL as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            20
        }
        27 => {
            let r = alloc_opt_str(builder, c.strs[OF_META as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_VALUE as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            16
        }
        28 => {
            let r = alloc_opt_str(builder, c.strs[OF_VALUE as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            16
        }
        38 => {
            let r = alloc_opt_str(builder, c.strs[OF_NAME as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            let r = alloc_opt_str(builder, c.strs[OF_VALUE as usize]);
            buf[8..16].copy_from_slice(&r.as_bytes());
            16
        }
        102 | 103 | 104 => {
            let r = alloc_opt_str(builder, c.strs[OF_VALUE as usize]);
            buf[0..8].copy_from_slice(&r.as_bytes());
            8
        }
        _ => return None,
    })
}

/// Encode a tail node's `type_data` from an op-stream collector, or `None`
/// for tags with no generated tail (the remaining hand-written arms).
#[allow(clippy::manual_range_patterns)]
pub(crate) fn encode_mdast_tail_from_ops(
    c: &FieldCollector,
    node_type: u8,
    builder: &mut ArenaBuilder<Mdast>,
) -> Option<Vec<u8>> {
    match node_type {
        21 => {
            let aligns: Vec<ColumnAlign> = c
                .align
                .unwrap_or(&[])
                .iter()
                .map(|&b| ColumnAlign::from_u8(b).unwrap_or(ColumnAlign::None))
                .collect();
            Some(encode_table_data(&aligns))
        }
        30 | 31 | 32 => {
            let head = alloc_opt_str(builder, c.strs[OF_NAME as usize]);
            let mut items = Vec::with_capacity(c.props.len());
            for &(key, _kind, value) in &c.props {
                items.push((builder.alloc_string(key), builder.alloc_string(value)));
            }
            Some(encode_directive_data(head, &items))
        }
        #[cfg(feature = "mdx")]
        100 | 101 => {
            let head = alloc_opt_str(builder, c.strs[OF_NAME as usize]);
            let attrs = intern_mdx_jsx_attrs(&c.props, builder);
            Some(encode_mdx_jsx_element_data(
                head,
                &attrs,
                c.explicit.unwrap_or(false),
            ))
        }
        _ => None,
    }
}

/// Encode a tail node's `type_data` from an op-stream collector, or `None`
/// for tags with no generated tail (the remaining hand-written arms).
#[allow(clippy::manual_range_patterns)]
pub(crate) fn encode_hast_tail_from_ops(
    c: &HastFieldCollector,
    node_type: u8,
    builder: &mut ArenaBuilder<Hast>,
) -> Option<Vec<u8>> {
    match node_type {
        1 => {
            let tag = builder.alloc_string(c.tag.unwrap_or("div"));
            let mut props = Vec::with_capacity(c.props.len());
            for &(name, kind, value) in &c.props {
                let n = builder.alloc_string(name);
                let v = if kind == PROP_BOOL_TRUE || kind == PROP_BOOL_FALSE {
                    StringRef::empty()
                } else {
                    builder.alloc_string(value)
                };
                props.push((n, kind, v));
            }
            Some(encode_element_data(tag, &props))
        }
        #[cfg(feature = "mdx")]
        10 | 11 => {
            let head = builder.alloc_string(c.tag.unwrap_or(""));
            let attrs = intern_mdx_jsx_attrs(&c.props, builder);
            Some(encode_mdx_jsx_element_data(
                head,
                &attrs,
                c.explicit.unwrap_or(false),
            ))
        }
        _ => None,
    }
}