Skip to main content

mas_templates/context/
branding.rs

1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 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 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    service_name: Arc<str>,
19    policy_uri: Option<Arc<str>>,
20    tos_uri: Option<Arc<str>>,
21    imprint: Option<Arc<str>>,
22}
23
24impl SiteBranding {
25    /// Create a new site branding based on the given server name and service name.
26    #[must_use]
27    pub fn new(server_name: impl Into<Arc<str>>, service_name: impl Into<Arc<str>>) -> Self {
28        Self {
29            server_name: server_name.into(),
30            service_name: service_name.into(),
31            policy_uri: None,
32            tos_uri: None,
33            imprint: None,
34        }
35    }
36
37    /// Set the policy URI.
38    #[must_use]
39    pub fn with_policy_uri(mut self, policy_uri: impl Into<Arc<str>>) -> Self {
40        self.policy_uri = Some(policy_uri.into());
41        self
42    }
43
44    /// Set the terms of service URI.
45    #[must_use]
46    pub fn with_tos_uri(mut self, tos_uri: impl Into<Arc<str>>) -> Self {
47        self.tos_uri = Some(tos_uri.into());
48        self
49    }
50
51    /// Set the imprint.
52    #[must_use]
53    pub fn with_imprint(mut self, imprint: impl Into<Arc<str>>) -> Self {
54        self.imprint = Some(imprint.into());
55        self
56    }
57}
58
59impl Object for SiteBranding {
60    fn get_value(self: &Arc<Self>, name: &Value) -> Option<Value> {
61        match name.as_str()? {
62            "server_name" => Some(self.server_name.clone().into()),
63            "service_name" => Some(self.service_name.clone().into()),
64            "policy_uri" => Some(Value::from(self.policy_uri.clone())),
65            "tos_uri" => Some(Value::from(self.tos_uri.clone())),
66            "imprint" => Some(Value::from(self.imprint.clone())),
67            _ => None,
68        }
69    }
70
71    fn enumerate(self: &Arc<Self>) -> Enumerator {
72        Enumerator::Str(&[
73            "server_name",
74            "service_name",
75            "policy_uri",
76            "tos_uri",
77            "imprint",
78        ])
79    }
80}