rewrite small part of the input argument handling
the commit rewrite a small part of the input argument handling, trying to follow besr rust practices. We get rid of a variable and of a mutable reference while keeping the code a bit more coincise.
This commit is contained in:
parent
89ce5029b9
commit
db7ee697b3
1 changed files with 43 additions and 47 deletions
66
src/main.rs
66
src/main.rs
|
@ -65,10 +65,9 @@ pub fn read_stdin() -> Vec<u8> {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let options = Options::from_args();
|
let options = Options::from_args();
|
||||||
let mut target: String = options.target.clone();
|
|
||||||
|
|
||||||
// Check if target was provided
|
// Check if target was provided
|
||||||
if target.len() == 0 {
|
if options.target.len() == 0 {
|
||||||
if !options.silent {
|
if !options.silent {
|
||||||
eprintln!("No target specified");
|
eprintln!("No target specified");
|
||||||
}
|
}
|
||||||
|
@ -83,65 +82,62 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let target_url: Url;
|
|
||||||
let mut use_stdin: bool = false;
|
let mut use_stdin: bool = false;
|
||||||
|
|
||||||
// Determine exact target URL
|
let target_url = match options.target.as_str() {
|
||||||
if target.clone() == "-" {
|
"-" => {
|
||||||
// Read from pipe (stdin)
|
// Read from pipe (stdin)
|
||||||
use_stdin = true;
|
use_stdin = true;
|
||||||
// Set default target URL to an empty data URL; the user can set it via --base-url
|
// Set default target URL to an empty data URL; the user can set it via --base-url
|
||||||
target_url = Url::parse("data:text/html,").unwrap();
|
Url::parse("data:text/html,").unwrap()
|
||||||
} else {
|
}
|
||||||
match Url::parse(&target.clone()) {
|
target => match Url::parse(&target) {
|
||||||
Ok(parsed_url) => {
|
Ok(url) => match url.scheme() {
|
||||||
if parsed_url.scheme() == "data"
|
"data" | "file" | "http" | "https" => url,
|
||||||
|| parsed_url.scheme() == "file"
|
unsupported_scheme => {
|
||||||
|| (parsed_url.scheme() == "http" || parsed_url.scheme() == "https")
|
|
||||||
{
|
|
||||||
target_url = parsed_url;
|
|
||||||
} else {
|
|
||||||
if !options.silent {
|
if !options.silent {
|
||||||
eprintln!("Unsupported target URL type: {}", &parsed_url.scheme());
|
eprintln!("Unsupported target URL type: {}", unsupported_scheme);
|
||||||
}
|
}
|
||||||
process::exit(1);
|
process::exit(1)
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
Err(_err) => {
|
Err(_) => {
|
||||||
// Failed to parse given base URL (perhaps it's a filesystem path?)
|
// Failed to parse given base URL (perhaps it's a filesystem path?)
|
||||||
let path: &Path = Path::new(&target);
|
let path: &Path = Path::new(&target);
|
||||||
|
match path.exists() {
|
||||||
if path.exists() {
|
true => match path.is_file() {
|
||||||
if path.is_file() {
|
true => {
|
||||||
match Url::from_file_path(fs::canonicalize(&path).unwrap()) {
|
let canonical_path = fs::canonicalize(&path).unwrap();
|
||||||
Ok(file_url) => {
|
match Url::from_file_path(canonical_path) {
|
||||||
target_url = file_url;
|
Ok(url) => url,
|
||||||
}
|
Err(_) => {
|
||||||
Err(_err) => {
|
|
||||||
if !options.silent {
|
if !options.silent {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"Could not generate file URL out of given path: {}",
|
"Could not generate file URL out of given path: {}",
|
||||||
"err"
|
&target
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
false => {
|
||||||
if !options.silent {
|
if !options.silent {
|
||||||
eprintln!("Local target is not a file: {}", &options.target);
|
eprintln!("Local target is not a file: {}", &target);
|
||||||
}
|
}
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
},
|
||||||
// Last chance, now we do what browsers do:
|
false => {
|
||||||
|
// It is not a FS path, now we do what browsers do:
|
||||||
// prepend "http://" and hope it points to a website
|
// prepend "http://" and hope it points to a website
|
||||||
target.insert_str(0, "http://");
|
Url::parse(&format!("https://{hopefully_url}", hopefully_url = &target))
|
||||||
target_url = Url::parse(&target).unwrap();
|
.unwrap()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
// Initialize client
|
// Initialize client
|
||||||
let mut cache = HashMap::new();
|
let mut cache = HashMap::new();
|
||||||
|
|
Loading…
Reference in a new issue