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

cosmos-sdk-proto: replace TypeUrl with Name trait #432

Merged
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
2 changes: 1 addition & 1 deletion cosmos-sdk-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#![warn(trivial_casts, trivial_numeric_casts, unused_import_braces)]

pub mod traits;
mod type_urls;
mod type_names;

pub use prost;
pub use prost_types::Any;
Expand Down
29 changes: 5 additions & 24 deletions cosmos-sdk-proto/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
//! Support traits for Cosmos SDK protobufs.

pub use prost::Message;
pub use prost::{Message, Name};

use crate::Any;
use prost::{DecodeError, EncodeError};
use std::str::FromStr;

/// Associate a type URL with a given proto.
pub trait TypeUrl: Message {
/// Type URL value
const TYPE_URL: &'static str;
}

/// Extension trait for [`Message`].
pub trait MessageExt: Message {
/// Parse this message proto from [`Any`].
fn from_any(any: &Any) -> Result<Self, DecodeError>
where
Self: Default + Sized + TypeUrl,
Self: Default + Name + Sized,
{
if any.type_url == Self::TYPE_URL {
Ok(Self::decode(&*any.value)?)
} else {
let mut err = DecodeError::new(format!(
"expected type URL: \"{}\" (got: \"{}\")",
Self::TYPE_URL,
&any.type_url
));
err.push("unexpected type URL", "type_url");
Err(err)
}
any.to_msg()
}

/// Serialize this message proto as [`Any`].
fn to_any(&self) -> Result<Any, EncodeError>
where
Self: TypeUrl,
Self: Name + Sized,
{
self.to_bytes().map(|bytes| Any {
type_url: Self::TYPE_URL.to_owned(),
value: bytes,
})
Any::from_msg(self)
}

/// Serialize this protobuf message as a byte vector.
Expand Down
Loading
Loading