use rudof_config::TomlConfig;
use rudof_iri::{IriS, iri_once};
use serde::{Deserialize, Serialize};
iri_once!(default_base, "http://base");
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(default)]
pub struct CommonConfig {
#[serde(rename = "base_iri", skip_serializing_if = "Option::is_none")]
pub(crate) base: Option<IriS>,
#[serde(rename = "auto_base")]
pub(crate) auto_base: bool,
}
impl CommonConfig {
pub fn new() -> Self {
Self {
base: Self::default_base(),
auto_base: Self::default_auto_base(),
}
}
pub fn with_base(mut self, base: Option<IriS>) -> Self {
self.base = base;
self
}
pub fn with_auto_base(mut self, base_no_fail: bool) -> Self {
self.auto_base = base_no_fail;
self
}
}
impl CommonConfig {
pub fn base(&self) -> Option<&IriS> {
if let Some(iri) = &self.base {
return Some(iri);
}
if self.auto_base {
return Some(default_base());
}
None
}
pub fn auto_base(&self) -> bool {
self.auto_base
}
}
#[allow(dead_code)]
#[rustfmt::skip]
impl CommonConfig {
#[inline] fn default_base() -> Option<IriS> { None }
#[inline] fn default_auto_base() -> bool { false }
}
impl Default for CommonConfig {
fn default() -> Self {
Self::new()
}
}
impl TomlConfig for CommonConfig {}
#[cfg(test)]
mod tests {
use super::CommonConfig;
use rudof_config::TomlConfig;
#[test]
fn defaults() {
let c = CommonConfig::default();
assert_eq!(c.auto_base(), CommonConfig::default_auto_base());
assert_eq!(c.base(), CommonConfig::default_base().as_ref());
}
#[test]
fn partial_toml_fills_remaining_defaults() {
let c = CommonConfig::from_toml_str(r#"auto_base = true"#).unwrap();
assert!(c.auto_base());
}
#[test]
fn toml_round_trip() {
let c = CommonConfig::default().with_auto_base(true);
let s = c.to_toml_string().unwrap();
let d = CommonConfig::from_toml_str(&s).unwrap();
assert_eq!(c, d);
}
}