ncmdump/
utils.rs

1use std::io::Read;
2
3use crate::error::Result;
4
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub enum FileType {
7    /// The standard ncm file.
8    #[cfg(feature = "ncmdump")]
9    Ncm,
10    /// The standard qmc file.
11    #[cfg(feature = "qmcdump")]
12    Qmc,
13    /// The other file type.
14    Other,
15}
16
17impl FileType {
18    /// Return the file type of the reader.
19    ///
20    /// > Notice: This function can't resolve the `Ncmdump` or `QmcDump`
21    ///
22    /// # Example
23    ///
24    /// ```
25    /// # use std::fs::File;
26    /// # use ncmdump::utils::FileType;
27    /// #
28    /// let mut file = File::open("res/test.ncm").unwrap();
29    /// let file_type = FileType::parse(&mut file).unwrap();
30    /// ```
31    pub fn parse<R>(reader: &mut R) -> Result<Self>
32    where
33        R: Read,
34    {
35        let mut head = [0; 8];
36        if reader.read(&mut head)? != 8 {
37            return Ok(FileType::Other);
38        }
39
40        let file_type = match head[..] {
41            #[cfg(feature = "ncmdump")]
42            [0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D] => FileType::Ncm,
43            #[cfg(feature = "qmcdump")]
44            [0xA5, 0x06, 0xB7, 0x89, _, _, _, _] => FileType::Qmc,
45            #[cfg(feature = "qmcdump")]
46            [0x8A, 0x0E, 0xE5, _, _, _, _, _] => FileType::Qmc,
47            _ => FileType::Other,
48        };
49        Ok(file_type)
50    }
51}
52
53/// Return the file type of the reader.
54///
55/// > Notice: This function can't resolve the `Ncmdump` or `QmcDump`
56///
57/// # Example
58///
59/// ```
60/// # use std::fs::File;
61/// # use ncmdump::utils::get_file_type;
62/// #
63/// let mut file = File::open("res/test.ncm").unwrap();
64/// let file_type = get_file_type(&mut file).unwrap();
65/// ```
66#[deprecated]
67pub fn get_file_type<R>(reader: &mut R) -> Result<FileType>
68where
69    R: Read,
70{
71    FileType::parse(reader)
72}
73
74/// Check if the reader is ncm format.
75///
76/// > Notice: The function can't resolve the `Ncmdump` or `QmcDump`
77///
78/// # Example
79///
80/// ```
81/// # use std::fs::File;
82/// # use ncmdump::utils::is_ncm_file;
83/// #
84/// let mut file = File::open("res/test.ncm").unwrap();
85/// let result = is_ncm_file(&mut file).unwrap();
86/// ```
87#[cfg(feature = "ncmdump")]
88pub fn is_ncm_file<R>(reader: &mut R) -> Result<bool>
89where
90    R: Read,
91{
92    let file_type = FileType::parse(reader)?;
93    Ok(file_type == FileType::Ncm)
94}
95
96/// Check if the reader is qmc format.
97///
98/// > Notice: The function can't resolve the `Ncmdump` or `QmcDump`
99///
100/// # Example
101///
102/// ```
103/// # use std::fs::File;
104/// # use ncmdump::utils::is_qmc_file;
105/// #
106/// let mut file = File::open("res/test.ncm").unwrap();
107/// let result = is_qmc_file(&mut file).unwrap();
108/// ```
109#[cfg(feature = "qmcdump")]
110pub fn is_qmc_file<R>(reader: &mut R) -> Result<bool>
111where
112    R: Read,
113{
114    let file_type = FileType::parse(reader)?;
115    Ok(file_type == FileType::Qmc)
116}
117
118#[cfg(test)]
119mod tests {
120    use std::fs::File;
121    use std::io::Error;
122
123    use crate::utils::{is_ncm_file, FileType};
124
125    #[cfg(feature = "ncmdump")]
126    #[test]
127    fn test_is_ncm_file_ok() -> Result<(), Error> {
128        let mut file = File::open("res/test.ncm")?;
129        let result = is_ncm_file(&mut file);
130        assert!(result.is_ok());
131        assert!(result.unwrap());
132        Ok(())
133    }
134
135    #[cfg(feature = "ncmdump")]
136    #[test]
137    fn test_get_file_type_ok() -> Result<(), Error> {
138        let mut file = File::open("res/test.ncm")?;
139        let file_type = FileType::parse(&mut file);
140        assert!(file_type.is_ok());
141        assert_eq!(file_type.unwrap(), FileType::Ncm);
142        Ok(())
143    }
144}