Skip to content

Commit 37ebd03

Browse files
committed
remove output from options, improve error messages
1 parent c406ff4 commit 37ebd03

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

src/core.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ pub struct Options {
6363
pub no_js: bool,
6464
pub no_metadata: bool,
6565
pub no_video: bool,
66-
pub output: String,
6766
pub silent: bool,
6867
pub timeout: u64,
6968
pub unwrap_noscript: bool,
@@ -102,12 +101,12 @@ const PLAINTEXT_MEDIA_TYPES: &[&str] = &[
102101
];
103102

104103
pub fn create_monolithic_document(
105-
target: String,
104+
source: String,
106105
options: &Options,
107106
mut cache: &mut Cache, // TODO: make it Option-al
108107
) -> Result<Vec<u8>, MonolithError> {
109-
// Check if target was provided
110-
if target.len() == 0 {
108+
// Check if source was provided
109+
if source.len() == 0 {
111110
return Err(MonolithError::new("no target specified"));
112111
}
113112

@@ -123,7 +122,7 @@ pub fn create_monolithic_document(
123122

124123
let mut use_stdin: bool = false;
125124

126-
let target_url = match target.as_str() {
125+
let target_url = match source.as_str() {
127126
"-" => {
128127
// Read from pipe (stdin)
129128
use_stdin = true;
@@ -150,26 +149,24 @@ pub fn create_monolithic_document(
150149
match Url::from_file_path(canonical_path) {
151150
Ok(url) => url,
152151
Err(_) => {
153-
// eprintln!(
154-
// "Could not generate file URL out of given path: {}",
155-
// &target
156-
// );
157-
return Err(MonolithError::new(
158-
"could not generate file URL out of given path",
159-
));
152+
return Err(MonolithError::new(&format!(
153+
"could not generate file URL out of given path \"{}\"",
154+
&target
155+
)));
160156
}
161157
}
162158
}
163159
false => {
164-
// eprintln!("Local target is not a file: {}", &target);
165-
return Err(MonolithError::new("local target is not a file"));
160+
return Err(MonolithError::new(&format!(
161+
"local target \"{}\" is not a file",
162+
&target
163+
)));
166164
}
167165
},
168166
false => {
169167
// It is not a FS path, now we do what browsers do:
170168
// prepend "http://" and hope it points to a website
171-
Url::parse(&format!("http://{hopefully_url}", hopefully_url = &target))
172-
.unwrap()
169+
Url::parse(&format!("http://{}", &target)).unwrap()
173170
}
174171
}
175172
}

src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ enum Output {
1616
}
1717

1818
impl Output {
19-
fn new(file_path: &str) -> Result<Output, IoError> {
20-
if file_path.is_empty() || file_path.eq("-") {
19+
fn new(destination: &str) -> Result<Output, IoError> {
20+
if destination.is_empty() || destination.eq("-") {
2121
Ok(Output::Stdout(io::stdout()))
2222
} else {
23-
Ok(Output::File(fs::File::create(file_path)?))
23+
Ok(Output::File(fs::File::create(destination)?))
2424
}
2525
}
2626

@@ -66,7 +66,8 @@ fn main() {
6666
// Process CLI flags and options
6767
let mut cookie_file_path: Option<String> = None;
6868
let mut options: Options = Options::default();
69-
let target;
69+
let source;
70+
let destination;
7071
{
7172
let app = App::new(env!("CARGO_PKG_NAME"))
7273
.version(env!("CARGO_PKG_VERSION"))
@@ -117,7 +118,7 @@ fn main() {
117118
.get_matches();
118119

119120
// Process the command
120-
target = app
121+
source = app
121122
.value_of("target")
122123
.expect("please set target")
123124
.to_string();
@@ -145,7 +146,7 @@ fn main() {
145146
options.no_js = app.is_present("no-js");
146147
options.insecure = app.is_present("insecure");
147148
options.no_metadata = app.is_present("no-metadata");
148-
options.output = app.value_of("output").unwrap_or("").to_string();
149+
destination = app.value_of("output").unwrap_or("").to_string();
149150
options.silent = app.is_present("silent");
150151
options.timeout = app
151152
.value_of("timeout")
@@ -209,10 +210,10 @@ fn main() {
209210
}
210211
}
211212

212-
match create_monolithic_document(target, &options, &mut cache) {
213+
match create_monolithic_document(source, &options, &mut cache) {
213214
Ok(result) => {
214215
// Define output
215-
let mut output = Output::new(&options.output).expect("Could not prepare output");
216+
let mut output = Output::new(&destination).expect("Could not prepare output");
216217

217218
// Write result into STDOUT or file
218219
output.write(&result).expect("Could not write output");

tests/core/options.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ mod passing {
2424
assert_eq!(options.no_js, false);
2525
assert_eq!(options.insecure, false);
2626
assert_eq!(options.no_metadata, false);
27-
assert_eq!(options.output, "".to_string());
2827
assert_eq!(options.silent, false);
2928
assert_eq!(options.timeout, 0);
3029
assert_eq!(options.user_agent, None);

0 commit comments

Comments
 (0)