rudof_rdf/rdf_core/visualizer/style/style.rs
1use crate::rdf_core::visualizer::{RDFVisualizationConfig, style::StereotypeStyle};
2
3/// Main style container for RDF visualization.
4///
5/// Represents a collection of stereotype styles that define the visual appearance
6/// of different RDF term types in PlantUML diagrams. This struct manages the
7/// overall styling configuration and provides methods to generate PlantUML style
8/// declarations.
9pub struct Style {
10 /// Collection of stereotype styles for different RDF term types.
11 stereotype_styles: Vec<StereotypeStyle>,
12}
13
14impl Style {
15 /// Creates a new empty `Style` instance.
16 ///
17 /// The returned style has no stereotype styles configured.
18 pub fn new() -> Self {
19 Style {
20 stereotype_styles: Vec::new(),
21 }
22 }
23
24 /// Adds a stereotype style to this style collection.
25 ///
26 /// # Arguments
27 /// * `style` - The `StereotypeStyle` to add to the collection.
28 pub fn add_stereotype_style(&mut self, style: StereotypeStyle) {
29 self.stereotype_styles.push(style);
30 }
31
32 /// Generates PlantUML style declarations for all configured stereotype styles.
33 ///
34 /// This method produces a complete PlantUML `<style>` block containing all
35 /// the stereotype definitions, followed by a directive to hide stereotypes
36 /// in the final diagram.
37 ///
38 /// # Returns
39 /// A string containing the PlantUML style declarations.
40 pub fn as_uml(&self) -> String {
41 let mut uml = String::new();
42 uml.push_str("<style>\n");
43 for style in &self.stereotype_styles {
44 uml.push_str(&style.as_plantuml());
45 }
46 uml.push_str("</style>\n");
47 uml.push_str("hide stereotype\n");
48 uml
49 }
50
51 /// Creates a `Style` instance from an RDF visualization configuration.
52 ///
53 /// This method automatically generates stereotype styles for all RDF term types
54 /// (URI nodes, blank nodes, literals, reifiers, asserted triples, non-asserted triples)
55 /// based on the provided configuration.
56 ///
57 /// # Arguments
58 /// * `config` - The `RDFVisualizationConfig` containing style settings.
59 ///
60 /// # Returns
61 /// A new `Style` instance with stereotype styles configured according to the config.
62 pub fn from_config(config: &RDFVisualizationConfig) -> Self {
63 let mut style = Style::new();
64 style.add_stereotype_style(reifier_style(config));
65 style.add_stereotype_style(literal_style(config));
66 style.add_stereotype_style(uri_style(config));
67 style.add_stereotype_style(bnode_style(config));
68 style.add_stereotype_style(asserted_style(config));
69 style.add_stereotype_style(non_asserted_style(config));
70 style
71 }
72}
73
74/// Creates a stereotype style for reifier nodes based on the configuration.
75///
76/// # Arguments
77/// * `config` - The RDF visualization configuration.
78///
79/// # Returns
80/// A `StereotypeStyle` configured for reifier nodes.
81fn reifier_style(config: &RDFVisualizationConfig) -> StereotypeStyle {
82 StereotypeStyle::new("reifier")
83 .with_background_color(config.reifier_background_color())
84 .with_line_color(config.reifier_line_color())
85 .with_line_thickness(config.reifier_line_thickness())
86}
87
88/// Creates a stereotype style for literal nodes based on the configuration.
89///
90/// # Arguments
91/// * `config` - The RDF visualization configuration.
92///
93/// # Returns
94/// A `StereotypeStyle` configured for literal nodes.
95fn literal_style(config: &RDFVisualizationConfig) -> StereotypeStyle {
96 StereotypeStyle::new("literal")
97 .with_background_color(config.literal_background_color())
98 .with_line_color(config.literal_line_color())
99 .with_line_thickness(config.literal_line_thickness())
100}
101
102/// Creates a stereotype style for URI nodes based on the configuration.
103///
104/// # Arguments
105/// * `config` - The RDF visualization configuration.
106///
107/// # Returns
108/// A `StereotypeStyle` configured for URI nodes.
109fn uri_style(config: &RDFVisualizationConfig) -> StereotypeStyle {
110 StereotypeStyle::new("uri")
111 .with_background_color(config.uri_background_color())
112 .with_line_color(config.uri_line_color())
113 .with_line_thickness(config.uri_line_thickness())
114 .with_round_corner(config.uri_round_corner())
115}
116
117/// Creates a stereotype style for blank nodes based on the configuration.
118///
119/// # Arguments
120/// * `config` - The RDF visualization configuration.
121///
122/// # Returns
123/// A `StereotypeStyle` configured for blank nodes.
124fn bnode_style(config: &RDFVisualizationConfig) -> StereotypeStyle {
125 StereotypeStyle::new("bnode")
126 .with_background_color(config.bnode_background_color())
127 .with_line_color(config.bnode_line_color())
128 .with_line_thickness(config.bnode_line_thickness())
129 .with_round_corner(config.bnode_round_corner())
130}
131
132/// Creates a stereotype style for asserted triple terms based on the configuration.
133///
134/// # Arguments
135/// * `config` - The RDF visualization configuration.
136///
137/// # Returns
138/// A `StereotypeStyle` configured for asserted triple terms.
139fn asserted_style(config: &RDFVisualizationConfig) -> StereotypeStyle {
140 StereotypeStyle::new("asserted")
141 .with_background_color(config.asserted_background_color())
142 .with_line_color(config.asserted_line_color())
143 .with_line_thickness(config.asserted_line_thickness())
144}
145
146/// Creates a stereotype style for non-asserted triple terms based on the configuration.
147///
148/// # Arguments
149/// * `config` - The RDF visualization configuration.
150///
151/// # Returns
152/// A `StereotypeStyle` configured for non-asserted triple terms.
153fn non_asserted_style(config: &RDFVisualizationConfig) -> StereotypeStyle {
154 StereotypeStyle::new("non_asserted")
155 .with_background_color(config.non_asserted_background_color())
156 .with_line_color(config.non_asserted_line_color())
157 .with_line_thickness(config.non_asserted_line_thickness())
158 .with_round_corner(config.non_asserted_round_corner())
159}
160
161impl Default for Style {
162 /// Returns a default `Style` instance with no stereotype styles configured.
163 ///
164 /// This is equivalent to calling `Style::new()`.
165 fn default() -> Self {
166 Style::new()
167 }
168}