1use crate::{
2 common::*,
3 file::{FileDumper, FileLoader, FilePath},
4};
5use prost::Message;
6
7pub type ProtobufPath<T> = FilePath<T, ProtobufDumper, ProtobufLoader>;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct ProtobufDumper {
11 _private: [u8; 0],
12}
13
14impl<T> FileDumper<T> for ProtobufDumper
15where
16 T: Message,
17{
18 type Error = anyhow::Error;
19
20 fn dump<P>(p: P, value: &T) -> Result<(), Self::Error>
21 where
22 P: AsRef<Path>,
23 {
24 let bytes = value.encode_to_vec();
25 fs::write(p, &bytes)?;
26 Ok(())
27 }
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
31pub struct ProtobufLoader {
32 _private: [u8; 0],
33}
34
35impl<T> FileLoader<T> for ProtobufLoader
36where
37 T: Message + Default,
38{
39 type Error = anyhow::Error;
40
41 fn load<P>(p: P) -> Result<T, Self::Error>
42 where
43 P: AsRef<Path>,
44 {
45 let bytes = fs::read(p)?;
46 let value = T::decode(&*bytes)?;
47 Ok(value)
48 }
49}