Initial commit
This commit is contained in:
commit
e030513d57
14 files changed
+928
No files matched your search
@@ -0,0 +1,47 @@
|
||||
use std::path::Path;
|
||||
|
||||
pub fn identify_format(path: &str) -> Option<&'static str> {
|
||||
let p = Path::new(path);
|
||||
let ext = p.extension().and_then(|s| s.to_str()).unwrap_or(path).to_lowercase();
|
||||
match ext.as_str() {
|
||||
"jpg" | "jpeg" => Some("jpeg"),
|
||||
"png" => Some("png"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
macro_rules! test_ident {
|
||||
($name:ident, $ext:expr, $expected:expr) => {
|
||||
#[test]
|
||||
fn $name() {
|
||||
assert_eq!(identify_format($ext), $expected);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
test_ident!(test_identify_format_1, "test.jpg", Some("jpeg"));
|
||||
test_ident!(test_identify_format_2, "test.jpeg", Some("jpeg"));
|
||||
test_ident!(test_identify_format_3, "test.png", Some("png"));
|
||||
test_ident!(test_identify_format_4, "png", Some("png"));
|
||||
test_ident!(test_identify_format_5, "jpeg", Some("jpeg"));
|
||||
test_ident!(test_identify_format_6, "test.txt", None);
|
||||
test_ident!(test_identify_format_7, "FILE.JPG", Some("jpeg"));
|
||||
test_ident!(test_identify_format_8, "file.PnG", Some("png"));
|
||||
test_ident!(test_identify_format_9, "no_ext", None);
|
||||
test_ident!(test_identify_format_10, "test.bmp", None);
|
||||
test_ident!(test_identify_format_11, "file.JPEG", Some("jpeg"));
|
||||
test_ident!(test_identify_format_12, "file.PNG", Some("png"));
|
||||
test_ident!(test_identify_format_13, "complex.file.name.jpg", Some("jpeg"));
|
||||
test_ident!(test_identify_format_14, ".hidden.png", Some("png"));
|
||||
test_ident!(test_identify_format_15, "jpg", Some("jpeg")); // "jpg" passed without dot, treats as ext if no dot in path but path is "jpg", ext becomes "jpg"
|
||||
test_ident!(test_identify_format_16, "a.jpg.txt", None);
|
||||
test_ident!(test_identify_format_17, "a.png.bak", None);
|
||||
test_ident!(test_identify_format_18, "a.b.c.JPEG", Some("jpeg"));
|
||||
test_ident!(test_identify_format_19, "test_file_without_extension", None);
|
||||
test_ident!(test_identify_format_20, ".jpg", None);
|
||||
test_ident!(test_identify_format_21, "", None);
|
||||
}
|
||||
Reference in new issue
Block a user