1use crate::system::error::{DoumError, DoumResult};
2use crate::system::{get_config_path, load_config, load_default_config, save_config};
3
4pub fn show_config() -> DoumResult<()> {
5 let config = load_config()?;
6 let config_path = get_config_path()?;
7 let toml_str = toml::to_string_pretty(&config)
8 .map_err(|e| DoumError::Config(format!("Failed to serialize config to TOML: {}", e)))?;
9
10 println!("Config file location: {}\n", config_path.display());
11 println!("{}", toml_str);
12
13 Ok(())
14}
15
16pub fn reset_config() -> DoumResult<()> {
17 let default_config = load_default_config()?;
18 save_config(&default_config)?;
19
20 println!("✅ Configuration reset to default");
21
22 Ok(())
23}
24
25pub fn set_config(key: &str, value: &str) -> DoumResult<()> {
26 let mut config = load_config()?;
27
28 match key {
29 "llm.provider" => {
30 if value != "openai" && value != "anthropic" {
31 return Err(DoumError::Config(format!(
32 "Invalid provider: {}. Available: openai, anthropic",
33 value
34 )));
35 }
36 config.llm.provider = value.to_string();
37 }
38 "llm.model" => {
39 config.llm.model = value.to_string();
40 }
41 "llm.timeout" => {
42 config.llm.timeout = value
43 .parse()
44 .map_err(|_| DoumError::Config(format!("Invalid timeout value: {}", value)))?;
45 }
46 "llm.max_retries" => {
47 config.llm.max_retries = value
48 .parse()
49 .map_err(|_| DoumError::Config(format!("Invalid max_retries value: {}", value)))?;
50 }
51 "llm.use_thinking" => {
52 config.llm.use_thinking = value
53 .parse()
54 .map_err(|_| DoumError::Config(format!("Invalid use_thinking value: {}", value)))?;
55 }
56 "llm.use_web_search" => {
57 config.llm.use_web_search = value.parse().map_err(|_| {
58 DoumError::Config(format!("Invalid use_web_search value: {}", value))
59 })?;
60 }
61 "context.max_lines" => {
62 config.context.max_lines = value
63 .parse()
64 .map_err(|_| DoumError::Config(format!("Invalid max_lines value: {}", value)))?;
65 }
66 "context.max_size_kb" => {
67 config.context.max_size_kb = value
68 .parse()
69 .map_err(|_| DoumError::Config(format!("Invalid max_size_kb value: {}", value)))?;
70 }
71 "logging.enabled" => {
72 config.logging.enabled = value.parse().map_err(|_| {
73 DoumError::Config(format!("Invalid logging.enabled value: {}", value))
74 })?;
75 }
76 "logging.level" => {
77 config.logging.level = value.to_string();
78 }
79 _ => {
80 return Err(DoumError::Config(format!("Unknown config key: {}", key)));
81 }
82 }
83
84 save_config(&config)?;
85 println!("✅ Config {} = {}", key, value);
86
87 Ok(())
88}
89
90pub fn get_config(key: &str) -> DoumResult<()> {
91 let config = load_config()?;
92
93 let value = match key {
94 "llm.provider" => config.llm.provider,
95 "llm.model" => config.llm.model,
96 "llm.timeout" => config.llm.timeout.to_string(),
97 "llm.max_retries" => config.llm.max_retries.to_string(),
98 "llm.use_thinking" => config.llm.use_thinking.to_string(),
99 "llm.use_web_search" => config.llm.use_web_search.to_string(),
100 "context.max_lines" => config.context.max_lines.to_string(),
101 "context.max_size_kb" => config.context.max_size_kb.to_string(),
102 "logging.enabled" => config.logging.enabled.to_string(),
103 "logging.level" => config.logging.level,
104 _ => {
105 return Err(DoumError::Config(format!("Unknown config key: {}", key)));
106 }
107 };
108
109 println!("{}", value);
110
111 Ok(())
112}
113
114pub fn unset_config(key: &str) -> DoumResult<()> {
115 let default_config = load_default_config()?;
116 let mut config = load_config()?;
117
118 match key {
119 "llm.provider" => config.llm.provider = default_config.llm.provider,
120 "llm.model" => config.llm.model = default_config.llm.model,
121 "llm.timeout" => config.llm.timeout = default_config.llm.timeout,
122 "llm.max_retries" => config.llm.max_retries = default_config.llm.max_retries,
123 "llm.use_thinking" => config.llm.use_thinking = default_config.llm.use_thinking,
124 "llm.use_web_search" => config.llm.use_web_search = default_config.llm.use_web_search,
125 "context.max_lines" => config.context.max_lines = default_config.context.max_lines,
126 "context.max_size_kb" => config.context.max_size_kb = default_config.context.max_size_kb,
127 "logging.enabled" => config.logging.enabled = default_config.logging.enabled,
128 "logging.level" => config.logging.level = default_config.logging.level,
129 _ => {
130 return Err(DoumError::Config(format!("Unknown config key: {}", key)));
131 }
132 }
133
134 save_config(&config)?;
135 println!("✅ Config {} reset to default", key);
136
137 Ok(())
138}