mas_config/sections/
templates.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2021-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 camino::Utf8PathBuf;
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10
11use super::ConfigurationSection;
12
13#[cfg(not(any(feature = "docker", feature = "dist")))]
14fn default_path() -> Utf8PathBuf {
15    "./templates/".into()
16}
17
18#[cfg(feature = "docker")]
19fn default_path() -> Utf8PathBuf {
20    "/usr/local/share/mas-cli/templates/".into()
21}
22
23#[cfg(feature = "dist")]
24fn default_path() -> Utf8PathBuf {
25    "./share/templates/".into()
26}
27
28fn is_default_path(value: &Utf8PathBuf) -> bool {
29    *value == default_path()
30}
31
32#[cfg(not(any(feature = "docker", feature = "dist")))]
33fn default_assets_path() -> Utf8PathBuf {
34    "./frontend/dist/manifest.json".into()
35}
36
37#[cfg(feature = "docker")]
38fn default_assets_path() -> Utf8PathBuf {
39    "/usr/local/share/mas-cli/manifest.json".into()
40}
41
42#[cfg(feature = "dist")]
43fn default_assets_path() -> Utf8PathBuf {
44    "./share/manifest.json".into()
45}
46
47fn is_default_assets_path(value: &Utf8PathBuf) -> bool {
48    *value == default_assets_path()
49}
50
51#[cfg(not(any(feature = "docker", feature = "dist")))]
52fn default_translations_path() -> Utf8PathBuf {
53    "./translations/".into()
54}
55
56#[cfg(feature = "docker")]
57fn default_translations_path() -> Utf8PathBuf {
58    "/usr/local/share/mas-cli/translations/".into()
59}
60
61#[cfg(feature = "dist")]
62fn default_translations_path() -> Utf8PathBuf {
63    "./share/translations/".into()
64}
65
66fn is_default_translations_path(value: &Utf8PathBuf) -> bool {
67    *value == default_translations_path()
68}
69
70/// Configuration related to templates
71#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone)]
72pub struct TemplatesConfig {
73    /// Path to the folder which holds the templates
74    #[serde(default = "default_path", skip_serializing_if = "is_default_path")]
75    #[schemars(with = "Option<String>")]
76    pub path: Utf8PathBuf,
77
78    /// Path to the assets manifest
79    #[serde(
80        default = "default_assets_path",
81        skip_serializing_if = "is_default_assets_path"
82    )]
83    #[schemars(with = "Option<String>")]
84    pub assets_manifest: Utf8PathBuf,
85
86    /// Path to the translations
87    #[serde(
88        default = "default_translations_path",
89        skip_serializing_if = "is_default_translations_path"
90    )]
91    #[schemars(with = "Option<String>")]
92    pub translations_path: Utf8PathBuf,
93}
94
95impl Default for TemplatesConfig {
96    fn default() -> Self {
97        Self {
98            path: default_path(),
99            assets_manifest: default_assets_path(),
100            translations_path: default_translations_path(),
101        }
102    }
103}
104
105impl TemplatesConfig {
106    /// Returns true if all fields are at their default values
107    pub(crate) fn is_default(&self) -> bool {
108        is_default_path(&self.path)
109            && is_default_assets_path(&self.assets_manifest)
110            && is_default_translations_path(&self.translations_path)
111    }
112}
113
114impl ConfigurationSection for TemplatesConfig {
115    const PATH: Option<&'static str> = Some("templates");
116}