colorimetry_plot/
layer.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2// Copyright (c) 2025, Harbers Bik LLC
3
4//! Layer module for SVG rendering
5//!
6//! This module provides the `Layer` struct, which represents a layer in an SVG document.
7//! It allows adding SVG nodes, setting attributes, and provides methods for rendering.
8use std::ops::Deref;
9
10use svg::{node::element::Group, Node};
11
12#[derive(Debug, Clone, Default)]
13pub struct Layer(pub(crate) Group);
14
15impl Layer {
16    pub fn new() -> Self {
17        Layer(Group::new())
18    }
19
20    pub fn add_layer(self, node: impl Into<Box<dyn Node>>) -> Self {
21        Layer(self.0.add(node))
22    }
23
24    pub fn set(self, key: &str, value: impl Into<String>) -> Self {
25        Layer(self.0.set(key, value.into()))
26    }
27
28    /*
29    pub fn set_class_and_style(self, class: Option<&str>, style: Option<&str>) -> Self {
30        let mut layer = self;
31        if let Some(c) = class {
32            layer = layer.set("class", c);
33        }
34        if let Some(s) = style {
35            layer = layer.set("style", s);
36        }
37        layer
38    }
39     */
40}
41
42impl From<Layer> for Box<dyn Node> {
43    fn from(layer: Layer) -> Self {
44        Box::new(layer.0)
45    }
46}
47
48impl std::fmt::Display for Layer {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "{}", self.0)
51    }
52}
53
54impl Deref for Layer {
55    type Target = Group;
56
57    fn deref(&self) -> &Self::Target {
58        &self.0
59    }
60}
61
62impl std::ops::DerefMut for Layer {
63    fn deref_mut(&mut self) -> &mut Self::Target {
64        &mut self.0
65    }
66}