pub use prost::{Message, Name};
use crate::Any;
use prost::{DecodeError, EncodeError};
use std::str::FromStr;
pub trait MessageExt: Message {
#[deprecated(since = "0.20.0", note = "use Any::to_msg instead")]
fn from_any(any: &Any) -> Result<Self, DecodeError>
where
Self: Default + Name + Sized,
{
any.to_msg()
}
#[deprecated(since = "0.20.0", note = "use Any::from_msg instead")]
fn to_any(&self) -> Result<Any, EncodeError>
where
Self: Name + Sized,
{
Any::from_msg(self)
}
fn to_bytes(&self) -> Result<Vec<u8>, EncodeError>;
}
impl<M> MessageExt for M
where
M: prost::Message,
{
fn to_bytes(&self) -> Result<Vec<u8>, EncodeError> {
let mut bytes = Vec::new();
Message::encode(self, &mut bytes)?;
Ok(bytes)
}
}
pub trait ParseOptional: AsRef<str> {
fn parse_optional<T: FromStr>(&self) -> Result<Option<T>, T::Err> {
if self.as_ref().is_empty() {
Ok(None)
} else {
Ok(Some(self.as_ref().parse()?))
}
}
}
impl ParseOptional for str {}
impl ParseOptional for String {}