Skip to main content

chordsketch_core/
render_result.rs

1//! Structured render result type for capturing warnings during rendering.
2
3/// Result of a render operation, containing both the rendered output
4/// and any warnings produced during rendering.
5///
6/// Renderers collect warnings (e.g., transpose saturation, chorus recall
7/// limits) into [`warnings`](Self::warnings) instead of printing them
8/// directly. Callers can inspect and display warnings as they see fit.
9#[derive(Debug, Clone)]
10#[must_use]
11pub struct RenderResult<T> {
12    /// The rendered output.
13    pub output: T,
14    /// Warnings emitted during rendering.
15    pub warnings: Vec<String>,
16}
17
18impl<T> RenderResult<T> {
19    /// Create a new `RenderResult` with the given output and no warnings.
20    pub fn new(output: T) -> Self {
21        Self {
22            output,
23            warnings: Vec::new(),
24        }
25    }
26
27    /// Create a new `RenderResult` with the given output and warnings.
28    pub fn with_warnings(output: T, warnings: Vec<String>) -> Self {
29        Self { output, warnings }
30    }
31
32    /// Returns `true` if there are no warnings.
33    #[must_use]
34    pub fn has_warnings(&self) -> bool {
35        !self.warnings.is_empty()
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_new_has_no_warnings() {
45        let result = RenderResult::new("hello");
46        assert_eq!(result.output, "hello");
47        assert!(result.warnings.is_empty());
48        assert!(!result.has_warnings());
49    }
50
51    #[test]
52    fn test_with_warnings() {
53        let result = RenderResult::with_warnings("output", vec!["warning 1".to_string()]);
54        assert_eq!(result.output, "output");
55        assert_eq!(result.warnings.len(), 1);
56        assert!(result.has_warnings());
57    }
58
59    #[test]
60    fn test_with_empty_warnings() {
61        let result = RenderResult::with_warnings(42, Vec::new());
62        assert_eq!(result.output, 42);
63        assert!(!result.has_warnings());
64    }
65}