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
use crate::{
err::Error,
file::{as_json, from_json, Build, Load, Save},
};
use serde::{Deserialize, Serialize};
use std::{
fmt::{Display, Formatter},
path::Path,
};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Redirect<T> {
There(String),
Here(T),
}
impl<T: Load> Load for Redirect<T>
where
for<'de> T: Deserialize<'de>,
{
#[inline]
fn load_data(path: &Path) -> Result<Self, Error> {
from_json(path)
}
}
impl<T: Serialize> Save for Redirect<T> {
#[inline]
fn save_data(&self, path: &Path) -> Result<(), Error> {
as_json(self, path)
}
}
impl<T: Load> Build for Redirect<T> {
type Inst = T;
#[inline]
fn build(self, in_dir: &Path) -> Result<Self::Inst, Error> {
match self {
Self::There(path) => {
let path = in_dir.join(path);
T::load(&path)
}
Self::Here(val) => Ok(val),
}
}
}
impl<T: Display> Display for Redirect<T> {
#[inline]
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
match *self {
Self::There(ref path) => write!(fmt, "-> {}", path),
Self::Here(ref item) => write!(fmt, "_! {}", item),
}
}
}