teo_parser/availability/
mod.rs

1use std::fmt::{Display, Formatter};
2use serde::Serialize;
3
4static NO_DATABASE: u32 = 1;
5static MONGO: u32 = 1 << 1;
6static MYSQL: u32 = 1 << 2;
7static POSTGRES: u32 = 1 << 3;
8static SQLITE: u32 = 1 << 4;
9static SQL: u32 = MYSQL | POSTGRES | SQLITE;
10static DATABASE: u32 = MONGO | SQL;
11static ALL: u32 = DATABASE | NO_DATABASE;
12
13#[repr(transparent)]
14#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)]
15pub struct Availability(u32);
16
17impl Availability {
18
19    pub fn no_database() -> Self {
20        Self(NO_DATABASE)
21    }
22
23    pub fn none() -> Self {
24        Self(0)
25    }
26
27    pub fn database() -> Self {
28        Self(DATABASE)
29    }
30
31    pub fn mongo() -> Self {
32        Self(MONGO)
33    }
34
35    pub fn sql() -> Self {
36        Self(SQL)
37    }
38
39    pub fn mysql() -> Self {
40        Self(MYSQL)
41    }
42
43    pub fn postgres() -> Self {
44        Self(POSTGRES)
45    }
46
47    pub fn sqlite() -> Self {
48        Self(SQLITE)
49    }
50
51    pub fn contains(&self, actual: Availability) -> bool {
52        self.0 & actual.0 != 0
53    }
54
55    pub fn is_none(&self) -> bool {
56        self.0 == 0
57    }
58
59    pub fn bi_and(&self, other: Availability) -> Availability {
60        Self(self.0 & other.0)
61    }
62}
63
64impl Default for Availability {
65
66    fn default() -> Self {
67        Self(ALL)
68    }
69}
70
71impl Display for Availability {
72
73    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74        let mut to_join = vec![];
75        if self.0 & NO_DATABASE > 0 {
76            to_join.push("noDatabase");
77        }
78        if self.0 & MYSQL > 0 {
79            to_join.push("mysql");
80        }
81        if self.0 & POSTGRES > 0 {
82            to_join.push("postgres");
83        }
84        if self.0 & SQLITE > 0 {
85            to_join.push("sqlite");
86        }
87        if self.0 & MONGO > 0 {
88            to_join.push("mongo");
89        }
90        if to_join.is_empty() {
91            f.write_str(&"none")
92        } else {
93            f.write_str(&to_join.join(" | "))
94        }
95    }
96}