Skip to main content

rudof_rdf/rdf_core/visualizer/style/
arrow_style.rs

1use crate::rdf_core::visualizer::style::{ThicknessStyle, UmlColor};
2use serde::{Deserialize, Serialize};
3
4/// Default line thickness used when no explicit thickness is specified.
5const DEFAULT_ARROW_LINE_THICKNESS: ThicknessStyle = ThicknessStyle::Normal;
6/// Default line color used when no explicit color is specified.
7const DEFAULT_ARROW_LINE_COLOR: UmlColor = UmlColor::Black;
8/// Default text color used when no explicit color is specified.
9const DEFAULT_ARROW_TEXT_COLOR: UmlColor = UmlColor::Black;
10
11/// Default style for subject arrows.
12pub const DEFAULT_SUBJECT_ARROW_STYLE: ArrowStyle = ArrowStyle {
13    line_color: Some(UmlColor::Blue),
14    line_thickness: Some(ThicknessStyle::Dashed),
15    text_color: Some(UmlColor::Blue),
16};
17/// Default style for predicate arrows.
18pub const DEFAULT_PREDICATE_ARROW_STYLE: ArrowStyle = ArrowStyle {
19    line_color: Some(UmlColor::Red),
20    line_thickness: Some(ThicknessStyle::Dashed),
21    text_color: Some(UmlColor::Red),
22};
23/// Default style for object arrows.
24pub const DEFAULT_OBJECT_ARROW_STYLE: ArrowStyle = ArrowStyle {
25    line_color: Some(UmlColor::Green),
26    line_thickness: Some(ThicknessStyle::Dashed),
27    text_color: Some(UmlColor::Green),
28};
29
30/// Defines the visual style of an arrow in a PlantUML diagram.
31#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
32pub struct ArrowStyle {
33    /// Color of the arrow line.
34    pub line_color: Option<UmlColor>,
35    /// Thickness and style of the arrow line.
36    pub line_thickness: Option<ThicknessStyle>,
37    /// Color of the arrow text/label.
38    pub text_color: Option<UmlColor>,
39}
40
41impl ArrowStyle {
42    /// Creates a new [`ArrowStyle`] initialized with default values.
43    pub fn new() -> Self {
44        ArrowStyle {
45            line_color: Some(DEFAULT_ARROW_LINE_COLOR),
46            line_thickness: Some(DEFAULT_ARROW_LINE_THICKNESS),
47            text_color: Some(DEFAULT_ARROW_TEXT_COLOR),
48        }
49    }
50
51    /// Sets the line color and returns the modified [`ArrowStyle`].
52    pub fn with_line_color(mut self, color: UmlColor) -> Self {
53        self.line_color = Some(color);
54        self
55    }
56
57    /// Sets the line thickness and returns the modified [`ArrowStyle`].
58    pub fn with_line_thickness(mut self, thickness: ThicknessStyle) -> Self {
59        self.line_thickness = Some(thickness);
60        self
61    }
62
63    /// Sets the text color and returns the modified [`ArrowStyle`].
64    pub fn with_text_color(mut self, color: UmlColor) -> Self {
65        self.text_color = Some(color);
66        self
67    }
68
69    /// Converts the arrow style into a PlantUML-compatible style string.
70    ///
71    /// # Returns
72    /// A `String` representing the arrow style in PlantUML syntax.
73    pub fn as_plantuml(&self) -> String {
74        format!(
75            "#line:{};{}text:{}",
76            self.line_color
77                .as_ref()
78                .map(|c| c.as_plantuml().to_lowercase())
79                .unwrap_or_default(),
80            self.line_thickness
81                .as_ref()
82                .map(|t| t.as_plantuml().to_lowercase())
83                .unwrap_or_default(),
84            self.text_color
85                .as_ref()
86                .map(|c| c.as_plantuml().to_lowercase())
87                .unwrap_or_default()
88        )
89    }
90}
91
92impl Default for ArrowStyle {
93    fn default() -> Self {
94        Self::new()
95    }
96}