charming_fork_zephyr/element/
boundary_gap.rs

1use serde::{ser::SerializeSeq, Serialize};
2
3/// The boundary gap on both sides of a coordinate axis. The setting and
4/// behavior of category axes and non-category axes are different.
5///
6/// The `BoundaryGap` of category axis can be set to either `true` or `false`.
7/// Default value is `true`.
8///
9/// For non-category axis, including time, numerical value, and log axes,
10/// `BoundaryGap` is an array of two values, representing the spanning range
11/// between minimum and maximum value.
12pub enum BoundaryGap {
13    CategoryAxis(bool),
14    NonCategoryAxis(String, String),
15}
16
17impl Serialize for BoundaryGap {
18    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19    where
20        S: serde::ser::Serializer,
21    {
22        match self {
23            BoundaryGap::CategoryAxis(b) => b.serialize(serializer),
24            BoundaryGap::NonCategoryAxis(min, max) => {
25                let mut s = serializer.serialize_seq(Some(2))?;
26                s.serialize_element(min)?;
27                s.serialize_element(max)?;
28                s.end()
29            }
30        }
31    }
32}
33
34impl From<bool> for BoundaryGap {
35    fn from(b: bool) -> Self {
36        BoundaryGap::CategoryAxis(b)
37    }
38}
39
40impl From<(&str, &str)> for BoundaryGap {
41    fn from((min, max): (&str, &str)) -> Self {
42        BoundaryGap::NonCategoryAxis(min.to_string(), max.to_string())
43    }
44}