1use crate::OutputFormat;
83
84pub const MAX_INPUT_SIZE: usize = 10 * 1024 * 1024;
86
87pub const MAX_YAML_SIZE: usize = 1024 * 1024;
89
90pub const MAX_NESTING_DEPTH: usize = 100;
92
93pub use crate::document::limits::MAX_YAML_DEPTH;
98
99pub const MAX_CARD_COUNT: usize = 1000;
102
103pub const MAX_FIELD_COUNT: usize = 1000;
106
107#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
109#[serde(rename_all = "lowercase")]
110pub enum Severity {
111 Error,
113 Warning,
115 Note,
117}
118
119#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
121#[serde(rename_all = "camelCase")]
122pub struct Location {
123 pub file: String,
125 pub line: u32,
127 pub column: u32,
129}
130
131#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
138#[serde(rename_all = "camelCase")]
139pub struct Diagnostic {
140 pub severity: Severity,
142 #[serde(skip_serializing_if = "Option::is_none", default)]
144 pub code: Option<String>,
145 pub message: String,
147 #[serde(skip_serializing_if = "Option::is_none", default)]
149 pub location: Option<Location>,
150 #[serde(skip_serializing_if = "Option::is_none", default)]
152 pub hint: Option<String>,
153 #[serde(skip_serializing_if = "Vec::is_empty", default)]
155 pub source_chain: Vec<String>,
156}
157
158impl Diagnostic {
159 pub fn new(severity: Severity, message: String) -> Self {
161 Self {
162 severity,
163 code: None,
164 message,
165 location: None,
166 hint: None,
167 source_chain: Vec::new(),
168 }
169 }
170
171 pub fn with_code(mut self, code: String) -> Self {
173 self.code = Some(code);
174 self
175 }
176
177 pub fn with_location(mut self, location: Location) -> Self {
179 self.location = Some(location);
180 self
181 }
182
183 pub fn with_hint(mut self, hint: String) -> Self {
185 self.hint = Some(hint);
186 self
187 }
188
189 pub fn with_source(mut self, source: &(dyn std::error::Error + 'static)) -> Self {
191 let mut current: Option<&(dyn std::error::Error + 'static)> = Some(source);
192 while let Some(err) = current {
193 self.source_chain.push(err.to_string());
194 current = err.source();
195 }
196 self
197 }
198
199 pub fn fmt_pretty(&self) -> String {
201 let mut result = format!(
202 "[{}] {}",
203 match self.severity {
204 Severity::Error => "ERROR",
205 Severity::Warning => "WARN",
206 Severity::Note => "NOTE",
207 },
208 self.message
209 );
210
211 if let Some(ref code) = self.code {
212 result.push_str(&format!(" ({})", code));
213 }
214
215 if let Some(ref loc) = self.location {
216 result.push_str(&format!("\n --> {}:{}:{}", loc.file, loc.line, loc.column));
217 }
218
219 if let Some(ref hint) = self.hint {
220 result.push_str(&format!("\n hint: {}", hint));
221 }
222
223 result
224 }
225
226 pub fn fmt_pretty_with_source(&self) -> String {
228 let mut result = self.fmt_pretty();
229
230 for (i, cause) in self.source_chain.iter().enumerate() {
231 result.push_str(&format!("\n cause {}: {}", i + 1, cause));
232 }
233
234 result
235 }
236}
237
238impl std::fmt::Display for Diagnostic {
239 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
240 write!(f, "{}", self.message)
241 }
242}
243
244#[derive(thiserror::Error, Debug)]
246pub enum ParseError {
247 #[error("Input too large: {size} bytes (max: {max} bytes)")]
249 InputTooLarge {
250 size: usize,
252 max: usize,
254 },
255
256 #[error("Invalid YAML structure: {0}")]
258 InvalidStructure(String),
259
260 #[error("YAML error at line {line}: {message}")]
262 YamlErrorWithLocation {
263 message: String,
265 line: usize,
267 block_index: usize,
269 },
270
271 #[error("{0}")]
273 Other(String),
274}
275
276impl ParseError {
277 pub fn to_diagnostic(&self) -> Diagnostic {
279 match self {
280 ParseError::InputTooLarge { size, max } => Diagnostic::new(
281 Severity::Error,
282 format!("Input too large: {} bytes (max: {} bytes)", size, max),
283 )
284 .with_code("parse::input_too_large".to_string()),
285 ParseError::InvalidStructure(msg) => Diagnostic::new(Severity::Error, msg.clone())
286 .with_code("parse::invalid_structure".to_string()),
287 ParseError::YamlErrorWithLocation {
288 message,
289 line,
290 block_index,
291 } => Diagnostic::new(
292 Severity::Error,
293 format!(
294 "YAML error at line {} (block {}): {}",
295 line, block_index, message
296 ),
297 )
298 .with_code("parse::yaml_error_with_location".to_string()),
299 ParseError::Other(msg) => Diagnostic::new(Severity::Error, msg.clone()),
300 }
301 }
302}
303
304impl From<Box<dyn std::error::Error + Send + Sync>> for ParseError {
305 fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
306 ParseError::Other(err.to_string())
307 }
308}
309
310impl From<String> for ParseError {
311 fn from(msg: String) -> Self {
312 ParseError::Other(msg)
313 }
314}
315
316impl From<&str> for ParseError {
317 fn from(msg: &str) -> Self {
318 ParseError::Other(msg.to_string())
319 }
320}
321
322#[derive(thiserror::Error, Debug)]
324pub enum RenderError {
325 #[error("{diag}")]
327 EngineCreation {
328 diag: Box<Diagnostic>,
330 },
331
332 #[error("{diag}")]
334 InvalidFrontmatter {
335 diag: Box<Diagnostic>,
337 },
338
339 #[error("Backend compilation failed with {} error(s)", diags.len())]
341 CompilationFailed {
342 diags: Vec<Diagnostic>,
344 },
345
346 #[error("{diag}")]
348 FormatNotSupported {
349 diag: Box<Diagnostic>,
351 },
352
353 #[error("{diag}")]
355 UnsupportedBackend {
356 diag: Box<Diagnostic>,
358 },
359
360 #[error("{diag}")]
362 ValidationFailed {
363 diag: Box<Diagnostic>,
365 },
366
367 #[error("{diag}")]
369 QuillConfig {
370 diag: Box<Diagnostic>,
372 },
373}
374
375impl RenderError {
376 pub fn diagnostics(&self) -> Vec<&Diagnostic> {
378 match self {
379 RenderError::CompilationFailed { diags } => diags.iter().collect(),
380 RenderError::EngineCreation { diag }
381 | RenderError::InvalidFrontmatter { diag }
382 | RenderError::FormatNotSupported { diag }
383 | RenderError::UnsupportedBackend { diag }
384 | RenderError::ValidationFailed { diag }
385 | RenderError::QuillConfig { diag } => vec![diag.as_ref()],
386 }
387 }
388}
389
390impl From<ParseError> for RenderError {
392 fn from(err: ParseError) -> Self {
393 RenderError::InvalidFrontmatter {
394 diag: Box::new(
395 Diagnostic::new(Severity::Error, err.to_string())
396 .with_code("parse::error".to_string()),
397 ),
398 }
399 }
400}
401
402#[derive(Debug)]
404pub struct RenderResult {
405 pub artifacts: Vec<crate::Artifact>,
407 pub warnings: Vec<Diagnostic>,
409 pub output_format: OutputFormat,
411}
412
413impl RenderResult {
414 pub fn new(artifacts: Vec<crate::Artifact>, output_format: OutputFormat) -> Self {
416 Self {
417 artifacts,
418 warnings: Vec::new(),
419 output_format,
420 }
421 }
422
423 pub fn with_warning(mut self, warning: Diagnostic) -> Self {
425 self.warnings.push(warning);
426 self
427 }
428}
429
430pub fn print_errors(err: &RenderError) {
432 for d in err.diagnostics() {
433 eprintln!("{}", d.fmt_pretty());
434 }
435}
436
437#[cfg(test)]
438mod tests {
439 use super::*;
440
441 #[test]
442 fn test_diagnostic_with_source_chain() {
443 let root_err = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
444 let diag =
445 Diagnostic::new(Severity::Error, "Rendering failed".to_string()).with_source(&root_err);
446
447 assert_eq!(diag.source_chain.len(), 1);
448 assert!(diag.source_chain[0].contains("File not found"));
449 }
450
451 #[test]
452 fn test_diagnostic_serialization() {
453 let diag = Diagnostic::new(Severity::Error, "Test error".to_string())
454 .with_code("E001".to_string())
455 .with_location(Location {
456 file: "test.typ".to_string(),
457 line: 10,
458 column: 5,
459 });
460
461 let json = serde_json::to_string(&diag).unwrap();
462 assert!(json.contains("Test error"));
463 assert!(json.contains("E001"));
464 assert!(json.contains("\"severity\":\"error\""));
465 assert!(json.contains("\"column\":5"));
466 }
467
468 #[test]
469 fn test_render_error_diagnostics_extraction() {
470 let diag1 = Diagnostic::new(Severity::Error, "Error 1".to_string());
471 let diag2 = Diagnostic::new(Severity::Error, "Error 2".to_string());
472
473 let err = RenderError::CompilationFailed {
474 diags: vec![diag1, diag2],
475 };
476
477 let diags = err.diagnostics();
478 assert_eq!(diags.len(), 2);
479 }
480
481 #[test]
482 fn test_diagnostic_fmt_pretty() {
483 let diag = Diagnostic::new(Severity::Warning, "Deprecated field used".to_string())
484 .with_code("W001".to_string())
485 .with_location(Location {
486 file: "input.md".to_string(),
487 line: 5,
488 column: 10,
489 })
490 .with_hint("Use the new field name instead".to_string());
491
492 let output = diag.fmt_pretty();
493 assert!(output.contains("[WARN]"));
494 assert!(output.contains("Deprecated field used"));
495 assert!(output.contains("W001"));
496 assert!(output.contains("input.md:5:10"));
497 assert!(output.contains("hint:"));
498 }
499
500 #[test]
501 fn test_diagnostic_fmt_pretty_with_source() {
502 let root_err = std::io::Error::other("Underlying error");
503 let diag = Diagnostic::new(Severity::Error, "Top-level error".to_string())
504 .with_code("E002".to_string())
505 .with_source(&root_err);
506
507 let output = diag.fmt_pretty_with_source();
508 assert!(output.contains("[ERROR]"));
509 assert!(output.contains("Top-level error"));
510 assert!(output.contains("cause 1:"));
511 assert!(output.contains("Underlying error"));
512 }
513
514 #[test]
515 fn test_render_result_with_warnings() {
516 let artifacts = vec![];
517 let warning = Diagnostic::new(Severity::Warning, "Test warning".to_string());
518
519 let result = RenderResult::new(artifacts, OutputFormat::Pdf).with_warning(warning);
520
521 assert_eq!(result.warnings.len(), 1);
522 assert_eq!(result.warnings[0].message, "Test warning");
523 }
524}