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
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::path::{Path, PathBuf};

use anyhow::{ensure, Result};

// == Errors ==
#[derive(Debug, thiserror::Error)]
pub enum BuildError {
    #[error("Missing required directory: '{0}'.")]
    MissingFolderError(&'static str),

    #[error("Missing required file: '{0}'.")]
    MissingFileError(&'static str),
}

/// Builds an NDS ROM given a directory with valid structure.
/// A directory is valid if [`is_nds_dir`] returns `Ok`
///
/// [`is_nds_dir`]: struct.Builder.html#method.is_nds_dir
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Builder {
    root: PathBuf,
}

impl Builder {
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
        let root = path.as_ref();

        Ok(Self {
            root: root.to_path_buf(),
        })
    }

    /// Determines whether a given path is a valid NDS ROM.
    /// A valid NDS ROM directory is made when a ROM is extracted
    /// with an [`Extractor`] and includes the following:
    ///
    /// ./data/
    /// ./overlay/
    /// ./header.bin
    /// ./arm9.bin
    /// ./arm7.bin
    ///
    /// Due to race conditions, the validity is not a guarantee that
    /// the directory is valid through the duration of program execution,
    /// so errors can still be thrown for missing files.
    ///
    /// [`Extractor`]: struct.Extractor.html#method.is_nds_dir
    pub fn is_nds_dir<P: AsRef<Path>>(path: P) -> Result<()> {
        let root = path.as_ref();

        //  Check root
        ensure!(root.is_dir(), BuildError::MissingFolderError("root"));

        //  Check system directories
        ensure!(
            root.join("data").is_dir(),
            BuildError::MissingFolderError("data")
        );
        ensure!(
            root.join("overlay").is_dir(),
            BuildError::MissingFolderError("overlay")
        );

        //  Check system files
        ensure!(
            root.join("arm9_overlay.bin").is_file(),
            BuildError::MissingFileError("arm9_overlay.bin")
        );
        ensure!(
            root.join("arm7_overlay.bin").is_file(),
            BuildError::MissingFileError("arm7_overlay.bin")
        );
        ensure!(
            root.join("arm9.bin").is_file(),
            BuildError::MissingFileError("arm9.bin")
        );
        ensure!(
            root.join("arm7.bin").is_file(),
            BuildError::MissingFileError("arm7.bin")
        );
        ensure!(
            root.join("header.bin").is_file(),
            BuildError::MissingFileError("header.bin")
        );

        Ok(())
    }

    /// Builds a ROM and saves it to the path given. This method will
    /// return an error when the directory is missing required files,
    /// or if there is an issue reading files or saving the ROM.
    pub fn build<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let _output = path.as_ref();

        // let header = read(self.root.join("header.bin"))?;

        // let fs = FileSystem::build(self.root)?;

        Ok(())
    }
}