gent_daemon/config/
discovery.rs1use super::*;
3
4#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(deny_unknown_fields)]
6pub struct Discovery {
7 pub enabled: bool,
8}
9
10impl Config {
11 pub fn discovery(&self) -> Result<Discovery> {
12 self.extra
13 .get("discovery")
14 .cloned()
15 .map(|value| value.try_into().context("invalid chat discovery settings"))
16 .transpose()
17 .map(Option::unwrap_or_default)
18 }
19
20 pub fn set_discovery(&mut self, settings: Discovery) -> Result<()> {
21 self.extra
22 .insert("discovery".into(), toml::Value::try_from(settings)?);
23 Ok(())
24 }
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30
31 #[test]
32 fn discovery_is_opt_in_and_projections_preserve_local_changes() {
33 let root = tempfile::tempdir().unwrap();
34 fs::set_permissions(root.path(), fs::Permissions::from_mode(0o700)).unwrap();
35 let original = initialize(root.path(), "discovery").unwrap();
36 assert!(!original.discovery().unwrap().enabled);
37 edit_local(root.path(), |cfg| {
38 cfg.set_discovery(Discovery { enabled: true })
39 })
40 .unwrap();
41 save(root.path(), &original).unwrap();
42 let enabled = load(root.path()).unwrap();
43 assert!(enabled.discovery().unwrap().enabled);
44 edit_local(root.path(), |cfg| cfg.set_discovery(Discovery::default())).unwrap();
45 save(root.path(), &enabled).unwrap();
46 assert!(!load(root.path()).unwrap().discovery().unwrap().enabled);
47 }
48}