1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
//! # pngcrush //! //! `pngcrush` is a library for [PNG Crush](https://pngcrush.com)'s HTTP API. extern crate reqwest; use std::error::Error; use std::path::Path; /// Returns raw byte data of a crushed version of the PNG located at the given path /// /// # Arguments /// /// * `path` - a path-like object which points to the PNG to crush /// /// # Examples /// /// ``` /// use std::io::Write as _; /// let data = pngcrush::crush("examples/uncrushed.png").unwrap(); /// let mut buffer = std::fs::File::create("examples/crushed.png").unwrap(); /// buffer.write_all(&data).unwrap(); /// ``` /// /// # Errors /// /// This function will return an Err if: /// * The given path is invalid /// * There is an error reading the path's contents /// * There is an error while sending the HTTP request /// * There is an error copying the response data into the output buffer /// pub fn crush<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, Box<dyn Error>> { let form = reqwest::multipart::Form::new().file("input", path)?; let mut buffer = Vec::new(); reqwest::Client::new() .post("https://pngcrush.com/crush") .multipart(form) .send()? .copy_to(&mut buffer)?; Ok(buffer) }