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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! # systemicons
//! 
//! With this lib you can retrive the system icon which is associated 
//! to a certain file extension. The icon will be in the .png format. 
//! Windows and Linux (GTK) are supperted.
//! 
//! When you specify an absolute path to a .exe file, then the icon is loaded from resource, if the exe contains an icon resource.
use std::{fmt, str::Utf8Error};
#[cfg(target_os = "windows")]
use image::ImageError;

/// Inner Error type of possible Error
pub enum InnerError {
    IoError(std::io::Error),
    Utf8Error(Utf8Error),
    GtkInitError,
    #[cfg(target_os = "windows")]
    ImageError(ImageError)
}

/// Possible Error
pub struct Error {
    pub message: String,
    pub inner_error: InnerError 
}

impl From<std::io::Error> for Error {
    fn from(error: std::io::Error) -> Self {
        Error {
            message: error.to_string(),
            inner_error: InnerError::IoError(error)
        }
    }
}

impl From<Utf8Error> for Error {
    fn from(error: Utf8Error) -> Self {
        Error {
            message: error.to_string(),
            inner_error: InnerError::Utf8Error(error)
        }
    }
}

#[cfg(target_os = "windows")]
impl From<ImageError> for Error {
    fn from(error: ImageError) -> Self {
        Error {
            message: error.to_string(),
            inner_error: InnerError::ImageError(error)
        }
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {:?})", self.message, self.inner_error)
    }
}

impl fmt::Debug for InnerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let res = match self {
            &InnerError::GtkInitError => "GtkInitError".to_string(),
            &InnerError::Utf8Error(_) => "Utf8Error".to_string(),
            &InnerError::IoError(_) => "IoError".to_string(),
            #[cfg(target_os = "windows")]
            &InnerError::ImageError(_) => "ImageError".to_string()
        };
        write!(f, "(Error type: {}", res)
    }
}

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "windows")]
mod windows;

/// Retrieving system icon. You have to specify the file extension and desired icon size (like 16, 32 or 64).
/// Returns the icon formatted as png as byte buffer.
#[cfg(target_os = "linux")]
pub fn get_icon(ext: &str, size: i32) -> Result<Vec<u8>, Error> {
    linux::request::get_icon(ext, size)
}
#[cfg(target_os = "windows")]
pub fn get_icon(ext: &str, size: i32) -> Result<Vec<u8>, Error> {
    windows::request::get_icon(ext, size)
}