mas_config/
util.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2021-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use figment::{Figment, error::Error as FigmentError};
8use serde::de::DeserializeOwned;
9
10/// Trait implemented by all configuration section to help loading specific part
11/// of the config and generate the sample config.
12pub trait ConfigurationSection: Sized + DeserializeOwned {
13    /// Specify where this section should live relative to the root.
14    const PATH: Option<&'static str> = None;
15
16    /// Validate the configuration section
17    ///
18    /// # Errors
19    ///
20    /// Returns an error if the configuration is invalid
21    fn validate(&self, _figment: &Figment) -> Result<(), FigmentError> {
22        Ok(())
23    }
24
25    /// Extract configuration from a Figment instance.
26    ///
27    /// # Errors
28    ///
29    /// Returns an error if the configuration could not be loaded
30    fn extract(figment: &Figment) -> Result<Self, FigmentError> {
31        let this: Self = if let Some(path) = Self::PATH {
32            figment.extract_inner(path)?
33        } else {
34            figment.extract()?
35        };
36
37        this.validate(figment)?;
38        Ok(this)
39    }
40}
41
42/// Extension trait for [`ConfigurationSection`] to allow extracting the
43/// configuration section from a [`Figment`] or return the default value if the
44/// section is not present.
45pub trait ConfigurationSectionExt: ConfigurationSection + Default {
46    /// Extract the configuration section from the given [`Figment`], or return
47    /// the default value if the section is not present.
48    ///
49    /// # Errors
50    ///
51    /// Returns an error if the configuration section is invalid.
52    fn extract_or_default(figment: &Figment) -> Result<Self, figment::Error> {
53        let this: Self = if let Some(path) = Self::PATH {
54            // If the configuration section is not present, we return the default value
55            if !figment.contains(path) {
56                return Ok(Self::default());
57            }
58
59            figment.extract_inner(path)?
60        } else {
61            figment.extract()?
62        };
63
64        this.validate(figment)?;
65        Ok(this)
66    }
67}
68
69impl<T: ConfigurationSection + Default> ConfigurationSectionExt for T {}