use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct ComponentDefinition {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub docs: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub args: Option<serde_json::Value>,
pub code: String,
}
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum OutputFormat {
#[default]
Html,
Javascript,
Schema,
Json,
}
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct RenderSettings {
#[serde(default)]
pub output: OutputFormat,
#[serde(default = "default_minify_true")]
pub minify: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub utils: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub directives: Option<Vec<String>>,
}
const fn default_minify_true() -> bool {
true
}
impl Default for RenderSettings {
fn default() -> Self {
Self {
output: OutputFormat::default(),
minify: true,
utils: None,
directives: None,
}
}
}
#[derive(Deserialize, Serialize)]
pub struct NamedMdxBatchInput {
#[serde(default)]
pub settings: RenderSettings,
pub mdx: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub components: Option<HashMap<String, ComponentDefinition>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct RenderedMdx {
pub metadata: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub output: Option<String>,
}
#[derive(Clone, Debug)]
pub struct ResourceLimits {
pub max_batch_size: usize,
pub max_mdx_content_size: usize,
pub max_component_code_size: usize,
}
impl Default for ResourceLimits {
fn default() -> Self {
Self {
max_batch_size: 1000,
max_mdx_content_size: 10 * 1024 * 1024, max_component_code_size: 1024 * 1024, }
}
}
impl ResourceLimits {
pub fn validate(&self) -> Result<(), String> {
if self.max_batch_size == 0 {
return Err("max_batch_size must be greater than 0".to_string());
}
if self.max_mdx_content_size == 0 {
return Err("max_mdx_content_size must be greater than 0".to_string());
}
if self.max_component_code_size == 0 {
return Err("max_component_code_size must be greater than 0".to_string());
}
const MAX_RECOMMENDED_BATCH_SIZE: usize = 100_000;
if self.max_batch_size > MAX_RECOMMENDED_BATCH_SIZE {
return Err(format!(
"max_batch_size ({}) exceeds recommended maximum of {}",
self.max_batch_size, MAX_RECOMMENDED_BATCH_SIZE
));
}
const MAX_RECOMMENDED_MDX_CONTENT_SIZE: usize = 100 * 1024 * 1024; if self.max_mdx_content_size > MAX_RECOMMENDED_MDX_CONTENT_SIZE {
return Err(format!(
"max_mdx_content_size ({}) exceeds recommended maximum of {} bytes (100 MB)",
self.max_mdx_content_size, MAX_RECOMMENDED_MDX_CONTENT_SIZE
));
}
Ok(())
}
}
pub struct TsxTransformConfig {
pub jsx_pragma: String,
pub jsx_pragma_frag: String,
pub minify: bool,
pub component_names: Option<std::collections::HashSet<String>>,
}
impl Default for TsxTransformConfig {
fn default() -> Self {
Self {
jsx_pragma: "engine.h".to_string(),
jsx_pragma_frag: "engine.Fragment".to_string(),
minify: false,
component_names: None,
}
}
}
impl TsxTransformConfig {
pub fn for_output(minify: bool) -> Self {
Self {
jsx_pragma: "h".to_string(),
jsx_pragma_frag: "Fragment".to_string(),
minify,
component_names: None,
}
}
pub fn for_engine(minify: bool) -> Self {
Self {
minify,
component_names: None,
..Self::default()
}
}
}