lib_humus_configuration/
settings.rs1use serde::{Deserialize, Serialize};
6
7use crate::ConfigFormat;
8
9#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
11pub struct Settings {
12 pub prefer: ConfigFormat,
14
15 #[cfg(feature="json")]
17 pub allow_json: bool,
18
19 #[cfg(feature="json5")]
23 pub allow_json5: bool,
24
25 #[cfg(feature="toml")]
27 pub allow_toml: bool,
28}
29
30impl Settings {
31 pub const fn allow_all(prefer: ConfigFormat) -> Self {
34 Self {
35 prefer,
36 #[cfg(feature="json")]
37 allow_json: true,
38 #[cfg(feature="json5")]
39 allow_json5: true,
40 #[cfg(feature="toml")]
41 allow_toml: true,
42 }
43 }
44
45 #[cfg(feature="json")]
47 pub const fn prefer_json() -> Self {
48 Self::allow_all(ConfigFormat::Json)
49 }
50
51 #[cfg(feature="json5")]
53 pub const fn prefer_json5() -> Self {
54 Self::allow_all(ConfigFormat::Json5)
55 }
56
57 #[cfg(feature="toml")]
59 pub const fn prefer_toml() -> Self {
60 Self::allow_all(ConfigFormat::Toml)
61 }
62
63 pub const fn allow_only(prefer: ConfigFormat) -> Self {
65 Self {
66 prefer,
67 #[cfg(feature="json")]
68 allow_json: false,
69 #[cfg(feature="json5")]
70 allow_json5: false,
71 #[cfg(feature="toml")]
72 allow_toml: false,
73 }
74 }
75
76 #[cfg(feature="json")]
78 pub const fn only_json() -> Self {
79 Self::allow_only(ConfigFormat::Json)
80 }
81
82 #[cfg(feature="json5")]
84 pub const fn only_json5() -> Self {
85 Self::allow_only(ConfigFormat::Json5)
86 }
87
88 #[cfg(feature="toml")]
90 pub const fn only_toml() -> Self {
91 Self::allow_only(ConfigFormat::Toml)
92 }
93
94 pub fn is_allowed(&self, test_for: ConfigFormat) -> bool {
96 if test_for == self.prefer {
97 return true;
98 }
99 match test_for {
100 #[cfg(feature="json")]
101 ConfigFormat::Json => {
102 #[cfg(not(feature="json5"))]
103 {
104 self.allow_json
105 }
106 #[cfg(feature="json5")]
107 {
108 self.allow_json || self.allow_json5 || matches!(self.prefer, ConfigFormat::Json5)
109 }
110 }
111 #[cfg(feature="json5")]
112 ConfigFormat::Json5 => self.allow_json5,
113 #[cfg(feature="toml")]
114 ConfigFormat::Toml => self.allow_toml,
115 }
116 }
117}