solverforge_service/
config.rs1use std::path::PathBuf;
2use std::time::Duration;
3
4#[derive(Debug, Clone)]
5pub struct ServiceConfig {
6 pub port: u16,
7 pub startup_timeout: Duration,
8 pub java_home: Option<PathBuf>,
9 pub java_opts: Vec<String>,
10 pub submodule_dir: Option<PathBuf>,
11 pub cache_dir: Option<PathBuf>,
12}
13
14impl Default for ServiceConfig {
15 fn default() -> Self {
16 Self {
17 port: 0, startup_timeout: Duration::from_secs(60),
19 java_home: None,
20 java_opts: vec![],
21 submodule_dir: None,
22 cache_dir: None,
23 }
24 }
25}
26
27impl ServiceConfig {
28 pub fn new() -> Self {
29 Self::default()
30 }
31
32 pub fn with_port(mut self, port: u16) -> Self {
33 self.port = port;
34 self
35 }
36
37 pub fn with_startup_timeout(mut self, timeout: Duration) -> Self {
38 self.startup_timeout = timeout;
39 self
40 }
41
42 pub fn with_java_home(mut self, path: impl Into<PathBuf>) -> Self {
43 self.java_home = Some(path.into());
44 self
45 }
46
47 pub fn with_java_opt(mut self, opt: impl Into<String>) -> Self {
48 self.java_opts.push(opt.into());
49 self
50 }
51
52 pub fn with_java_opts(mut self, opts: Vec<String>) -> Self {
53 self.java_opts = opts;
54 self
55 }
56
57 pub fn with_submodule_dir(mut self, path: impl Into<PathBuf>) -> Self {
58 self.submodule_dir = Some(path.into());
59 self
60 }
61
62 pub fn with_cache_dir(mut self, path: impl Into<PathBuf>) -> Self {
63 self.cache_dir = Some(path.into());
64 self
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_default_config() {
74 let config = ServiceConfig::default();
75 assert_eq!(config.port, 0);
76 assert_eq!(config.startup_timeout, Duration::from_secs(60));
77 assert!(config.java_home.is_none());
78 assert!(config.java_opts.is_empty());
79 }
80
81 #[test]
82 fn test_builder_pattern() {
83 let config = ServiceConfig::new()
84 .with_port(8080)
85 .with_startup_timeout(Duration::from_secs(30))
86 .with_java_home("/usr/lib/jvm/java-21")
87 .with_java_opt("-Xmx2g");
88
89 assert_eq!(config.port, 8080);
90 assert_eq!(config.startup_timeout, Duration::from_secs(30));
91 assert_eq!(
92 config.java_home,
93 Some(PathBuf::from("/usr/lib/jvm/java-21"))
94 );
95 assert_eq!(config.java_opts, vec!["-Xmx2g".to_string()]);
96 }
97
98 #[test]
99 fn test_multiple_java_opts() {
100 let config = ServiceConfig::new()
101 .with_java_opt("-Xmx2g")
102 .with_java_opt("-Xms512m");
103
104 assert_eq!(config.java_opts.len(), 2);
105 }
106}