aw_test/models/
collection.rs

1#![allow(unused)]
2use serde::{Deserialize, Serialize, Deserializer};
3use std::collections::HashMap;
4use serde_json::value::Value;
5use std::fmt::Display;
6use super::*;
7
8#[derive(Debug, Serialize, Clone)]
9#[serde(deny_unknown_fields)]
10#[serde(untagged)]
11pub enum EmptyOption<T> {
12    Some(T),
13    None {},
14}
15
16impl<T> Display for EmptyOption<T>
17where
18    T: Display,
19{
20    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
21        match self {
22            EmptyOption::Some(t) => write!(f, "{}", t),
23            EmptyOption::None {} => write!(f, ""),
24        }
25    }
26}
27
28impl<'de, T> Deserialize<'de> for EmptyOption<T>
29where
30    T: Deserialize<'de>,
31{
32    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
33    where
34        D: Deserializer<'de>,
35    {
36        Option::deserialize(deserializer).map(Into::into)
37    }
38}
39
40impl<T> From<EmptyOption<T>> for Option<T> {
41    fn from(empty_option: EmptyOption<T>) -> Option<T> {
42        match empty_option {
43            EmptyOption::Some(option) => Some(option),
44            EmptyOption::None {} => None,
45        }
46    }
47}
48
49impl<T> From<Option<T>> for EmptyOption<T> {
50    fn from(option: Option<T>) -> EmptyOption<T> {
51        match option {
52            Some(option) => EmptyOption::Some(option),
53            None {} => EmptyOption::None {},
54        }
55    }
56}
57
58impl<T> EmptyOption<T> {
59    fn into_option(self) -> Option<T> {
60        self.into()
61    }
62    fn as_option(&self) -> Option<&T> {
63        match self {
64            EmptyOption::Some(option) => Some(option),
65            EmptyOption::None {} => None,
66        }
67    }
68}
69
70#[derive(Serialize, Deserialize, Debug, Clone)]
71pub struct Collection {
72        #[serde(rename(serialize = "id", deserialize = "$id"))]
73        pub id: String,
74        #[serde(rename(serialize = "read", deserialize = "$read"))]
75        pub read: Vec<String>,
76        #[serde(rename(serialize = "write", deserialize = "$write"))]
77        pub write: Vec<String>,
78        pub name: String,
79        pub enabled: bool,
80        pub permission: String,
81        pub attributes: Vec<Attribute>,
82        pub indexes: Vec<Index>,
83}
84
85impl Display for Collection {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        let mut formatBuffer = String::new();
88        formatBuffer.push_str(&format!("{:?}", self.id));
89        for item in &self.read {
90            formatBuffer.push_str(&format!("{:?}", item));
91        }
92        for item in &self.write {
93            formatBuffer.push_str(&format!("{:?}", item));
94        }
95        formatBuffer.push_str(&format!("{:?}", self.name));
96        formatBuffer.push_str(&format!("{:?}", self.enabled));
97        formatBuffer.push_str(&format!("{:?}", self.permission));
98        for item in &self.attributes {
99            formatBuffer.push_str(&format!("{:?}", item));
100        }
101        for item in &self.indexes {
102            formatBuffer.push_str(&format!("{:?}", item));
103        }
104
105        write!(f, "{}", formatBuffer)
106    }
107}
108
109impl Collection {
110    pub fn new(id: String, read: Vec<String>, write: Vec<String>, name: String, enabled: bool, permission: String, attributes: Vec<Attribute>, indexes: Vec<Index>, ) -> Self {
111        Self {
112            id: id,
113            read: read,
114            write: write,
115            name: name,
116            enabled: enabled,
117            permission: permission,
118            attributes: attributes,
119            indexes: indexes,
120            }
121    }
122}