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
102
103
104
105
106
pub mod reader;
pub mod unpack;
pub mod writer;

use std::collections::HashMap;

use uuid::Uuid;

#[derive(Clone, Debug, PartialEq)]
pub struct Package {
    pub assets: HashMap<Uuid, PackageAsset>,
}

impl Package {
    pub fn new() -> Self {
        Package {
            assets: HashMap::new(),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct PackageAsset {
    pub pathname: String,
    pub preview: Option<Vec<u8>>,
    pub meta: Option<Vec<u8>>,
    pub data: Option<Vec<u8>>,
}

impl PackageAsset {
    pub fn new(pathname: String) -> Self {
        PackageAsset {
            pathname,
            preview: None,
            meta: None,
            data: None,
        }
    }
}

#[derive(Debug, Default)]
pub struct PackageAssetBuilder {
    pub pathname: Option<String>,
    pub preview: Option<Vec<u8>>,
    pub meta: Option<Vec<u8>>,
    pub data: Option<Vec<u8>>,
}

impl PackageAssetBuilder {
    pub fn with_pathname(mut self, pathname: String) -> PackageAssetBuilder {
        self.pathname = Some(pathname);
        self
    }

    pub fn with_preview(mut self, preview: Vec<u8>) -> PackageAssetBuilder {
        self.preview = Some(preview);
        self
    }

    pub fn with_meta(mut self, meta: Vec<u8>) -> PackageAssetBuilder {
        self.meta = Some(meta);
        self
    }

    pub fn with_data(mut self, data: Vec<u8>) -> PackageAssetBuilder {
        self.data = Some(data);
        self
    }

    pub fn build(self) -> PackageAsset {
        PackageAsset {
            pathname: self.pathname.expect("Asset has a required path"),
            preview: self.preview,
            meta: self.meta,
            data: self.data,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn create_package() {
        let package_from_internal = Package {
            assets: HashMap::new(),
        };
        let package = Package::new();

        assert_eq!(package_from_internal, package);
    }

    #[test]
    fn create_package_asset() {
        let packageasset_from_internal = PackageAsset {
            pathname: String::from("Assets/TestMaterial.mat"),
            preview: None,
            meta: None,
            data: None,
        };
        let packageasset = PackageAsset::new(String::from("Assets/TestMaterial.mat"));

        assert_eq!(packageasset_from_internal, packageasset);
    }
}