dot_ix_model/common/node_images.rs
1use std::ops::{Deref, DerefMut};
2
3use indexmap::IndexMap;
4use serde::{Deserialize, Serialize};
5
6use crate::common::{ImageId, NodeId};
7
8/// Image to insert for each node. `IndexMap<NodeId, ImageId>` newtype.
9///
10/// # Examples
11///
12/// Each URL should either be a hyperlink to an image, or a base64 encoded
13/// data URL:
14///
15/// ```yaml
16/// hierarchy:
17/// dove: {}
18/// blue: {}
19///
20/// images:
21/// dove_svg:
22/// path: "https://peace.mk/img/dove.svg"
23/// width: "50px"
24/// height: "50px"
25///
26/// blue_inline:
27/// path: >-
28/// data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZBAMAAAA2x5hQAAAAAX
29/// NSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAADUExURUeK/z7BOdMAAAAJcEhZcwAADsIAAA
30/// 7CARUoSoAAAAAOSURBVCjPYxgFNAMMDAABXgABAvs87wAAAABJRU5ErkJggg==
31/// width: "50px"
32/// height: "50px"
33///
34/// node_images:
35/// dove: dove_svg
36/// blue: blue_inline
37/// ```
38#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
39pub struct NodeImages(IndexMap<NodeId, ImageId>);
40
41impl NodeImages {
42 /// Returns a new `NodeImages` map.
43 pub fn new() -> Self {
44 Self::default()
45 }
46
47 /// Returns a new `NodeImages` map with the given preallocated
48 /// capacity.
49 pub fn with_capacity(capacity: usize) -> Self {
50 Self(IndexMap::with_capacity(capacity))
51 }
52
53 /// Returns the underlying map.
54 pub fn into_inner(self) -> IndexMap<NodeId, ImageId> {
55 self.0
56 }
57}
58
59impl Deref for NodeImages {
60 type Target = IndexMap<NodeId, ImageId>;
61
62 fn deref(&self) -> &Self::Target {
63 &self.0
64 }
65}
66
67impl DerefMut for NodeImages {
68 fn deref_mut(&mut self) -> &mut Self::Target {
69 &mut self.0
70 }
71}
72
73impl From<IndexMap<NodeId, ImageId>> for NodeImages {
74 fn from(inner: IndexMap<NodeId, ImageId>) -> Self {
75 Self(inner)
76 }
77}
78
79impl FromIterator<(NodeId, ImageId)> for NodeImages {
80 fn from_iter<I: IntoIterator<Item = (NodeId, ImageId)>>(iter: I) -> Self {
81 Self(IndexMap::from_iter(iter))
82 }
83}