stitchy_core/files/
util.rs

1
2use image::ImageFormat;
3
4const BYTES_KIB: u64 = 1024;
5const BYTES_MIB: u64 = 1024 * 1024;
6
7/// Return a string representing the size of the file, in bytes, KiB, or MiB
8///
9/// Used in [crate::ImageFileSet::into_image_contents] if outputting information about the input
10/// files, and shared with the CLI crate to print output file size in the same format.
11pub fn make_size_string(length_bytes: u64) -> String {
12    match length_bytes {
13        l if l < BYTES_KIB => format!(
14            "{} bytes", l
15        ),
16        l if l < 10 * BYTES_KIB => format!(
17            "{}.{} KiB", l / BYTES_KIB, (10 * (l % BYTES_KIB)) / BYTES_KIB
18        ),
19        l if l < BYTES_MIB => format!(
20            "{} KiB", l / BYTES_KIB
21        ),
22        l if l < 10 * BYTES_MIB => format!(
23            "{}.{} MiB", l / BYTES_MIB, (10 * (l % BYTES_MIB)) / BYTES_MIB
24        ),
25        l => format!("{} MiB", l / BYTES_MIB)
26    }
27}
28
29/// Mappings of known file extensions to their image format
30pub fn extension_formats() -> [(&'static str, ImageFormat); 6] {
31    [
32        (".jpg", ImageFormat::Jpeg),
33        (".jpeg", ImageFormat::Jpeg),
34        (".png", ImageFormat::Png),
35        (".gif", ImageFormat::Gif),
36        (".bmp", ImageFormat::Bmp),
37        (".webp", ImageFormat::WebP)
38    ]
39}