summaryrefslogtreecommitdiff
path: root/dichroism/src/image_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dichroism/src/image_api.rs')
-rw-r--r--dichroism/src/image_api.rs42
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);
}
}