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
use derivative::Derivative;
use crate::{
error::{Error, ErrorValue, Result},
file::File,
};
pub trait Resource: Send + Sync + 'static {
fn version(&self, path: &str) -> Result<String>;
fn file(&self, path: &str) -> Result<Vec<u8>>;
}
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Ironworks {
#[derivative(Debug = "ignore")]
resources: Vec<Box<dyn Resource>>,
}
impl Default for Ironworks {
fn default() -> Self {
Self::new()
}
}
impl Ironworks {
pub fn new() -> Self {
Self {
resources: Default::default(),
}
}
pub fn add_resource(&mut self, resource: impl Resource) {
self.resources.push(Box::new(resource));
}
#[must_use]
pub fn with_resource(mut self, resource: impl Resource) -> Self {
self.resources.push(Box::new(resource));
self
}
pub fn version(&self, path: &str) -> Result<String> {
self.find_first(path, |resource| resource.version(path))
}
pub fn file<F: File>(&self, path: &str) -> Result<F> {
let data = self.find_first(path, |resource| resource.file(path))?;
F::read(data)
}
fn find_first<F, O>(&self, path: &str, f: F) -> Result<O>
where
F: Fn(&Box<dyn Resource>) -> Result<O>,
{
self.resources
.iter()
.rev()
.map(f)
.find(|result| !matches!(result, Err(Error::NotFound(ErrorValue::Path(_)))))
.unwrap_or_else(|| Err(Error::NotFound(ErrorValue::Path(path.into()))))
}
}