pnwkit_core/
data.rs

1use dashmap::DashMap;
2use serde::{de::Visitor, Deserialize};
3
4use crate::Value;
5
6pub type Object = DashMap<String, Value>;
7
8type Errors = Vec<Error>;
9
10#[derive(Clone, Debug, Deserialize)]
11pub struct Error {
12    pub(crate) message: String,
13}
14
15// type Extensions = Vec<Extension>;
16
17// #[derive(Clone, Debug, Deserialize)]
18// pub struct Extension {
19//     pub(crate) key: String,
20//     pub(crate) version: u8,
21// }
22
23#[derive(Clone, Debug, Deserialize)]
24pub struct QueryReturn {
25    pub(crate) errors: Option<Errors>,
26    pub(crate) data: Option<Data>,
27    // pub(crate) extensions: Option<Extensions>,
28}
29
30#[derive(Clone, Debug)]
31pub struct Data(pub Object);
32
33impl Data {
34    pub fn inner(self) -> Object {
35        self.0
36    }
37}
38
39impl<'de> Deserialize<'de> for Data {
40    fn deserialize<D>(deserializer: D) -> Result<Data, D::Error>
41    where
42        D: serde::Deserializer<'de>,
43    {
44        Ok(Self(deserializer.deserialize_map(ObjectVisitor::new())?))
45    }
46}
47
48#[derive(Clone, Debug)]
49pub struct ObjectVisitor {}
50
51impl ObjectVisitor {
52    pub fn new() -> Self {
53        Self {}
54    }
55}
56
57impl<'de> Visitor<'de> for ObjectVisitor {
58    type Value = Object;
59
60    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
61        formatter.write_str("a map")
62    }
63
64    fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error>
65    where
66        A: serde::de::MapAccess<'de>,
67    {
68        let map = Object::with_capacity(access.size_hint().unwrap_or(0));
69
70        while let Some((key, value)) = access.next_entry()? {
71            map.insert(key, value);
72        }
73
74        Ok(map)
75    }
76}
77
78#[cfg(feature = "subscriptions")]
79#[derive(Clone, Debug, Deserialize)]
80pub struct SubscriptionAuthData {
81    pub(crate) auth: String,
82}