graphviz_dot_builder/item/node/
node.rs

1/*
2Copyright 2020 Erwan Mahe (github.com/erwanM974)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17
18
19use crate::item::node::style::GraphvizNodeStyle;
20use crate::traits::{DotTranslatable, RenameableWithPrefix};
21
22#[derive(Eq,PartialEq,Clone)]
23pub struct GraphVizNode {
24    pub id : String,
25    pub style : GraphvizNodeStyle
26}
27
28impl GraphVizNode {
29    pub fn new(id : String,
30               style : GraphvizNodeStyle) -> GraphVizNode {
31        GraphVizNode{id,style}
32    }
33}
34
35impl DotTranslatable for GraphVizNode {
36    fn to_dot_string(&self) -> String {
37        let style : Vec<String> = self.style.iter().map(
38            |item| item.to_dot_string()).collect();
39
40        if style.is_empty() {
41            format!("{};", self.id)
42        } else {
43            format!("{} [{}];", self.id, style.join(","))
44        }
45    }
46}
47
48impl RenameableWithPrefix for GraphVizNode {
49    fn rename_with_prefix(&self, prefix: &str) -> Self {
50        GraphVizNode::new(format!("{}{}",prefix,self.id),self.style.clone())
51    }
52}
53