gopher_mcp_rust/
config.rs1#[derive(Debug, Clone, Default)]
5pub struct Config {
6 provider: String,
7 model: String,
8 api_key: Option<String>,
9 server_config: Option<String>,
10}
11
12impl Config {
13 pub fn provider(&self) -> &str {
15 &self.provider
16 }
17
18 pub fn model(&self) -> &str {
20 &self.model
21 }
22
23 pub fn api_key(&self) -> Option<&str> {
25 self.api_key.as_deref()
26 }
27
28 pub fn server_config(&self) -> Option<&str> {
30 self.server_config.as_deref()
31 }
32
33 pub fn has_api_key(&self) -> bool {
35 self.api_key.as_ref().map_or(false, |k| !k.is_empty())
36 }
37
38 pub fn has_server_config(&self) -> bool {
40 self.server_config.as_ref().map_or(false, |c| !c.is_empty())
41 }
42}
43
44#[derive(Debug, Default)]
46pub struct ConfigBuilder {
47 config: Config,
48}
49
50impl ConfigBuilder {
51 pub fn new() -> Self {
53 Self::default()
54 }
55
56 pub fn with_provider<S: Into<String>>(mut self, provider: S) -> Self {
58 self.config.provider = provider.into();
59 self
60 }
61
62 pub fn with_model<S: Into<String>>(mut self, model: S) -> Self {
64 self.config.model = model.into();
65 self
66 }
67
68 pub fn with_api_key<S: Into<String>>(mut self, api_key: S) -> Self {
70 self.config.api_key = Some(api_key.into());
71 self
72 }
73
74 pub fn with_server_config<S: Into<String>>(mut self, server_config: S) -> Self {
76 self.config.server_config = Some(server_config.into());
77 self
78 }
79
80 pub fn build(self) -> Config {
82 self.config
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_builder_with_api_key() {
92 let config = ConfigBuilder::new()
93 .with_provider("AnthropicProvider")
94 .with_model("claude-3-haiku-20240307")
95 .with_api_key("test-api-key")
96 .build();
97
98 assert_eq!(config.provider(), "AnthropicProvider");
99 assert_eq!(config.model(), "claude-3-haiku-20240307");
100 assert!(config.has_api_key());
101 assert!(!config.has_server_config());
102 assert_eq!(config.api_key(), Some("test-api-key"));
103 }
104
105 #[test]
106 fn test_builder_with_server_config() {
107 let server_config = r#"{"succeeded": true}"#;
108 let config = ConfigBuilder::new()
109 .with_provider("AnthropicProvider")
110 .with_model("claude-3-haiku-20240307")
111 .with_server_config(server_config)
112 .build();
113
114 assert_eq!(config.provider(), "AnthropicProvider");
115 assert_eq!(config.model(), "claude-3-haiku-20240307");
116 assert!(!config.has_api_key());
117 assert!(config.has_server_config());
118 assert_eq!(config.server_config(), Some(server_config));
119 }
120
121 #[test]
122 fn test_empty_config() {
123 let config = ConfigBuilder::new().build();
124
125 assert!(!config.has_api_key());
126 assert!(!config.has_server_config());
127 assert!(config.provider().is_empty());
128 assert!(config.model().is_empty());
129 }
130}