Skip to main content

systemprompt_models/validators/
validation_config_provider.rs

1//! Config provider wrapper for validation with all pre-loaded configs.
2
3use crate::{Config, ContentConfigRaw, ServicesConfig};
4use systemprompt_traits::ConfigProvider;
5
6#[derive(Debug, Clone, serde::Deserialize)]
7pub struct WebConfigRaw {
8    #[serde(default)]
9    pub site_name: Option<String>,
10    #[serde(default)]
11    pub base_url: Option<String>,
12    #[serde(default)]
13    pub theme: Option<String>,
14}
15
16#[derive(Debug, Clone, serde::Deserialize)]
17pub struct WebMetadataRaw {
18    #[serde(default)]
19    pub title: Option<String>,
20    #[serde(default)]
21    pub description: Option<String>,
22}
23
24#[derive(Debug)]
25pub struct ValidationConfigProvider {
26    config: Config,
27    services_config: ServicesConfig,
28    content_config: Option<ContentConfigRaw>,
29    web_config: Option<WebConfigRaw>,
30    web_metadata: Option<WebMetadataRaw>,
31}
32
33impl ValidationConfigProvider {
34    pub const fn new(config: Config, services_config: ServicesConfig) -> Self {
35        Self {
36            config,
37            services_config,
38            content_config: None,
39            web_config: None,
40            web_metadata: None,
41        }
42    }
43
44    pub fn with_content_config(mut self, config: ContentConfigRaw) -> Self {
45        self.content_config = Some(config);
46        self
47    }
48
49    pub fn with_web_config(mut self, config: WebConfigRaw) -> Self {
50        self.web_config = Some(config);
51        self
52    }
53
54    pub fn with_web_metadata(mut self, metadata: WebMetadataRaw) -> Self {
55        self.web_metadata = Some(metadata);
56        self
57    }
58
59    pub const fn services_config(&self) -> &ServicesConfig {
60        &self.services_config
61    }
62
63    pub const fn config(&self) -> &Config {
64        &self.config
65    }
66
67    pub const fn content_config(&self) -> Option<&ContentConfigRaw> {
68        self.content_config.as_ref()
69    }
70
71    pub const fn web_config(&self) -> Option<&WebConfigRaw> {
72        self.web_config.as_ref()
73    }
74
75    pub const fn web_metadata(&self) -> Option<&WebMetadataRaw> {
76        self.web_metadata.as_ref()
77    }
78}
79
80impl ConfigProvider for ValidationConfigProvider {
81    fn get(&self, key: &str) -> Option<String> {
82        self.config.get(key)
83    }
84
85    fn database_url(&self) -> &str {
86        self.config.database_url()
87    }
88
89    fn system_path(&self) -> &str {
90        self.config.system_path()
91    }
92
93    fn api_port(&self) -> u16 {
94        self.config.api_port()
95    }
96
97    fn as_any(&self) -> &dyn std::any::Any {
98        self
99    }
100}