Skip to main content

config/
section.rs

1use crate::{Configuration, Value};
2use std::{borrow::Borrow, ops::Deref};
3
4/// Defines the behavior for a section of application configuration values.
5pub trait ConfigurationSection:
6    Configuration + AsRef<dyn Configuration> + Borrow<dyn Configuration> + Deref<Target = dyn Configuration>
7{
8    /// Gets the key this section occupies in its parent.
9    fn key(&self) -> &str;
10
11    /// Gets the full path to this section within the [`Configuration`](crate::Configuration).
12    fn path(&self) -> &str;
13
14    /// Gets the section value.
15    fn value(&self) -> Value;
16
17    /// Converts the [`ConfigurationSection`] into a [`Configuration`](crate::Configuration).
18    fn as_config(&self) -> Box<dyn Configuration>;
19}
20
21pub mod ext {
22
23    use super::*;
24
25    /// Defines extension methods for [`ConfigurationSection`].
26    pub trait ConfigurationSectionExtensions {
27        /// Gets a value indicating whether the configuration section exists.
28        ///
29        /// # Remarks
30        ///
31        /// A configuration section is considered nonexistent if it has no
32        /// value and no children
33        fn exists(&self) -> bool;
34    }
35
36    impl ConfigurationSectionExtensions for dyn ConfigurationSection + '_ {
37        fn exists(&self) -> bool {
38            !self.value().is_empty() || !self.children().is_empty()
39        }
40    }
41
42    impl<T: ConfigurationSection> ConfigurationSectionExtensions for T {
43        fn exists(&self) -> bool {
44            !self.value().is_empty() || !self.children().is_empty()
45        }
46    }
47}