mas_templates/context/
branding.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 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 std::sync::Arc;
8
9use minijinja::{
10    Value,
11    value::{Enumerator, Object},
12};
13
14/// Site branding information.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct SiteBranding {
17    server_name: Arc<str>,
18    policy_uri: Option<Arc<str>>,
19    tos_uri: Option<Arc<str>>,
20    imprint: Option<Arc<str>>,
21}
22
23impl SiteBranding {
24    /// Create a new site branding based on the given server name.
25    #[must_use]
26    pub fn new(server_name: impl Into<Arc<str>>) -> Self {
27        Self {
28            server_name: server_name.into(),
29            policy_uri: None,
30            tos_uri: None,
31            imprint: None,
32        }
33    }
34
35    /// Set the policy URI.
36    #[must_use]
37    pub fn with_policy_uri(mut self, policy_uri: impl Into<Arc<str>>) -> Self {
38        self.policy_uri = Some(policy_uri.into());
39        self
40    }
41
42    /// Set the terms of service URI.
43    #[must_use]
44    pub fn with_tos_uri(mut self, tos_uri: impl Into<Arc<str>>) -> Self {
45        self.tos_uri = Some(tos_uri.into());
46        self
47    }
48
49    /// Set the imprint.
50    #[must_use]
51    pub fn with_imprint(mut self, imprint: impl Into<Arc<str>>) -> Self {
52        self.imprint = Some(imprint.into());
53        self
54    }
55}
56
57impl Object for SiteBranding {
58    fn get_value(self: &Arc<Self>, name: &Value) -> Option<Value> {
59        match name.as_str()? {
60            "server_name" => Some(self.server_name.clone().into()),
61            "policy_uri" => self.policy_uri.clone().map(Value::from),
62            "tos_uri" => self.tos_uri.clone().map(Value::from),
63            "imprint" => self.imprint.clone().map(Value::from),
64            _ => None,
65        }
66    }
67
68    fn enumerate(self: &Arc<Self>) -> Enumerator {
69        Enumerator::Str(&["server_name", "policy_uri", "tos_uri", "imprint"])
70    }
71}