diff options
author | Adam T. Carpenter <atc@53hor.net> | 2020-10-03 22:07:39 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2020-10-03 22:07:39 -0400 |
commit | 8da473460c3f82737ff34d02006ceac4c289243c (patch) | |
tree | a16128726bb163fa2877f5990e032fde12ecea4c /dichroism/src/image_api.rs | |
parent | 58c6bd29b2efd14d7ae386124adf0750e6179370 (diff) | |
download | theglassyladies-8da473460c3f82737ff34d02006ceac4c289243c.tar.xz theglassyladies-8da473460c3f82737ff34d02006ceac4c289243c.zip |
broke out functions, wrote more tests, added test data
Diffstat (limited to 'dichroism/src/image_api.rs')
-rw-r--r-- | dichroism/src/image_api.rs | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/dichroism/src/image_api.rs b/dichroism/src/image_api.rs index d1649d5..44effe4 100644 --- a/dichroism/src/image_api.rs +++ b/dichroism/src/image_api.rs @@ -1,3 +1,4 @@ +use crate::error::DichroismError; use crate::result::Result; use base64::decode; use regex::Regex; @@ -7,34 +8,41 @@ lazy_static! { Regex::new("^data:image/(png|jpeg);base64,(?P<data>.+)").expect("Couldn't parse Regex!"); } -pub fn data_uri_to_files(uri: &str) -> Result<()> { - let caps = if let Some(c) = DATA_URI_RE.captures(uri) { - c - } else { - todo!(); - }; - - let data = caps - .name("data") - .expect("Data URI data extraction should never fail.") - .as_str(); - +pub fn generate_images(data: &str) -> Result<()> { let bytes = decode(data)?; let img = image::load_from_memory(&bytes)?; - img.save("test_full.jpg")?; - let thumb = img.thumbnail(200, 200); - thumb.save("test_thumbnail.jpg")?; + //img.save("test_full.jpg")?; + let _thumb = img.thumbnail(200, 200); + //thumb.save("test_thumbnail.jpg")?; Ok(()) } +pub fn extract_data(uri: &str) -> Result<&str> { + let caps = DATA_URI_RE + .captures(uri) + .ok_or(DichroismError::UriDataExtract)?; + + Ok(caps + .name("data") + .expect("Should never fail if regex succeeded") + .as_str()) +} + #[cfg(test)] mod tests { use super::*; const TEST_DATA_URI: &str = include_str!("unit_test_data/img_data_uri.txt"); + const TEST_DATA_BASE64: &str = include_str!("unit_test_data/test_data_base64.txt"); + + #[test] + fn test_generate_images() { + generate_images(TEST_DATA_BASE64.trim()).unwrap(); + } #[test] - fn test_data_uri_to_files() { - data_uri_to_files(TEST_DATA_URI).unwrap(); + fn test_extract_data() { + let base64_data = extract_data(TEST_DATA_URI).unwrap(); + assert_eq!(TEST_DATA_BASE64.trim(), base64_data); } } |