use serde::{Deserialize, Serialize};
use crate::ConfigFormat;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Settings {
pub prefer: ConfigFormat,
#[cfg(feature = "json")]
pub allow_json: bool,
#[cfg(feature = "json5")]
pub allow_json5: bool,
#[cfg(feature = "toml")]
pub allow_toml: bool,
}
impl Settings {
pub const fn allow_all(prefer: ConfigFormat) -> Self {
Self {
prefer,
#[cfg(feature = "json")]
allow_json: true,
#[cfg(feature = "json5")]
allow_json5: true,
#[cfg(feature = "toml")]
allow_toml: true,
}
}
#[cfg(feature = "json")]
pub const fn prefer_json() -> Self {
Self::allow_all(ConfigFormat::Json)
}
#[cfg(feature = "json5")]
pub const fn prefer_json5() -> Self {
Self::allow_all(ConfigFormat::Json5)
}
#[cfg(feature = "toml")]
pub const fn prefer_toml() -> Self {
Self::allow_all(ConfigFormat::Toml)
}
pub const fn allow_only(prefer: ConfigFormat) -> Self {
Self {
prefer,
#[cfg(feature = "json")]
allow_json: false,
#[cfg(feature = "json5")]
allow_json5: false,
#[cfg(feature = "toml")]
allow_toml: false,
}
}
#[cfg(feature = "json")]
pub const fn only_json() -> Self {
Self::allow_only(ConfigFormat::Json)
}
#[cfg(feature = "json5")]
pub const fn only_json5() -> Self {
Self::allow_only(ConfigFormat::Json5)
}
#[cfg(feature = "toml")]
pub const fn only_toml() -> Self {
Self::allow_only(ConfigFormat::Toml)
}
pub fn is_allowed(&self, test_for: ConfigFormat) -> bool {
if test_for == self.prefer {
return true;
}
match test_for {
#[cfg(feature = "json")]
ConfigFormat::Json => {
#[cfg(not(feature = "json5"))]
{
self.allow_json
}
#[cfg(feature = "json5")]
{
self.allow_json
|| self.allow_json5
|| matches!(self.prefer, ConfigFormat::Json5)
}
}
#[cfg(feature = "json5")]
ConfigFormat::Json5 => self.allow_json5,
#[cfg(feature = "toml")]
ConfigFormat::Toml => self.allow_toml,
}
}
}