protobuf_mapper/
lib.rs

1mod convert;
2pub mod result;
3
4use crate::result::Error;
5
6pub use self::convert::Json;
7pub use protobuf_mapper_codegen::*;
8
9pub trait ProtoPack<T>
10where
11  Self: Sized,
12{
13  fn pack(self) -> Result<T, Error>;
14}
15
16pub trait ProtoUnpack<T>
17where
18  Self: Sized,
19{
20  fn unpack(value: T) -> Result<Self, Error>;
21}
22
23pub trait ProtoEnumMeta {
24  const NAME: &'static str;
25  fn get_variant_name(&self) -> &'static str;
26}
27
28pub trait ProtoEnum<T>
29where
30  Self: Sized,
31{
32  fn from_i32(v: i32) -> Option<Self>;
33  fn into_proto_enum(self) -> T;
34  fn unpack_i32(v: i32) -> Result<Self, Error> where Self: ProtoEnumMeta{
35    Self::from_i32(v).ok_or_else(|| Error::EnumDiscriminantNotFound {
36      enum_name: Self::NAME,
37      discriminant: v
38    })
39  }
40  fn unpack_enum(v: T) -> Self;
41}
42
43impl<T1, T2> ProtoPack<Option<T1>> for Option<T2>
44where
45  T2: ProtoPack<T1>,
46{
47  fn pack(self) -> Result<Option<T1>, Error> {
48    if let Some(value) = self {
49      Ok(Some(value.pack()?))
50    } else {
51      Ok(None)
52    }
53  }
54}
55
56impl<T1, T2> ProtoUnpack<Option<T1>> for Option<T2>
57where
58  T2: ProtoUnpack<T1>,
59{
60  fn unpack(value: Option<T1>) -> Result<Self, Error> {
61    if let Some(value) = value {
62      Ok(Some(T2::unpack(value)?))
63    } else {
64      Ok(None)
65    }
66  }
67}