Skip to content

Commit 164b5fd

Browse files
committed
Added Config Manager
1 parent 9932049 commit 164b5fd

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

src/config.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub const ENABLE_COMPRESSION: bool = false;
2323
pub const ENABLE_ENCRYPTION: bool = true;
2424

2525
/// Main network configuration structure that contains all configurable settings
26-
#[derive(Debug, Clone, Deserialize, Serialize)]
26+
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
2727
pub struct NetworkConfig {
2828
/// Server-specific configuration
2929
#[serde(default)]
@@ -42,34 +42,25 @@ pub struct NetworkConfig {
4242
pub logging: LoggingConfig,
4343
}
4444

45-
impl Default for NetworkConfig {
46-
fn default() -> Self {
47-
Self {
48-
server: ServerConfig::default(),
49-
client: ClientConfig::default(),
50-
transport: TransportConfig::default(),
51-
logging: LoggingConfig::default(),
52-
}
53-
}
54-
}
45+
// Default implementation is now derived
5546

5647
impl NetworkConfig {
5748
/// Load configuration from a TOML file
5849
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
5950
let mut file = File::open(path)
60-
.map_err(|e| ProtocolError::ConfigError(format!("Failed to open config file: {}", e)))?;
51+
.map_err(|e| ProtocolError::ConfigError(format!("Failed to open config file: {e}")))?;
6152

6253
let mut contents = String::new();
6354
file.read_to_string(&mut contents)
64-
.map_err(|e| ProtocolError::ConfigError(format!("Failed to read config file: {}", e)))?;
55+
.map_err(|e| ProtocolError::ConfigError(format!("Failed to read config file: {e}")))?;
6556

6657
Self::from_toml(&contents)
6758
}
6859

6960
/// Load configuration from TOML string
7061
pub fn from_toml(content: &str) -> Result<Self> {
7162
toml::from_str::<Self>(content)
72-
.map_err(|e| ProtocolError::ConfigError(format!("Failed to parse TOML: {}", e)))
63+
.map_err(|e| ProtocolError::ConfigError(format!("Failed to parse TOML: {e}")))
7364
}
7465

7566
/// Load configuration from environment variables
@@ -122,10 +113,10 @@ impl NetworkConfig {
122113
/// Save configuration to a file
123114
pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
124115
let content = toml::to_string_pretty(self)
125-
.map_err(|e| ProtocolError::ConfigError(format!("Failed to serialize config: {}", e)))?;
116+
.map_err(|e| ProtocolError::ConfigError(format!("Failed to serialize config: {e}")))?;
126117

127118
std::fs::write(path, content)
128-
.map_err(|e| ProtocolError::ConfigError(format!("Failed to write config file: {}", e)))?;
119+
.map_err(|e| ProtocolError::ConfigError(format!("Failed to write config file: {e}")))?;
129120

130121
Ok(())
131122
}
@@ -328,6 +319,6 @@ mod log_level_serde {
328319
{
329320
let level_str = String::deserialize(deserializer)?;
330321
Level::from_str(&level_str)
331-
.map_err(|_| serde::de::Error::custom(format!("Invalid log level: {}", level_str)))
322+
.map_err(|_| serde::de::Error::custom(format!("Invalid log level: {level_str}")))
332323
}
333324
}

src/service/client.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ impl Client {
2626
/// Connect and perform secure handshake with timeout using default configuration
2727
#[instrument(skip(addr), fields(address = %addr))]
2828
pub async fn connect(addr: &str) -> Result<Self> {
29-
let mut config = ClientConfig::default();
30-
config.address = addr.to_string();
29+
let config = ClientConfig {
30+
address: addr.to_string(),
31+
..Default::default()
32+
};
3133
Self::connect_with_config(config).await
3234
}
3335

src/service/daemon.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ pub async fn start_with_shutdown(
4747
shutdown_rx: oneshot::Receiver<()>
4848
) -> Result<()> {
4949
// Use default configuration with overridden address
50-
let mut config = ServerConfig::default();
51-
config.address = addr.to_string();
50+
let config = ServerConfig {
51+
address: addr.to_string(),
52+
..Default::default()
53+
};
5254
start_with_config_and_shutdown(config, shutdown_rx).await
5355
}
5456

0 commit comments

Comments
 (0)