Run clippy and rustfmt

This commit is contained in:
Joshua Holmer 2016-01-10 15:13:12 -05:00
parent 471d5179d5
commit e24fa4ae27
3 changed files with 29 additions and 11 deletions

View file

@ -133,16 +133,24 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
let output_data = png.output();
if file_original_size <= output_data.len() {
println!("File already optimized");
return Ok(())
return Ok(());
}
if opts.pretend {
println!("Running in pretend mode, no output");
} else {
if opts.backup {
match fs::copy(in_file, in_file.with_extension(format!("bak.{}", in_file.extension().unwrap().to_str().unwrap()))) {
match fs::copy(in_file,
in_file.with_extension(format!("bak.{}",
in_file.extension()
.unwrap()
.to_str()
.unwrap()))) {
Ok(x) => x,
Err(_) => return Err(format!("Unable to write to backup file at {}", opts.out_file.display()))
Err(_) => {
return Err(format!("Unable to write to backup file at {}",
opts.out_file.display()))
}
};
}
@ -150,22 +158,31 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
let mut buffer = BufWriter::new(io::stdout());
match buffer.write_all(&output_data) {
Ok(_) => (),
Err(_) => return Err(format!("Unable to write to stdout"))
Err(_) => return Err(format!("Unable to write to stdout")),
}
} else {
let out_file = match File::create(&opts.out_file) {
Ok(x) => x,
Err(_) => return Err(format!("Unable to write to file {}", opts.out_file.display()))
Err(_) => {
return Err(format!("Unable to write to file {}", opts.out_file.display()))
}
};
let mut buffer = BufWriter::new(out_file);
match buffer.write_all(&output_data) {
Ok(_) => println!("Output: {}", opts.out_file.display()),
Err(_) => return Err(format!("Unable to write to file {}", opts.out_file.display()))
Err(_) => {
return Err(format!("Unable to write to file {}", opts.out_file.display()))
}
}
}
}
println!(" IDAT size = {} bytes ({} bytes decrease)", png.idat_data.len(), idat_original_size - png.idat_data.len());
println!(" file size = {} bytes ({} bytes = {:.2}% decrease)", output_data.len(), file_original_size - output_data.len(), (file_original_size - output_data.len()) as f64 / file_original_size as f64);
println!(" IDAT size = {} bytes ({} bytes decrease)",
png.idat_data.len(),
idat_original_size - png.idat_data.len());
println!(" file size = {} bytes ({} bytes = {:.2}% decrease)",
output_data.len(),
file_original_size - output_data.len(),
(file_original_size - output_data.len()) as f64 / file_original_size as f64);
Ok(())
}

View file

@ -391,7 +391,8 @@ fn parse_opts_into_struct(matches: &ArgMatches, opts: &mut optipng::Options) ->
if !path.exists() {
} else if !path.is_dir() {
return Err(format!("{} is an existing file (not a directory), cannot create directory", x));
return Err(format!("{} is an existing file (not a directory), cannot create directory",
x));
}
opts.out_dir = Some(path);
}

View file

@ -189,7 +189,7 @@ impl PngData {
output.append(&mut ihdr_data);
output.write_u32::<BigEndian>(crc).ok();
// Ancillary headers
for (key, header) in self.aux_headers.iter() {
for (key, header) in &self.aux_headers {
let mut header_data = header.clone();
output.reserve(header_data.len() + 12);
output.write_u32::<BigEndian>(header_data.len() as u32).ok();
@ -205,7 +205,7 @@ impl PngData {
output.reserve(palette_data.len() + 12);
output.write_u32::<BigEndian>(palette_data.len() as u32).ok();
let mut type_head = "PLTE".as_bytes().to_owned();
let crc =crc32::checksum_ieee(&palette_data);
let crc = crc32::checksum_ieee(&palette_data);
output.append(&mut type_head);
output.append(&mut palette_data);
output.write_u32::<BigEndian>(crc).ok();