-
Notifications
You must be signed in to change notification settings - Fork 31
/
mod.rs
220 lines (195 loc) · 6.65 KB
/
mod.rs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! This module provides an object safe equivalent of the `tendermint_proto::Protobuf` trait,
//! thereby allowing for easy Google Protocol Buffers encoding and decoding of domain types with
//! validation.
//!
//! Domain types implementing the `Protobuf` trait are expected to implement `TryFrom<T>` and
//! `Into<T>` where `T` is the raw type. The equivalent object safe `erased` counterparts have
//! blanket implementations and are derived automatically.
//!
//! ## Examples
//!
//! ```rust
//! use core::convert::TryFrom;
//!
//! use prost::Message;
//! use ibc_proto::protobuf::Protobuf;
//!
//! // This struct would ordinarily be automatically generated by prost.
//! #[derive(Clone, PartialEq, Message)]
//! pub struct MyRawType {
//! #[prost(uint64, tag="1")]
//! pub a: u64,
//! #[prost(string, tag="2")]
//! pub b: String,
//! }
//!
//! #[derive(Clone)]
//! pub struct MyDomainType {
//! a: u64,
//! b: String,
//! }
//!
//! impl MyDomainType {
//! // Trivial constructor with basic validation logic.
//! pub fn new(a: u64, b: String) -> Result<Self, String> {
//! if a < 1 {
//! return Err("a must be greater than 0".to_owned());
//! }
//! Ok(Self { a, b })
//! }
//! }
//!
//! impl TryFrom<MyRawType> for MyDomainType {
//! type Error = String;
//!
//! fn try_from(value: MyRawType) -> Result<Self, Self::Error> {
//! Self::new(value.a, value.b)
//! }
//! }
//!
//! impl From<MyDomainType> for MyRawType {
//! fn from(value: MyDomainType) -> Self {
//! Self { a: value.a, b: value.b }
//! }
//! }
//!
//! impl Protobuf<MyRawType> for MyDomainType {}
//! ```
mod erased;
mod error;
#[allow(unused_imports)]
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt::Display;
use bytes::Buf;
use prost::Message;
use subtle_encoding::hex;
pub use self::error::Error;
/// Object safe equivalent of `tendermint_proto::Protobuf`.
pub trait Protobuf<Raw: Message + Default>
where
Self: erased::TryFrom<Raw> + erased::CloneInto<Raw>,
<Self as erased::TryFrom<Raw>>::Error: Display,
{
/// Encode into a buffer in Protobuf format.
///
/// Uses [`prost::Message::encode`] after converting into its counterpart
/// Protobuf data structure.
///
/// [`prost::Message::encode`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encode
fn encode(&self, buf: &mut Vec<u8>) -> Result<(), Error> {
self.clone_into().encode(buf).map_err(Error::encode_message)
}
/// Encode with a length-delimiter to a buffer in Protobuf format.
///
/// An error will be returned if the buffer does not have sufficient capacity.
///
/// Uses [`prost::Message::encode_length_delimited`] after converting into
/// its counterpart Protobuf data structure.
///
/// [`prost::Message::encode_length_delimited`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encode_length_delimited
fn encode_length_delimited(&self, buf: &mut Vec<u8>) -> Result<(), Error> {
self.clone_into()
.encode_length_delimited(buf)
.map_err(Error::encode_message)
}
/// Constructor that attempts to decode an instance from a buffer.
///
/// The entire buffer will be consumed.
///
/// Similar to [`prost::Message::decode`] but with additional validation
/// prior to constructing the destination type.
///
/// [`prost::Message::decode`]: https://docs.rs/prost/*/prost/trait.Message.html#method.decode
fn decode<B: Buf>(buf: B) -> Result<Self, Error>
where
Self: Sized,
{
let raw = Raw::decode(buf).map_err(Error::decode_message)?;
Self::try_from(raw).map_err(Error::try_from::<Raw, Self, _>)
}
/// Constructor that attempts to decode a length-delimited instance from
/// the buffer.
///
/// The entire buffer will be consumed.
///
/// Similar to [`prost::Message::decode_length_delimited`] but with
/// additional validation prior to constructing the destination type.
///
/// [`prost::Message::decode_length_delimited`]: https://docs.rs/prost/*/prost/trait.Message.html#method.decode_length_delimited
fn decode_length_delimited<B: Buf>(buf: B) -> Result<Self, Error>
where
Self: Sized,
{
let raw = Raw::decode_length_delimited(buf).map_err(Error::decode_message)?;
Self::try_from(raw).map_err(Error::try_from::<Raw, Self, _>)
}
/// Returns the encoded length of the message without a length delimiter.
///
/// Uses [`prost::Message::encoded_len`] after converting to its
/// counterpart Protobuf data structure.
///
/// [`prost::Message::encoded_len`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encoded_len
fn encoded_len(&self) -> usize {
self.clone_into().encoded_len()
}
/// Encodes into a Protobuf-encoded `Vec<u8>`.
fn encode_vec(&self) -> Vec<u8> {
self.clone_into().encode_to_vec()
}
/// Constructor that attempts to decode a Protobuf-encoded instance from a
/// `Vec<u8>` (or equivalent).
fn decode_vec(v: &[u8]) -> Result<Self, Error>
where
Self: Sized,
{
Self::decode(v)
}
/// Encode with a length-delimiter to a `Vec<u8>` Protobuf-encoded message.
fn encode_length_delimited_vec(&self) -> Vec<u8> {
self.clone_into().encode_length_delimited_to_vec()
}
/// Constructor that attempts to decode a Protobuf-encoded instance with a
/// length-delimiter from a `Vec<u8>` or equivalent.
fn decode_length_delimited_vec(v: &[u8]) -> Result<Self, Error>
where
Self: Sized,
{
Self::decode_length_delimited(v)
}
fn encode_to_hex_string(&self) -> String {
let buf = self.encode_vec();
let encoded = hex::encode(buf);
String::from_utf8(encoded).expect("hex-encoded string should always be valid UTF-8")
}
}
#[cfg(test)]
mod test {
use core::convert::{From, TryFrom};
use super::*;
use crate::google::protobuf::Any;
#[test]
fn test_protobuf_object_safety() {
let _test: Option<Box<dyn Protobuf<Any, Error = Error>>> = None;
}
#[test]
fn test_protobuf_blanket_impls() {
trait Foo: Protobuf<Any, Error = Error> {}
#[derive(Clone)]
struct Domain;
impl Foo for Domain {}
impl Protobuf<Any> for Domain {}
impl TryFrom<Any> for Domain {
type Error = Error;
fn try_from(_: Any) -> Result<Self, Self::Error> {
unimplemented!()
}
}
impl From<Domain> for Any {
fn from(_: Domain) -> Self {
unimplemented!()
}
}
}
}