-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Socket Addrs #812
Socket Addrs #812
Conversation
we can turn a runtime `.parse()?` error into a start time config error.
Binding to port `0` let's the OS assign a port. As unlikely as it is to hit a used port, it's still more likely than being given one.
@@ -20,7 +16,7 @@ pub struct Settings { | |||
pub mode: Mode, | |||
/// Listen address. Required. Default is 0.0.0.0:9081 | |||
#[serde(default = "default_listen_addr")] | |||
pub listen: String, | |||
pub listen: SocketAddr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this operation is fallible clap is able to do this automatically when not using the default method? this doesn't need any additional annotations for the serde macro or anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afaik the only requirement is the type implements FromStr
, then Clap and Serde will attempt to convert into the type using that.
#[test]
fn test_config_parse() {
use std::net::SocketAddr;
#[derive(Debug, serde::Deserialize)]
struct TestSettings {
one: SocketAddr,
#[serde(default = "default_two")]
two: SocketAddr,
}
fn default_two() -> SocketAddr {
"1.2.3.4:19001".parse().unwrap()
}
let x: TestSettings = config::Config::builder()
.add_source(File::with_name("./socket_addr_test.toml"))
.build()
.and_then(|config| config.try_deserialize())
.expect("valid settings");
println!("config: {x:?}");
}
SocketAddr
can be parsed directly from a config file. This moves potential errors even earlier in the bootup process.