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
46
47
48
49
50
51
52
53
//! This crate contains decoding functions for the embedded icons of
//! several video game console ROM formats.
//!
//! The main use-case is to write thumbnailers for file managers.
use camino::Utf8PathBuf;

pub mod error;
pub mod image;
pub mod nds;

/// Extracts the thumbnail for a given `path` as an `image::RgbaImage`.
///
/// # Errors
/// All errors are contained in variants of the [`ThumbnailerError`] enum.
///
/// [`ThumbnailerError`]: error/enum.ThumbnailerError.html
///
pub fn extract_thumbnail(path: &str) -> Result<image::Image, error::ThumbnailerError> {
    let path = Utf8PathBuf::from(path);
    match path.extension() {
        None => Err(error::ThumbnailerError::NoExtension),
        Some(ext) => match ext {
            "nds" => nds::extract_thumbnail(&path),
            _ => Err(error::ThumbnailerError::UnsupportedExtension),
        },
    }
}

#[cfg(feature = "cxx")]
pub fn extract_thumbnail_ffi(path: &str) -> Result<ffi::Image, error::ThumbnailerError> {
    Ok(extract_thumbnail(path)?.into())
}

#[cfg(feature = "cxx")]
#[cxx::bridge]
mod ffi {
    struct Image {
        pixels: Vec<Pixel>,
        width: usize,
        height: usize,
    }

    struct Pixel {
        r: u8,
        g: u8,
        b: u8,
        a: u8,
    }

    extern "Rust" {
        fn extract_thumbnail_ffi(path: &str) -> Result<Image>;
    }
}