mas_config/sections/
matrix.rs1use rand::{
8 Rng,
9 distributions::{Alphanumeric, DistString},
10};
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13use serde_with::serde_as;
14use url::Url;
15
16use super::ConfigurationSection;
17
18fn default_homeserver() -> String {
19 "localhost:8008".to_owned()
20}
21
22fn default_endpoint() -> Url {
23 Url::parse("http://localhost:8008/").unwrap()
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default)]
28#[serde(rename_all = "snake_case")]
29pub enum HomeserverKind {
30 #[default]
32 Synapse,
33
34 SynapseReadOnly,
39}
40
41#[serde_as]
43#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
44pub struct MatrixConfig {
45 #[serde(default)]
47 pub kind: HomeserverKind,
48
49 #[serde(default = "default_homeserver")]
51 pub homeserver: String,
52
53 pub secret: String,
55
56 #[serde(default = "default_endpoint")]
58 pub endpoint: Url,
59}
60
61impl ConfigurationSection for MatrixConfig {
62 const PATH: Option<&'static str> = Some("matrix");
63}
64
65impl MatrixConfig {
66 pub(crate) fn generate<R>(mut rng: R) -> Self
67 where
68 R: Rng + Send,
69 {
70 Self {
71 kind: HomeserverKind::default(),
72 homeserver: default_homeserver(),
73 secret: Alphanumeric.sample_string(&mut rng, 32),
74 endpoint: default_endpoint(),
75 }
76 }
77
78 pub(crate) fn test() -> Self {
79 Self {
80 kind: HomeserverKind::default(),
81 homeserver: default_homeserver(),
82 secret: "test".to_owned(),
83 endpoint: default_endpoint(),
84 }
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use figment::{
91 Figment, Jail,
92 providers::{Format, Yaml},
93 };
94
95 use super::*;
96
97 #[test]
98 fn load_config() {
99 Jail::expect_with(|jail| {
100 jail.create_file(
101 "config.yaml",
102 r"
103 matrix:
104 homeserver: matrix.org
105 secret: test
106 ",
107 )?;
108
109 let config = Figment::new()
110 .merge(Yaml::file("config.yaml"))
111 .extract_inner::<MatrixConfig>("matrix")?;
112
113 assert_eq!(&config.homeserver, "matrix.org");
114 assert_eq!(&config.secret, "test");
115
116 Ok(())
117 });
118 }
119}