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
use crate::cache_pair::CachePair;

pub enum PackageTrioType {
    H,
    F,
    B,
}

pub struct Package<T: CachePair> {
    directory: std::path::PathBuf,
    name: String,
    is_post_ensmallening: bool,
    packages: (Option<T>, Option<T>, Option<T>),
}

impl<T: CachePair> Package<T> {
    pub fn new(directory: std::path::PathBuf, name: String, is_post_ensmallening: bool) -> Self {
        let mut package = Self {
            directory,
            name,
            is_post_ensmallening,
            packages: (None, None, None),
        };
        package.load_package_pairs();
        package
    }

    fn load_package_pairs(&mut self) {
        for package_type in [PackageTrioType::H, PackageTrioType::F, PackageTrioType::B] {
            let (toc_path, cache_path) = self.get_pair_path(&package_type);
            if !toc_path.exists() && !cache_path.exists() {
                continue;
            }

            let package_pair = T::new(toc_path, cache_path, self.is_post_ensmallening);

            match package_type {
                PackageTrioType::H => self.packages.0 = Some(package_pair),
                PackageTrioType::F => self.packages.1 = Some(package_pair),
                PackageTrioType::B => self.packages.2 = Some(package_pair),
            }
        }
    }

    fn get_pair_path(
        &self,
        package_type: &PackageTrioType,
    ) -> (std::path::PathBuf, std::path::PathBuf) {
        let mut toc_path = self.directory.clone();
        let mut cache_path = self.directory.clone();

        let trio_char = match package_type {
            PackageTrioType::H => "H",
            PackageTrioType::F => "F",
            PackageTrioType::B => "B",
        };

        toc_path.push(format!("{}.{}.toc", trio_char, self.name));
        cache_path.push(format!("{}.{}.cache", trio_char, self.name));

        (toc_path, cache_path)
    }

    pub fn directory(&self) -> &std::path::PathBuf {
        &self.directory
    }

    pub fn name(&self) -> &String {
        &self.name
    }

    pub fn is_post_ensmallening(&self) -> bool {
        self.is_post_ensmallening
    }

    pub fn get(&self, package_type: &PackageTrioType) -> Option<&T> {
        match package_type {
            PackageTrioType::H => self.packages.0.as_ref(),
            PackageTrioType::F => self.packages.1.as_ref(),
            PackageTrioType::B => self.packages.2.as_ref(),
        }
    }
}