|
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2025, Direction interministérielle du numérique - Gouvernement |
| 5 | +// Français |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to |
| 9 | +// deal in the Software without restriction, including without limitation the |
| 10 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 23 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | +// |
| 25 | + |
| 26 | +use schemars::JsonSchema; |
| 27 | +use serde::{Deserialize, Serialize}; |
| 28 | +use serde_with::serde_as; |
| 29 | +use url::Url; |
| 30 | + |
| 31 | +use super::ConfigurationSection; |
| 32 | + |
| 33 | +fn default_identity_server_url() -> Url { |
| 34 | + Url::parse("http://localhost:8090/").unwrap() |
| 35 | +} |
| 36 | + |
| 37 | +/// Tchap specific configuration |
| 38 | +#[serde_as] |
| 39 | +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] |
| 40 | +pub struct TchapAppConfig { |
| 41 | + /// Identity Server Url |
| 42 | + #[serde(default = "default_identity_server_url")] |
| 43 | + pub identity_server_url: Url, |
| 44 | +} |
| 45 | + |
| 46 | +impl ConfigurationSection for TchapAppConfig { |
| 47 | + const PATH: Option<&'static str> = Some("tchap"); |
| 48 | + |
| 49 | + // NOTE: implement this function to perform validation on config |
| 50 | + fn validate( |
| 51 | + &self, |
| 52 | + _figment: &figment::Figment, |
| 53 | + ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { |
| 54 | + Ok(()) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use figment::{ |
| 61 | + Figment, Jail, |
| 62 | + providers::{Format, Yaml}, |
| 63 | + }; |
| 64 | + |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn load_config() { |
| 69 | + Jail::expect_with(|jail| { |
| 70 | + jail.create_file( |
| 71 | + "config.yaml", |
| 72 | + r" |
| 73 | + tchap: |
| 74 | + identity_server_url: http://localhost:8091 |
| 75 | + ", |
| 76 | + )?; |
| 77 | + |
| 78 | + let config = Figment::new() |
| 79 | + .merge(Yaml::file("config.yaml")) |
| 80 | + .extract_inner::<TchapAppConfig>("tchap")?; |
| 81 | + |
| 82 | + assert_eq!( |
| 83 | + &config.identity_server_url.as_str().to_owned(), |
| 84 | + "http://localhost:8091/" |
| 85 | + ); |
| 86 | + |
| 87 | + Ok(()) |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments