Skip to content
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

Merged
merged 5 commits into from
May 14, 2024
Merged

Socket Addrs #812

merged 5 commits into from
May 14, 2024

Conversation

michaeldjeffrey
Copy link
Contributor

  • SocketAddr can be parsed directly from a config file. This moves potential errors even earlier in the bootup process.
  • During tests, get an assigned open port from the OS. This removes the small chance you get a conflicting port when running tests locally.

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,
Copy link
Contributor

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?

Copy link
Contributor Author

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:?}");
}

@michaeldjeffrey michaeldjeffrey merged commit d35d865 into main May 14, 2024
1 check passed
@michaeldjeffrey michaeldjeffrey deleted the mj/socketaddrs branch May 14, 2024 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants