mas_config/util.rs
1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 2021-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5// Please see LICENSE files in the repository root for full details.
6
7use figment::Figment;
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(
22 &self,
23 _figment: &Figment,
24 ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
25 Ok(())
26 }
27
28 /// Extract configuration from a Figment instance.
29 ///
30 /// # Errors
31 ///
32 /// Returns an error if the configuration could not be loaded
33 fn extract(
34 figment: &Figment,
35 ) -> Result<Self, Box<dyn std::error::Error + Send + Sync + 'static>> {
36 let this: Self = if let Some(path) = Self::PATH {
37 figment.extract_inner(path)?
38 } else {
39 figment.extract()?
40 };
41
42 this.validate(figment)?;
43 Ok(this)
44 }
45}
46
47/// Extension trait for [`ConfigurationSection`] to allow extracting the
48/// configuration section from a [`Figment`] or return the default value if the
49/// section is not present.
50pub trait ConfigurationSectionExt: ConfigurationSection + Default {
51 /// Extract the configuration section from the given [`Figment`], or return
52 /// the default value if the section is not present.
53 ///
54 /// # Errors
55 ///
56 /// Returns an error if the configuration section is invalid.
57 fn extract_or_default(
58 figment: &Figment,
59 ) -> Result<Self, Box<dyn std::error::Error + Send + Sync + 'static>> {
60 let this: Self = if let Some(path) = Self::PATH {
61 // If the configuration section is not present, we return the default value
62 if !figment.contains(path) {
63 return Ok(Self::default());
64 }
65
66 figment.extract_inner(path)?
67 } else {
68 figment.extract()?
69 };
70
71 this.validate(figment)?;
72 Ok(this)
73 }
74}
75
76impl<T: ConfigurationSection + Default> ConfigurationSectionExt for T {}