Run clippy and rustfmt
This commit is contained in:
parent
471d5179d5
commit
e24fa4ae27
3 changed files with 29 additions and 11 deletions
33
src/lib.rs
33
src/lib.rs
|
|
@ -133,16 +133,24 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
||||||
let output_data = png.output();
|
let output_data = png.output();
|
||||||
if file_original_size <= output_data.len() {
|
if file_original_size <= output_data.len() {
|
||||||
println!("File already optimized");
|
println!("File already optimized");
|
||||||
return Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.pretend {
|
if opts.pretend {
|
||||||
println!("Running in pretend mode, no output");
|
println!("Running in pretend mode, no output");
|
||||||
} else {
|
} else {
|
||||||
if opts.backup {
|
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,
|
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());
|
let mut buffer = BufWriter::new(io::stdout());
|
||||||
match buffer.write_all(&output_data) {
|
match buffer.write_all(&output_data) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(_) => return Err(format!("Unable to write to stdout"))
|
Err(_) => return Err(format!("Unable to write to stdout")),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let out_file = match File::create(&opts.out_file) {
|
let out_file = match File::create(&opts.out_file) {
|
||||||
Ok(x) => x,
|
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);
|
let mut buffer = BufWriter::new(out_file);
|
||||||
match buffer.write_all(&output_data) {
|
match buffer.write_all(&output_data) {
|
||||||
Ok(_) => println!("Output: {}", opts.out_file.display()),
|
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!(" IDAT size = {} bytes ({} bytes decrease)",
|
||||||
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);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -391,7 +391,8 @@ fn parse_opts_into_struct(matches: &ArgMatches, opts: &mut optipng::Options) ->
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
|
|
||||||
} else if !path.is_dir() {
|
} 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);
|
opts.out_dir = Some(path);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -189,7 +189,7 @@ impl PngData {
|
||||||
output.append(&mut ihdr_data);
|
output.append(&mut ihdr_data);
|
||||||
output.write_u32::<BigEndian>(crc).ok();
|
output.write_u32::<BigEndian>(crc).ok();
|
||||||
// Ancillary headers
|
// Ancillary headers
|
||||||
for (key, header) in self.aux_headers.iter() {
|
for (key, header) in &self.aux_headers {
|
||||||
let mut header_data = header.clone();
|
let mut header_data = header.clone();
|
||||||
output.reserve(header_data.len() + 12);
|
output.reserve(header_data.len() + 12);
|
||||||
output.write_u32::<BigEndian>(header_data.len() as u32).ok();
|
output.write_u32::<BigEndian>(header_data.len() as u32).ok();
|
||||||
|
|
@ -205,7 +205,7 @@ impl PngData {
|
||||||
output.reserve(palette_data.len() + 12);
|
output.reserve(palette_data.len() + 12);
|
||||||
output.write_u32::<BigEndian>(palette_data.len() as u32).ok();
|
output.write_u32::<BigEndian>(palette_data.len() as u32).ok();
|
||||||
let mut type_head = "PLTE".as_bytes().to_owned();
|
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 type_head);
|
||||||
output.append(&mut palette_data);
|
output.append(&mut palette_data);
|
||||||
output.write_u32::<BigEndian>(crc).ok();
|
output.write_u32::<BigEndian>(crc).ok();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue