Skip to content

Instantly share code, notes, and snippets.

@shakyShane
Created August 6, 2018 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakyShane/ac493cf66fb5877f81336b6604d7390d to your computer and use it in GitHub Desktop.
Save shakyShane/ac493cf66fb5877f81336b6604d7390d to your computer and use it in GitHub Desktop.
fn modify_url(caps: &Captures, host: &str, port: u16) -> Option<String> {
let first_match = caps.iter().nth(0)?;
let match_item = first_match?;
if let Ok(mut url) = Url::parse(match_item.as_str()) {
// The following 2 method calls will mutate
// the url, but return *different* Errors if they fail
//
// Because I don't care about the actual error, only
// if it succeeded or failed, you can convert a Result
// to Option via .ok()
//
// So if setting the host or port fails here, just do
// nothing and the `?` will short-circuit and return a None
// for this function
url.set_host(Some(host)).ok()?;
url.set_port(Some(port)).ok()?;
Some(url.to_string())
} else {
None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment