Skip to main content

sawtooth/
protos.rs

1/*
2 * Copyright 2018 Bitwise IO, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * -----------------------------------------------------------------------------
16 */
17
18//! Protobuf structs and associated conversion traits.
19
20use std::error::Error as StdError;
21
22#[derive(Debug)]
23pub enum ProtoConversionError {
24    DeserializationError(String),
25    SerializationError(String),
26    InvalidTypeError(String),
27}
28
29impl StdError for ProtoConversionError {
30    fn description(&self) -> &str {
31        match *self {
32            ProtoConversionError::DeserializationError(ref msg) => msg,
33            ProtoConversionError::SerializationError(ref msg) => msg,
34            ProtoConversionError::InvalidTypeError(ref msg) => msg,
35        }
36    }
37
38    fn cause(&self) -> Option<&dyn StdError> {
39        match *self {
40            ProtoConversionError::DeserializationError(_) => None,
41            ProtoConversionError::SerializationError(_) => None,
42            ProtoConversionError::InvalidTypeError(_) => None,
43        }
44    }
45}
46
47impl std::fmt::Display for ProtoConversionError {
48    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
49        match *self {
50            ProtoConversionError::DeserializationError(ref s) => {
51                write!(f, "unable to deserialize during protobuf conversion: {}", s)
52            }
53            ProtoConversionError::SerializationError(ref s) => {
54                write!(f, "unable to serialize during protobuf conversion: {}", s)
55            }
56            ProtoConversionError::InvalidTypeError(ref s) => write!(
57                f,
58                "invalid type encountered during protobuf conversion: {}",
59                s
60            ),
61        }
62    }
63}
64
65impl From<std::string::FromUtf8Error> for ProtoConversionError {
66    fn from(e: std::string::FromUtf8Error) -> Self {
67        ProtoConversionError::SerializationError(format!("{}", e))
68    }
69}
70
71impl From<hex::FromHexError> for ProtoConversionError {
72    fn from(e: hex::FromHexError) -> Self {
73        ProtoConversionError::SerializationError(format!("{}", e))
74    }
75}
76
77pub trait FromProto<P>: Sized {
78    fn from_proto(other: P) -> Result<Self, ProtoConversionError>;
79}
80
81pub trait FromNative<N>: Sized {
82    fn from_native(other: N) -> Result<Self, ProtoConversionError>;
83}
84
85pub trait FromBytes<N>: Sized {
86    fn from_bytes(bytes: &[u8]) -> Result<N, ProtoConversionError>;
87}
88
89pub trait IntoBytes: Sized {
90    fn into_bytes(self) -> Result<Vec<u8>, ProtoConversionError>;
91}
92
93pub trait IntoNative<T>: Sized
94where
95    T: FromProto<Self>,
96{
97    fn into_native(self) -> Result<T, ProtoConversionError> {
98        FromProto::from_proto(self)
99    }
100}
101
102pub trait IntoProto<T>: Sized
103where
104    T: FromNative<Self>,
105{
106    fn into_proto(self) -> Result<T, ProtoConversionError> {
107        FromNative::from_native(self)
108    }
109}
110
111// Includes the autogenerated protobuf messages
112include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));