Skip to main content

shared/settings/
user.rs

1use super::{
2    ExtensionSettings, SettingsDeserializeExt, SettingsDeserializer, SettingsSerializeExt,
3    SettingsSerializer,
4};
5use compact_str::ToCompactString;
6use serde::{Deserialize, Serialize};
7use utoipa::ToSchema;
8
9#[derive(Clone, ToSchema, Serialize, Deserialize)]
10pub struct AppSettingsUser {
11    pub max_server_group_count: u64,
12    pub max_api_key_count: u64,
13    pub max_command_snippet_count: u64,
14    pub max_security_key_count: u64,
15    pub max_ssh_key_count: u64,
16
17    pub allow_changing_language: bool,
18
19    pub route_order: Option<Vec<super::RouteOrderItem>>,
20}
21
22#[async_trait::async_trait]
23impl SettingsSerializeExt for AppSettingsUser {
24    async fn serialize(
25        &self,
26        serializer: SettingsSerializer,
27    ) -> Result<SettingsSerializer, anyhow::Error> {
28        Ok(serializer
29            .write_raw_setting(
30                "max_server_group_count",
31                self.max_server_group_count.to_compact_string(),
32            )
33            .write_raw_setting(
34                "max_api_key_count",
35                self.max_api_key_count.to_compact_string(),
36            )
37            .write_raw_setting(
38                "max_command_snippet_count",
39                self.max_command_snippet_count.to_compact_string(),
40            )
41            .write_raw_setting(
42                "max_security_key_count",
43                self.max_security_key_count.to_compact_string(),
44            )
45            .write_raw_setting(
46                "max_ssh_key_count",
47                self.max_ssh_key_count.to_compact_string(),
48            )
49            .write_raw_setting(
50                "allow_changing_language",
51                self.allow_changing_language.to_compact_string(),
52            )
53            .write_serde_setting("route_order", &self.route_order)?)
54    }
55}
56
57pub struct AppSettingsUserDeserializer;
58
59#[async_trait::async_trait]
60impl SettingsDeserializeExt for AppSettingsUserDeserializer {
61    async fn deserialize_boxed(
62        &self,
63        mut deserializer: SettingsDeserializer<'_>,
64    ) -> Result<ExtensionSettings, anyhow::Error> {
65        Ok(Box::new(AppSettingsUser {
66            max_server_group_count: deserializer
67                .take_raw_setting("max_server_group_count")
68                .and_then(|s| s.parse().ok())
69                .unwrap_or(25),
70            max_api_key_count: deserializer
71                .take_raw_setting("max_api_key_count")
72                .and_then(|s| s.parse().ok())
73                .unwrap_or(50),
74            max_command_snippet_count: deserializer
75                .take_raw_setting("max_command_snippet_count")
76                .and_then(|s| s.parse().ok())
77                .unwrap_or(100),
78            max_security_key_count: deserializer
79                .take_raw_setting("max_security_key_count")
80                .and_then(|s| s.parse().ok())
81                .unwrap_or(50),
82            max_ssh_key_count: deserializer
83                .take_raw_setting("max_ssh_key_count")
84                .and_then(|s| s.parse().ok())
85                .unwrap_or(50),
86            allow_changing_language: deserializer
87                .take_raw_setting("allow_changing_language")
88                .map(|s| s == "true")
89                .unwrap_or(true),
90            route_order: deserializer
91                .read_serde_setting("route_order")
92                .unwrap_or_default(),
93        }))
94    }
95}