Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, the Humility CLI accepts IP addresses as a `String`, and then attempts to parse them as an `Ipv6Addr` and scope. This is a bit unfortunate, as when argument parsing is performed by `clap`, the CLI will emit a somewhat nicer error message when parsing fails (indicating which argument the error occurred while parsing, including the invalid value, and suggesting the help text). On the other hand, when we accept the argument as a `String`, `clap` doesn't add that useful context to the error message. This branch adds a `ScopedV6Addr` newtype, consisting of an `Ipv6Addr` and a scope identifier, and adds a `FromStr` implementation for that type. Now, we can use `ScopedV6Addr` as the type of the CLI argument, and `clap` performs the parsing for us, getting us somewhat nicer error messages. Compare the error output from master: ```console $ cargo run -- --ip ::%fakeiface0 probe Finished dev [unoptimized + debuginfo] target(s) in 0.13s Running `target/debug/humility --ip '::%fakeiface0' probe` humility probe failed: Could not find interface for fakeiface0 $ cargo run -- --ip :: probe Finished dev [unoptimized + debuginfo] target(s) in 0.13s Running `target/debug/humility --ip '::' probe` humility probe failed: Missing scope id in IP (e.g. '%en0') $ cargo run -- --ip barf%eth0 probe Finished dev [unoptimized + debuginfo] target(s) in 0.13s Running `target/debug/humility --ip 'barf%eth0' probe` humility probe failed: invalid Zip archive: Invalid zip header ``` with the error output on this branch: ```console $ cargo run -- --ip :: probe Finished dev [unoptimized + debuginfo] target(s) in 0.14s Running `target/debug/humility --ip '::' probe` error: Invalid value "::" for '--ip <IP>': missing scope ID (e.g. "%en0") in IPv6 address For more information try --help $ cargo run -- --ip ::%fakeiface0 probe Finished dev [unoptimized + debuginfo] target(s) in 8.45s Running `target/debug/humility --ip '::%fakeiface0' probe` error: Invalid value "::%fakeiface0" for '--ip <IP>': Could not find interface for fakeiface0 For more information try --help $ cargo run -- --ip barf%eth0 probe Finished dev [unoptimized + debuginfo] target(s) in 12.87s Running `target/debug/humility --ip 'barf%eth0' probe` error: Invalid value "barf%eth0" for '--ip <IP>': "barf" is not a valid IPv6 address For more information try --help ``` Additionally, using the `ScopedIpv6Addr` type allows us to provide a `fmt::Display` implementation that combines the IP address and scope ID when formatting. This lets us get rid of a little bit of repeated logic for formatting an address with the delimiter, which seems kinda nice to me.
- Loading branch information