Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove errors for encode{,_length_delimited}_vec #73

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Remove errors for `encode_vec` and `encode_length_delimited_vec` in
`Protobuf` ([#73](https://github.com/cosmos/ibc-proto-rs/issues/73))
5 changes: 0 additions & 5 deletions src/protobuf/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloc::format;
use alloc::string::String;
use core::fmt::Display;
use core::format_args;
use core::num::TryFromIntError;

use flex_error::{define_error, DisplayOnly};
use prost::{DecodeError, EncodeError};
Expand All @@ -25,10 +24,6 @@ define_error! {
DecodeMessage
[ DisplayOnly<DecodeError> ]
| _ | { "error decoding buffer into message" },

ParseLength
[ DisplayOnly<TryFromIntError> ]
| _ | { "error parsing encoded length" },
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/protobuf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ use alloc::vec::Vec;
use core::fmt::Display;

use bytes::Buf;
use prost::encoding::encoded_len_varint;
use prost::Message;
use subtle_encoding::hex;

Expand Down Expand Up @@ -145,9 +144,8 @@ where
}

/// Encodes into a Protobuf-encoded `Vec<u8>`.
fn encode_vec(&self) -> Result<Vec<u8>, Error> {
let mut wire = Vec::with_capacity(self.encoded_len());
self.encode(&mut wire).map(|_| wire)
fn encode_vec(&self) -> Vec<u8> {
self.clone_into().encode_to_vec()
}

/// Constructor that attempts to decode a Protobuf-encoded instance from a
Expand All @@ -160,11 +158,8 @@ where
}

/// Encode with a length-delimiter to a `Vec<u8>` Protobuf-encoded message.
fn encode_length_delimited_vec(&self) -> Result<Vec<u8>, Error> {
let len = self.encoded_len();
let lenu64 = len.try_into().map_err(Error::parse_length)?;
let mut wire = Vec::with_capacity(len + encoded_len_varint(lenu64));
self.encode_length_delimited(&mut wire).map(|_| wire)
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
Expand All @@ -177,7 +172,7 @@ where
}

fn encode_to_hex_string(&self) -> String {
let buf = self.encode_vec().expect("encoding shouldn't fail");
let buf = self.encode_vec();
let encoded = hex::encode(buf);
String::from_utf8(encoded).expect("hex-encoded string should always be valid UTF-8")
}
Expand Down