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
#[derive(Debug)]
struct Uncreatable;
impl std::fmt::Display for Uncreatable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "The game was unable to be created")
}
}
impl std::error::Error for Uncreatable {}
pub trait Game: Sized {
fn create(engine: &mut crate::Engine) -> Result<Self, Box<dyn std::error::Error>>;
#[allow(clippy::missing_errors_doc)]
fn update(&mut self, engine: &mut crate::Engine) -> Result<bool, Box<dyn std::error::Error>>;
fn receive_input(&mut self, _engine: &mut crate::Engine, _input: String) {}
}
impl<F: (FnMut(&mut crate::Engine) -> Result<bool, Box<dyn std::error::Error>>) + 'static> Game
for F
{
fn create(_: &mut crate::Engine) -> Result<Self, Box<dyn std::error::Error>> {
Err(Uncreatable.into())
}
fn update(&mut self, engine: &mut crate::Engine) -> Result<bool, Box<dyn std::error::Error>> {
(self)(engine)
}
}