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
use crate::{
common::*,
file::{FileDumper, FileLoader, FilePath},
};
use prost::Message;
pub type ProtobufPath<T> = FilePath<T, ProtobufDumper, ProtobufLoader>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ProtobufDumper {
_private: [u8; 0],
}
impl<T> FileDumper<T> for ProtobufDumper
where
T: Message,
{
type Error = anyhow::Error;
fn dump<P>(p: P, value: &T) -> Result<(), Self::Error>
where
P: AsRef<Path>,
{
let bytes = value.encode_to_vec();
fs::write(p, &bytes)?;
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ProtobufLoader {
_private: [u8; 0],
}
impl<T> FileLoader<T> for ProtobufLoader
where
T: Message + Default,
{
type Error = anyhow::Error;
fn load<P>(p: P) -> Result<T, Self::Error>
where
P: AsRef<Path>,
{
let bytes = fs::read(p)?;
let value = T::decode(&*bytes)?;
Ok(value)
}
}