Rust tonic and prost_types conversion

1.6k views Asked by At

I'm using tonic framework, a rust grpc server implementation. In the generated rust code from the proto file, I have a struct which has a field:

#[prost(message, optional, tag="3")]
pub data: ::core::option::Option<::prost_types::Value>,

generated from a protobuff field:

google.protobuf.Value data = 3; 

I can't seem to find a way to init data which is type prost_types::Value by converting a struct I have. I'm doing something like:

prost_types::Value::try_from(myOwnsStructVar)

But it does not work. Anyone have used prost_types lib before and know how to encode/convert to prost_types::Value

myOwnsStructVar is a type struct. I need to convert it to prost_types::Struct So then I can do:

prost_types::value::Kind::StructValue(myOwnsStructVarAfterConversiontToProstStruct)
1

There are 1 answers

4
Ian S. On

Just from looking at the docs, we can see that Value is a struct with a single public field kind which is of type Option<Kind>. The docs say that if this field is None then that indicates an error. If you look at the docs for Kind, then it's apparent this is where the actual data is stored. So if you want to initialize something of type Value then you'd want to do something like the following, substituting in the appropriate variant of Kind for your use case:

Value {
    kind: Some(Kind::NumberValue(10.0f64))
}

The reason that your try_from solution didn't work is because there are no TryFrom implementations for Value other than the default blanket implementation.