chart_js_rs/
doughnut.rs

1use serde::{
2    de::{self, DeserializeOwned},
3    Deserialize, Serialize,
4};
5
6use crate::{objects::*, traits::*, ChartExt};
7
8#[derive(Debug, Clone, Deserialize, Serialize, Default)]
9pub struct Doughnut<A: Annotation> {
10    #[serde(rename = "type")]
11    r#type: DoughnutString,
12    data: Dataset<Vec<SinglePointDataset>>,
13    options: ChartOptions<A>,
14    id: String,
15}
16impl<A: Annotation + DeserializeOwned> ChartExt<A> for Doughnut<A> {
17    type DS = Dataset<Vec<SinglePointDataset>>;
18
19    fn get_id(self) -> String {
20        self.id
21    }
22    fn id(mut self, id: String) -> Self {
23        self.id = id;
24        self
25    }
26
27    fn get_data(&mut self) -> &mut Self::DS {
28        &mut self.data
29    }
30
31    fn get_options(&mut self) -> &mut ChartOptions<A> {
32        &mut self.options
33    }
34}
35
36#[derive(Debug, Default, Clone)]
37pub struct DoughnutString;
38impl<'de> Deserialize<'de> for DoughnutString {
39    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
40    where
41        D: serde::Deserializer<'de>,
42    {
43        match String::deserialize(deserializer)?.to_lowercase().as_str() {
44            "doughnut" => Ok(DoughnutString),
45            other => Err(de::Error::custom(format!(
46                "`{other}` is not a valid DoughnutString."
47            ))),
48        }
49    }
50}
51impl Serialize for DoughnutString {
52    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
53    where
54        S: serde::Serializer,
55    {
56        serializer.serialize_str("doughnut")
57    }
58}