dot_ix_model/common/
images.rs

1use std::ops::{Deref, DerefMut};
2
3use indexmap::IndexMap;
4use serde::{Deserialize, Serialize};
5
6use crate::common::{dot_src_and_styles::GraphvizImage, ImageId};
7
8/// Images available for . `IndexMap<ImageId, GraphvizImage>` 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 Images(IndexMap<ImageId, GraphvizImage>);
40
41impl Images {
42    /// Returns a new `Images` map.
43    pub fn new() -> Self {
44        Self::default()
45    }
46
47    /// Returns a new `Images` 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<ImageId, GraphvizImage> {
55        self.0
56    }
57}
58
59impl Deref for Images {
60    type Target = IndexMap<ImageId, GraphvizImage>;
61
62    fn deref(&self) -> &Self::Target {
63        &self.0
64    }
65}
66
67impl DerefMut for Images {
68    fn deref_mut(&mut self) -> &mut Self::Target {
69        &mut self.0
70    }
71}
72
73impl From<IndexMap<ImageId, GraphvizImage>> for Images {
74    fn from(inner: IndexMap<ImageId, GraphvizImage>) -> Self {
75        Self(inner)
76    }
77}
78
79impl FromIterator<(ImageId, GraphvizImage)> for Images {
80    fn from_iter<I: IntoIterator<Item = (ImageId, GraphvizImage)>>(iter: I) -> Self {
81        Self(IndexMap::from_iter(iter))
82    }
83}