Skip to main content

memscope_rs/render_engine/
renderer.rs

1//! Renderer trait for pluggable renderers
2//!
3//! This module defines the Renderer trait and output formats.
4
5use crate::snapshot::MemorySnapshot;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9/// Output format type
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11pub enum OutputFormat {
12    /// JSON format
13    Json,
14    /// HTML format
15    Html,
16    /// Binary format
17    Binary,
18    /// CSV format
19    Csv,
20    /// SVG format
21    Svg,
22}
23
24impl fmt::Display for OutputFormat {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            OutputFormat::Json => write!(f, "json"),
28            OutputFormat::Html => write!(f, "html"),
29            OutputFormat::Binary => write!(f, "binary"),
30            OutputFormat::Csv => write!(f, "csv"),
31            OutputFormat::Svg => write!(f, "svg"),
32        }
33    }
34}
35
36/// Render configuration
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct RenderConfig {
39    /// Output format
40    pub format: OutputFormat,
41    /// Output path (if writing to file)
42    pub output_path: Option<String>,
43    /// Whether to include detailed information
44    pub verbose: bool,
45    /// Whether to include timestamps
46    pub include_timestamps: bool,
47}
48
49impl Default for RenderConfig {
50    fn default() -> Self {
51        Self {
52            format: OutputFormat::Json,
53            output_path: None,
54            verbose: false,
55            include_timestamps: true,
56        }
57    }
58}
59
60/// Render result
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct RenderResult {
63    /// The rendered data as bytes
64    pub data: Vec<u8>,
65    /// Output format used
66    pub format: OutputFormat,
67    /// Number of bytes rendered
68    pub size: usize,
69}
70
71/// Renderer trait for pluggable output formats
72///
73/// All renderers must implement this trait to be used with the RenderEngine.
74pub trait Renderer: Send + Sync {
75    /// Get the output format of this renderer
76    fn format(&self) -> OutputFormat;
77
78    /// Render a memory snapshot
79    ///
80    /// # Arguments
81    /// * `snapshot` - The memory snapshot to render
82    /// * `config` - Render configuration
83    ///
84    /// # Returns
85    /// Result containing the rendered data or an error
86    fn render(
87        &self,
88        snapshot: &MemorySnapshot,
89        config: &RenderConfig,
90    ) -> Result<RenderResult, String>;
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_output_format_display() {
99        assert_eq!(OutputFormat::Json.to_string(), "json");
100        assert_eq!(OutputFormat::Html.to_string(), "html");
101        assert_eq!(OutputFormat::Binary.to_string(), "binary");
102    }
103
104    #[test]
105    fn test_render_config_default() {
106        let config = RenderConfig::default();
107        assert_eq!(config.format, OutputFormat::Json);
108        assert!(!config.verbose);
109        assert!(config.include_timestamps);
110    }
111}