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
20#[async_trait::async_trait]
21impl SettingsSerializeExt for AppSettingsUser {
22    async fn serialize(
23        &self,
24        serializer: SettingsSerializer,
25    ) -> Result<SettingsSerializer, anyhow::Error> {
26        Ok(serializer
27            .write_raw_setting(
28                "max_server_group_count",
29                self.max_server_group_count.to_compact_string(),
30            )
31            .write_raw_setting(
32                "max_api_key_count",
33                self.max_api_key_count.to_compact_string(),
34            )
35            .write_raw_setting(
36                "max_command_snippet_count",
37                self.max_command_snippet_count.to_compact_string(),
38            )
39            .write_raw_setting(
40                "max_security_key_count",
41                self.max_security_key_count.to_compact_string(),
42            )
43            .write_raw_setting(
44                "max_ssh_key_count",
45                self.max_ssh_key_count.to_compact_string(),
46            )
47            .write_raw_setting(
48                "allow_changing_language",
49                self.allow_changing_language.to_compact_string(),
50            ))
51    }
52}
53
54pub struct AppSettingsUserDeserializer;
55
56#[async_trait::async_trait]
57impl SettingsDeserializeExt for AppSettingsUserDeserializer {
58    async fn deserialize_boxed(
59        &self,
60        mut deserializer: SettingsDeserializer<'_>,
61    ) -> Result<ExtensionSettings, anyhow::Error> {
62        Ok(Box::new(AppSettingsUser {
63            max_server_group_count: deserializer
64                .take_raw_setting("max_server_group_count")
65                .and_then(|s| s.parse().ok())
66                .unwrap_or(25),
67            max_api_key_count: deserializer
68                .take_raw_setting("max_api_key_count")
69                .and_then(|s| s.parse().ok())
70                .unwrap_or(50),
71            max_command_snippet_count: deserializer
72                .take_raw_setting("max_command_snippet_count")
73                .and_then(|s| s.parse().ok())
74                .unwrap_or(100),
75            max_security_key_count: deserializer
76                .take_raw_setting("max_security_key_count")
77                .and_then(|s| s.parse().ok())
78                .unwrap_or(50),
79            max_ssh_key_count: deserializer
80                .take_raw_setting("max_ssh_key_count")
81                .and_then(|s| s.parse().ok())
82                .unwrap_or(50),
83            allow_changing_language: deserializer
84                .take_raw_setting("allow_changing_language")
85                .map(|s| s == "true")
86                .unwrap_or(true),
87        }))
88    }
89}