img_gen_spec/validators/layers/background.rs
1use super::{ColorKind, PreserveAspect};
2
3#[cfg(feature = "pyo3")]
4use pyo3::prelude::*;
5
6use serde::{Deserialize, Serialize};
7
8/// An attribute to describe a [`Layer`](struct@crate::Layer)'s
9/// ``background``.
10#[cfg_attr(
11 feature = "pyo3",
12 pyclass(module = "img_gen", get_all, set_all, from_py_object)
13)]
14#[derive(Debug, Clone, Default, Serialize, Deserialize)]
15pub struct Background {
16 /// A path to an image file.
17 ///
18 /// If the given image path has no file extension, then
19 /// it will be treated as an SVG image.
20 ///
21 /// This also supports built-in SVG icons from the following icon packs:
22 ///
23 /// - Material Design Icons (``material/{icon_slug}``)
24 /// - Simple Icons (``simple/{icon_slug}``)
25 /// - Octicons (``octicons/{icon_slug}``)
26 /// - FontAwesome Free (``fontawesome/<brands|solid|regular>/{icon_slug}``)
27 ///
28 /// Otherwise, the image file's path is resolved via a search through the list of
29 /// ``external_resource_paths`` provided to the ``Generator`` (in `img_gen_renderer` crate),
30 /// which defaults to the current working directory if unspecified or an empty list.
31 pub image: Option<String>,
32 /// A color overlaid on top of the `image`.
33 /// If no image is specified, then the layer is simple filled with this color.
34 pub color: Option<ColorKind>,
35 /// This controls how the original image is rendered into the layer.
36 /// Default is to preserve the original image's width and height.
37 #[serde(default)]
38 pub preserve_aspect: PreserveAspect,
39}
40
41#[cfg(test)]
42mod test {
43 #![allow(clippy::unwrap_used, clippy::panic)]
44
45 use super::Background;
46
47 #[test]
48 fn duplicate_color_last_wins() {
49 let yaml = "color: red\ncolor: blue\n";
50 let opts = serde_saphyr::options! {
51 duplicate_keys: serde_saphyr::options::DuplicateKeyPolicy::LastWins,
52 };
53 // Parse into an intermediate `serde_json::Value` with LastWins, then deserialize
54 let value: serde_json::Value = serde_saphyr::from_str_with_options(yaml, opts).unwrap();
55 let bg: Background = serde_json::from_value(value).unwrap();
56 assert!(bg.color.is_some());
57 match bg.color.unwrap() {
58 super::ColorKind::SolidColor(sc) => assert_eq!(sc.get_b(), 255u8),
59 other => panic!("unexpected color kind: {:?}", other),
60 }
61 }
62}