Skip to main content

dendryform_core/
legend.rs

1//! Legend entry type.
2
3use serde::{Deserialize, Serialize};
4
5use crate::color::Color;
6
7/// A single entry in the diagram legend.
8///
9/// Rendered as a colored swatch with a label.
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub struct LegendEntry {
13    color: Color,
14    label: String,
15}
16
17impl LegendEntry {
18    /// Creates a new legend entry.
19    pub fn new(color: Color, label: &str) -> Self {
20        Self {
21            color,
22            label: label.to_owned(),
23        }
24    }
25
26    /// Returns the swatch color.
27    pub fn color(&self) -> Color {
28        self.color
29    }
30
31    /// Returns the label text.
32    pub fn label(&self) -> &str {
33        &self.label
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_new() {
43        let entry = LegendEntry::new(Color::Blue, "Clients");
44        assert_eq!(entry.color(), Color::Blue);
45        assert_eq!(entry.label(), "Clients");
46    }
47
48    #[test]
49    fn test_serde_round_trip() {
50        let entry = LegendEntry::new(Color::Purple, "Business Logic");
51        let json = serde_json::to_string(&entry).unwrap();
52        let deserialized: LegendEntry = serde_json::from_str(&json).unwrap();
53        assert_eq!(entry, deserialized);
54    }
55}