Skip to main content

img_gen_spec/validators/layers/
ellipse.rs

1#[cfg(feature = "pyo3")]
2use pyo3::prelude::*;
3
4use serde::{Deserialize, Serialize};
5
6use super::{Border, ColorKind};
7
8/// An attribute to represent an Arc rendered in the layer.
9#[derive(Debug, Clone, Default, Serialize, Deserialize)]
10#[cfg_attr(
11    feature = "pyo3",
12    pyclass(module = "img_gen", get_all, set_all, from_py_object)
13)]
14pub struct Arc {
15    /// The starting angle of the arc in degrees.
16    pub start: f32,
17    /// The ending angle of the arc in degrees.
18    pub end: f32,
19}
20
21/// An attribute to represent an Ellipse rendered in the layer.
22#[derive(Debug, Clone, Default, Serialize, Deserialize)]
23#[cfg_attr(
24    feature = "pyo3",
25    pyclass(module = "img_gen", get_all, set_all, from_py_object)
26)]
27pub struct Ellipse {
28    /// The [`Border`] (if specified) ro render around the ellipse.
29    pub border: Option<Border>,
30    /// The color used to fill the ellipse.
31    #[serde(default = "ColorKind::transparent_default")]
32    pub color: ColorKind,
33    /// The Arc (if specified) to render instead of a full ellipse.
34    pub arc: Option<Arc>,
35    /// Extend the border of an arc to the center of the ellipse, creating a pie shape.
36    ///
37    /// Does not affect full ellipses, and is ignored if `arc` is not specified.
38    #[serde(default, alias = "border-to-origin")]
39    pub border_to_origin: bool,
40}
41
42#[cfg(test)]
43mod tests {
44    #![allow(clippy::unwrap_used)]
45
46    use super::*;
47
48    #[test]
49    fn deserialize_default_ellipse() {
50        let json = r#"{}"#;
51        let ellipse: Ellipse = serde_json::from_str(json).unwrap();
52        assert_eq!(ellipse.color.get_color_tuple_at(0, 0), (0, 0, 0, 0));
53        assert!(ellipse.border.is_none());
54        assert!(ellipse.arc.is_none());
55        assert!(!ellipse.border_to_origin);
56    }
57}