rtdlib/types/
statistical_value.rs

1
2use crate::types::*;
3use crate::errors::*;
4use uuid::Uuid;
5
6
7
8
9/// A value with information about its recent changes
10#[derive(Debug, Clone, Default, Serialize, Deserialize)]
11pub struct StatisticalValue {
12  #[doc(hidden)]
13  #[serde(rename(serialize = "@type", deserialize = "@type"))]
14  td_name: String,
15  #[doc(hidden)]
16  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
17  extra: Option<String>,
18  /// The current value
19  value: f32,
20  /// The value for the previous day
21  previous_value: f32,
22  /// The growth rate of the value, as a percentage
23  growth_rate_percentage: f32,
24  
25}
26
27impl RObject for StatisticalValue {
28  #[doc(hidden)] fn td_name(&self) -> &'static str { "statisticalValue" }
29  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
30  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
31}
32
33
34
35impl StatisticalValue {
36  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
37  pub fn builder() -> RTDStatisticalValueBuilder {
38    let mut inner = StatisticalValue::default();
39    inner.td_name = "statisticalValue".to_string();
40    inner.extra = Some(Uuid::new_v4().to_string());
41    RTDStatisticalValueBuilder { inner }
42  }
43
44  pub fn value(&self) -> f32 { self.value }
45
46  pub fn previous_value(&self) -> f32 { self.previous_value }
47
48  pub fn growth_rate_percentage(&self) -> f32 { self.growth_rate_percentage }
49
50}
51
52#[doc(hidden)]
53pub struct RTDStatisticalValueBuilder {
54  inner: StatisticalValue
55}
56
57impl RTDStatisticalValueBuilder {
58  pub fn build(&self) -> StatisticalValue { self.inner.clone() }
59
60   
61  pub fn value(&mut self, value: f32) -> &mut Self {
62    self.inner.value = value;
63    self
64  }
65
66   
67  pub fn previous_value(&mut self, previous_value: f32) -> &mut Self {
68    self.inner.previous_value = previous_value;
69    self
70  }
71
72   
73  pub fn growth_rate_percentage(&mut self, growth_rate_percentage: f32) -> &mut Self {
74    self.inner.growth_rate_percentage = growth_rate_percentage;
75    self
76  }
77
78}
79
80impl AsRef<StatisticalValue> for StatisticalValue {
81  fn as_ref(&self) -> &StatisticalValue { self }
82}
83
84impl AsRef<StatisticalValue> for RTDStatisticalValueBuilder {
85  fn as_ref(&self) -> &StatisticalValue { &self.inner }
86}
87
88
89