haproxy_stats/formats/
json.rs

1use serde::Deserialize;
2use serde_json::Value as SerdeJsonValue;
3
4#[derive(Deserialize, Debug, Clone)]
5pub struct Field {
6    pub pos: usize,
7    pub name: Box<str>,
8}
9
10#[derive(Deserialize, Debug, Clone)]
11pub struct Tags {
12    pub origin: Box<str>,
13    pub nature: Box<str>,
14    pub scope: Box<str>,
15}
16
17#[derive(Deserialize, Debug, Clone)]
18#[serde(tag = "type", content = "value")]
19pub enum Value {
20    #[serde(rename = "s32")]
21    S32(i32),
22    #[serde(rename = "s64")]
23    S64(i64),
24    #[serde(rename = "u32")]
25    U32(u32),
26    #[serde(rename = "u64")]
27    U64(u64),
28    #[serde(rename = "str")]
29    Str(Box<str>),
30}
31
32impl Value {
33    pub fn as_i32(&self) -> Option<i32> {
34        match self {
35            Self::S32(v) => Some(*v),
36            _ => None,
37        }
38    }
39
40    pub fn as_i64(&self) -> Option<i64> {
41        match self {
42            Self::S32(v) => Some(*v as i64),
43            Self::S64(v) => Some(*v),
44            _ => None,
45        }
46    }
47
48    pub fn as_u32(&self) -> Option<u32> {
49        match self {
50            Self::U32(v) => Some(*v),
51            _ => None,
52        }
53    }
54
55    pub fn as_u64(&self) -> Option<u64> {
56        match self {
57            Self::U32(v) => Some(*v as u64),
58            Self::U64(v) => Some(*v),
59            _ => None,
60        }
61    }
62
63    pub fn as_str(&self) -> Option<&str> {
64        match self {
65            Self::Str(v) => Some(v),
66            _ => None,
67        }
68    }
69
70    pub fn value_to_string(&self) -> String {
71        match self {
72            Self::S32(v) => v.to_string(),
73            Self::S64(v) => v.to_string(),
74            Self::U32(v) => v.to_string(),
75            Self::U64(v) => v.to_string(),
76            Self::Str(v) => v.to_string(),
77        }
78    }
79}
80
81impl From<&Value> for SerdeJsonValue {
82    fn from(v: &Value) -> Self {
83        match v {
84            Value::S32(v) => SerdeJsonValue::Number((*v).into()),
85            Value::S64(v) => SerdeJsonValue::Number((*v).into()),
86            Value::U32(v) => SerdeJsonValue::Number((*v).into()),
87            Value::U64(v) => SerdeJsonValue::Number((*v).into()),
88            Value::Str(v) => SerdeJsonValue::String(v.to_string()),
89        }
90    }
91}