Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Adds comments in Host struct
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Nov 27, 2019
1 parent 9605937 commit c92537c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/commands/dev/server_config/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@ pub struct Host {

impl Host {
pub fn new(host: &str) -> Result<Self, failure::Error> {
// try to create a url from host
let url = match Url::parse(&host) {
Ok(host) => Ok(host),
// if it doesn't work, it might be because there was no scheme
// default to https
Err(_) => Url::parse(&format!("https://{}", host)),
}?;

// validate scheme
let scheme = url.scheme();
if scheme != "http" && scheme != "https" {
failure::bail!("Your host scheme must be either http or https")
}

// validate host
let host = url.host_str().ok_or(format_err!("Invalid host, accepted formats are example.com, http://example.com, or https://example.com"))?;

// recreate url without any trailing path
let url = Url::parse(&format!("{}://{}", scheme, host))?;
Ok(Host { url })
}
Expand Down

0 comments on commit c92537c

Please sign in to comment.