memscope_rs/render_engine/
renderer.rs1use crate::snapshot::MemorySnapshot;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11pub enum OutputFormat {
12 Json,
14 Html,
16 Binary,
18 Csv,
20 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#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct RenderConfig {
39 pub format: OutputFormat,
41 pub output_path: Option<String>,
43 pub verbose: bool,
45 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#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct RenderResult {
63 pub data: Vec<u8>,
65 pub format: OutputFormat,
67 pub size: usize,
69}
70
71pub trait Renderer: Send + Sync {
75 fn format(&self) -> OutputFormat;
77
78 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}