wolf_graph_dot/
node_attributes.rs1use std::fmt;
2
3use crate::{Color, NodeShape, NodeStyle};
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct NodeAttributes {
7 pub label: Option<String>,
8 pub color: Option<Color>,
9 pub style: Option<NodeStyle>,
10 pub shape: Option<NodeShape>,
11}
12
13impl NodeAttributes {
14 pub fn new(
15 label: Option<String>,
16 color: Option<Color>,
17 style: Option<NodeStyle>,
18 shape: Option<NodeShape>,
19 ) -> Self {
20 Self { label, color, style, shape }
21 }
22
23 pub fn attributes(&self) -> Option<String> {
24 crate::details::encode::encode_attributes(&[
25 ("label", self.label.as_ref().map(|s| s as &dyn ToString)),
26 ("color", self.color.as_ref().map(|c| c as &dyn ToString)),
27 ("style", self.style.as_ref().map(|s| s as &dyn ToString)),
28 ("shape", self.shape.as_ref().map(|s| s as &dyn ToString)),
29 ], true)
30 }
31}
32
33impl Default for NodeAttributes {
34 fn default() -> Self {
35 Self::new(None, None, None, None)
36 }
37}
38
39impl fmt::Display for NodeAttributes {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 if let Some(encoded) = self.attributes() {
42 write!(f, "{}", encoded)
43 } else {
44 Ok(())
45 }
46 }
47}