Skip to content

Commit

Permalink
Add From instances for Value (#1091)
Browse files Browse the repository at this point in the history
* Add Into instances for Value

* Update conversions.rs

* rustfmt
  • Loading branch information
andrzejressel authored Jul 19, 2024
1 parent a291d06 commit 23d9fd7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
62 changes: 62 additions & 0 deletions prost-types/src/conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::protobuf::Value;
use crate::value;
use crate::String;
use crate::Vec;
use ::prost::alloc::collections::BTreeMap;

impl From<value::Kind> for Value {
fn from(value: value::Kind) -> Self {
Value { kind: Some(value) }
}
}

macro_rules! impl_number_value {
($t: ty) => {
impl From<$t> for Value {
fn from(value: $t) -> Self {
value::Kind::NumberValue(value.into()).into()
}
}
};
}

impl_number_value!(u8);
impl_number_value!(u16);
impl_number_value!(u32);

impl_number_value!(i8);
impl_number_value!(i16);
impl_number_value!(i32);

impl_number_value!(f32);
impl_number_value!(f64);

impl From<bool> for Value {
fn from(value: bool) -> Self {
value::Kind::BoolValue(value).into()
}
}

impl From<String> for Value {
fn from(value: String) -> Self {
value::Kind::StringValue(value).into()
}
}

impl<'a> From<&'a str> for Value {
fn from(value: &str) -> Self {
value::Kind::StringValue(value.into()).into()
}
}

impl From<Vec<Value>> for Value {
fn from(value: Vec<Value>) -> Self {
value::Kind::ListValue(crate::protobuf::ListValue { values: value }).into()
}
}

impl From<BTreeMap<String, Value>> for Value {
fn from(value: BTreeMap<String, Value>) -> Self {
value::Kind::StructValue(crate::protobuf::Struct { fields: value }).into()
}
}
2 changes: 2 additions & 0 deletions prost-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ pub use timestamp::TimestampError;

mod type_url;
pub(crate) use type_url::{type_url_for, TypeUrl};

mod conversions;

0 comments on commit 23d9fd7

Please sign in to comment.