-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SocketAddress Abstraction for IP Address and Port Handling (#359)
* add SocketAddress * extract common methods into parse_utils.rs
- Loading branch information
1 parent
846a978
commit 2417ddc
Showing
5 changed files
with
339 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use rama_core::error::{ErrorExt, OpaqueError}; | ||
use std::net::{IpAddr, Ipv6Addr}; | ||
|
||
pub(super) fn split_port_from_str(s: &str) -> Result<(&str, u16), OpaqueError> { | ||
if let Some(colon) = s.as_bytes().iter().rposition(|c| *c == b':') { | ||
match s[colon + 1..].parse() { | ||
Ok(port) => Ok((&s[..colon], port)), | ||
Err(err) => Err(err.context("parse port as u16")), | ||
} | ||
} else { | ||
Err(OpaqueError::from_display("missing port")) | ||
} | ||
} | ||
|
||
pub(super) fn try_to_parse_str_to_ip(value: &str) -> Option<IpAddr> { | ||
if value.starts_with('[') || value.ends_with(']') { | ||
let value = value | ||
.strip_prefix('[') | ||
.and_then(|value| value.strip_suffix(']'))?; | ||
Some(IpAddr::V6(value.parse::<Ipv6Addr>().ok()?)) | ||
} else { | ||
value.parse::<IpAddr>().ok() | ||
} | ||
} |
Oops, something went wrong.