lib_humus_configuration/
settings.rs

1// SPDX-FileCopyrightText: 2025 Slatian
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5use serde::{Deserialize, Serialize};
6
7use crate::ConfigFormat;
8
9/// Which formats are allowed and which to prefer
10#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
11pub struct Settings {
12	/// The preferred configuration format. This is always allowed implicitly.
13	pub prefer: ConfigFormat,
14
15	/// Explicit permission to parse as JSON
16	#[cfg(feature="json")]
17	pub allow_json: bool,
18
19	/// Explicit permission to parse as JSON5.
20	///
21	/// Implicitly allows JSON as it is a subset of JSON5.
22	#[cfg(feature="json5")]
23	pub allow_json5: bool,
24
25	/// Explicit permission to parse as TOML
26	#[cfg(feature="toml")]
27	pub allow_toml: bool,
28}
29
30impl Settings {
31	/// Creates a new settings instance that allows all formats
32	/// but prefers the given one.
33	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	/// Allow all formats, but  prefer JSON.
46	#[cfg(feature="json")]
47	pub const fn prefer_json() -> Self {
48		Self::allow_all(ConfigFormat::Json)
49	}
50
51	/// Allow all formats, but prefer JSON5.
52	#[cfg(feature="json5")]
53	pub const fn prefer_json5() -> Self {
54		Self::allow_all(ConfigFormat::Json5)
55	}
56
57	/// Allow all formats, but prefer TOML.
58	#[cfg(feature="toml")]
59	pub const fn prefer_toml() -> Self {
60		Self::allow_all(ConfigFormat::Toml)
61	}
62
63	/// Creates a new settings instance that only allows the given preferred format.
64	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	/// Only allows parsing as JSON.
77	#[cfg(feature="json")]
78	pub const fn only_json() -> Self {
79		Self::allow_only(ConfigFormat::Json)
80	}
81
82	/// Only allows parsing as JSON5 (and JSON).
83	#[cfg(feature="json5")]
84	pub const fn only_json5() -> Self {
85		Self::allow_only(ConfigFormat::Json5)
86	}
87
88	/// Only allow parsing as TOML.
89	#[cfg(feature="toml")]
90	pub const fn only_toml() -> Self {
91		Self::allow_only(ConfigFormat::Toml)
92	}
93
94	/// Wheter the settings allow the parsing the given configuration format.
95	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}