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
//! Contains all the filetypes that `libmtp` claims to support and can handle.
//! Note that some devices may not support some filetypes.

use libmtp_sys as ffi;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::ToPrimitive;
use std::ffi::CStr;
use std::fmt::{self, Display};

/// Enumeration that holds the supported filetypes, this enum implements `Display`
/// with the description of the file type.
#[derive(Debug, Clone, FromPrimitive, ToPrimitive)]
pub enum Filetype {
    Folder = 0,
    Wav,
    Mp3,
    Wma,
    Ogg,
    Audible,
    Mp4,
    UndefAudio,
    Wmv,
    Avi,
    Mpeg,
    Asf,
    Qt,
    UndefVideo,
    Jpeg,
    Jfif,
    Tiff,
    Bmp,
    Gif,
    Pict,
    Png,
    VCalendar1,
    VCalendar2,
    VCard2,
    VCard3,
    WindowsImageFormat,
    WinExec,
    Text,
    Html,
    Firmware,
    Aac,
    MediaCard,
    Flac,
    Mp2,
    M4a,
    Doc,
    Xml,
    Xls,
    Ppt,
    Mht,
    Jp2,
    Jpx,
    Album,
    Playlist,
    Unknown,
}

impl Display for Filetype {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let ftype = self.to_u32().unwrap();

        unsafe {
            let desc = ffi::LIBMTP_Get_Filetype_Description(ftype);
            let cstr = CStr::from_ptr(desc);

            write!(f, "{}", cstr.to_str().unwrap())
        }
    }
}