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