hydro_lang/viz/
config.rs

1use clap::{Parser, ValueEnum};
2
3/// Enum for choosing between mermaid, dot, and json graph writing.
4#[derive(Copy, Clone, Debug, ValueEnum)]
5pub enum GraphType {
6    /// Mermaid graphs.
7    Mermaid,
8    /// Dot (Graphviz) graphs.
9    Dot,
10    /// JSON format for interactive graphs.
11    Json,
12}
13
14impl std::fmt::Display for GraphType {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        write!(f, "{:?}", self)
17    }
18}
19
20/// Configuration for graph generation in examples.
21#[derive(Parser, Debug, Default)]
22pub struct GraphConfig {
23    /// Graph format to generate and display
24    #[clap(long)]
25    pub graph: Option<GraphType>,
26
27    /// Write graph to file instead of opening in browser
28    #[clap(long)]
29    pub file: bool,
30
31    /// Don't show metadata in graph nodes
32    #[clap(long)]
33    pub no_metadata: bool,
34
35    /// Don't show location groups
36    #[clap(long)]
37    pub no_location_groups: bool,
38
39    /// Don't include tee IDs in nodes
40    #[clap(long)]
41    pub no_tee_ids: bool,
42
43    /// Use full/long labels instead of short ones
44    #[clap(long)]
45    pub long_labels: bool,
46}
47
48impl GraphConfig {
49    /// Returns true if the program should exit after generating a graph file.
50    /// This happens when both --file and --graph flags are provided.
51    pub fn should_exit_after_graph_generation(&self) -> bool {
52        self.file && self.graph.is_some()
53    }
54}
55
56/// Configuration for visualizer URL generation and compression.
57/// Controls how graphs are encoded and opened in web browsers.
58#[derive(Debug, Clone)]
59pub struct VisualizerConfig {
60    /// Base URL for the visualizer (default: <https://hydro.run/hydroscope>)
61    pub base_url: String,
62    /// Whether to enable compression for small graphs
63    pub enable_compression: bool,
64    /// Maximum URL length before falling back to file-based approach
65    pub max_url_length: usize,
66    /// Minimum JSON size to attempt compression
67    pub min_compression_size: usize,
68}
69
70impl Default for VisualizerConfig {
71    fn default() -> Self {
72        // Check for environment variable override for local development
73        let base_url = std::env::var("HYDRO_VISUALIZER_URL")
74            .unwrap_or_else(|_| "https://hydro.run/docs/hydroscope".to_string());
75
76        Self {
77            base_url,
78            enable_compression: true,
79            max_url_length: 4000,
80            min_compression_size: 1000,
81        }
82    }
83}
84
85impl VisualizerConfig {
86    /// Create a new configuration with custom base URL
87    pub fn with_base_url(base_url: impl Into<String>) -> Self {
88        Self {
89            base_url: base_url.into(),
90            ..Default::default()
91        }
92    }
93
94    /// Create a configuration for local development
95    pub fn local() -> Self {
96        Self::with_base_url("http://localhost:3000/hydroscope")
97    }
98
99    /// Disable compression (useful for debugging)
100    pub fn without_compression(mut self) -> Self {
101        self.enable_compression = false;
102        self
103    }
104}