chart_js_rs/
doughnut.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use serde::{
    de::{self, DeserializeOwned},
    Deserialize, Serialize,
};

use crate::{objects::*, traits::*, ChartExt};

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct Doughnut<A: Annotation> {
    #[serde(rename = "type")]
    r#type: DoughnutString,
    data: Dataset<Vec<SinglePointDataset>>,
    options: ChartOptions<A>,
    id: String,
}
impl<A: Annotation + DeserializeOwned> ChartExt<A> for Doughnut<A> {
    type DS = Dataset<Vec<SinglePointDataset>>;

    fn get_id(self) -> String {
        self.id
    }
    fn id(mut self, id: String) -> Self {
        self.id = id;
        self
    }

    fn get_data(&mut self) -> &mut Self::DS {
        &mut self.data
    }

    fn get_options(&mut self) -> &mut ChartOptions<A> {
        &mut self.options
    }
}

#[derive(Debug, Default, Clone)]
pub struct DoughnutString;
impl<'de> Deserialize<'de> for DoughnutString {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        match String::deserialize(deserializer)?.to_lowercase().as_str() {
            "doughnut" => Ok(DoughnutString),
            other => Err(de::Error::custom(format!(
                "`{other}` is not a valid DoughnutString."
            ))),
        }
    }
}
impl Serialize for DoughnutString {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str("doughnut")
    }
}