1use serde::{Deserialize, Serialize};
16use std::collections::HashMap;
17
18#[derive(Deserialize, Serialize, Clone, Debug)]
20pub struct ComponentDefinition {
21 #[serde(skip_serializing_if = "Option::is_none")]
23 pub name: Option<String>,
24 #[serde(skip_serializing_if = "Option::is_none")]
26 pub docs: Option<String>,
27 #[serde(skip_serializing_if = "Option::is_none")]
29 pub args: Option<serde_json::Value>,
30 pub code: String,
32}
33
34#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
36#[serde(rename_all = "lowercase")]
37pub enum OutputFormat {
38 #[default]
40 Html,
41 Javascript,
43 Schema,
45}
46
47#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Default)]
49#[serde(rename_all = "lowercase")]
50pub enum RenderEngine {
51 #[default]
53 Base,
54 Custom,
56}
57
58#[derive(Deserialize, Serialize, Clone, Debug)]
60pub struct RenderSettings {
61 #[serde(default)]
63 pub output: OutputFormat,
64 #[serde(default = "default_minify_true")]
66 pub minify: bool,
67 #[serde(default)]
69 pub engine: RenderEngine,
70 #[serde(default, skip_serializing_if = "Vec::is_empty")]
72 pub components: Vec<String>,
73}
74
75const fn default_minify_true() -> bool {
76 true
77}
78
79impl Default for RenderSettings {
80 fn default() -> Self {
81 Self {
82 output: OutputFormat::default(),
83 minify: true,
84 engine: RenderEngine::default(),
85 components: Vec::new(),
86 }
87 }
88}
89
90#[derive(Deserialize, Serialize)]
92pub struct NamedMdxBatchInput {
93 #[serde(default)]
95 pub settings: RenderSettings,
96 pub mdx: HashMap<String, String>,
98 #[serde(skip_serializing_if = "Option::is_none")]
100 pub components: Option<HashMap<String, ComponentDefinition>>,
101}
102
103#[derive(Serialize, Deserialize, Debug)]
105pub struct RenderedMdx {
106 pub metadata: serde_json::Value,
108 #[serde(skip_serializing_if = "Option::is_none")]
116 pub output: Option<String>,
117}
118
119#[derive(Clone, Debug)]
125pub struct ResourceLimits {
126 pub max_batch_size: usize,
128 pub max_mdx_content_size: usize,
130 pub max_component_code_size: usize,
132}
133
134impl Default for ResourceLimits {
135 fn default() -> Self {
136 Self {
137 max_batch_size: 1000,
138 max_mdx_content_size: 10 * 1024 * 1024, max_component_code_size: 1024 * 1024, }
141 }
142}
143
144impl ResourceLimits {
145 pub fn validate(&self) -> Result<(), String> {
150 if self.max_batch_size == 0 {
151 return Err("max_batch_size must be greater than 0".to_string());
152 }
153
154 if self.max_mdx_content_size == 0 {
155 return Err("max_mdx_content_size must be greater than 0".to_string());
156 }
157
158 if self.max_component_code_size == 0 {
159 return Err("max_component_code_size must be greater than 0".to_string());
160 }
161
162 const MAX_RECOMMENDED_BATCH_SIZE: usize = 100_000;
164 if self.max_batch_size > MAX_RECOMMENDED_BATCH_SIZE {
165 return Err(format!(
166 "max_batch_size ({}) exceeds recommended maximum of {}",
167 self.max_batch_size, MAX_RECOMMENDED_BATCH_SIZE
168 ));
169 }
170
171 const MAX_RECOMMENDED_MDX_CONTENT_SIZE: usize = 100 * 1024 * 1024; if self.max_mdx_content_size > MAX_RECOMMENDED_MDX_CONTENT_SIZE {
173 return Err(format!(
174 "max_mdx_content_size ({}) exceeds recommended maximum of {} bytes (100 MB)",
175 self.max_mdx_content_size, MAX_RECOMMENDED_MDX_CONTENT_SIZE
176 ));
177 }
178
179 Ok(())
180 }
181}
182
183pub struct TsxTransformConfig {
185 pub jsx_pragma: String,
187 pub jsx_pragma_frag: String,
189 pub minify: bool,
191 pub component_names: Option<std::collections::HashSet<String>>,
193}
194
195impl Default for TsxTransformConfig {
196 fn default() -> Self {
197 Self {
198 jsx_pragma: "engine.h".to_string(),
199 jsx_pragma_frag: "engine.Fragment".to_string(),
200 minify: false,
201 component_names: None,
202 }
203 }
204}
205
206impl TsxTransformConfig {
207 pub fn for_output(minify: bool) -> Self {
209 Self {
210 jsx_pragma: "h".to_string(),
211 jsx_pragma_frag: "Fragment".to_string(),
212 minify,
213 component_names: None,
214 }
215 }
216
217 pub fn for_engine(minify: bool) -> Self {
219 Self {
220 minify,
221 component_names: None,
222 ..Self::default()
223 }
224 }
225}