diff options
| author | David Lönnhager <david.l@mullvad.net> | 2022-06-21 15:47:14 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2022-07-07 14:35:21 +0200 |
| commit | a6bc1cc1dfd1aa890b9f328400b1f92e3e966cdf (patch) | |
| tree | 460b2f00f4b3ac130ca36325440bd0b12b9c32ea | |
| parent | ccb3b9f66b0409260df811e0a5f39ad36feaf4b3 (diff) | |
| download | mullvadvpn-a6bc1cc1dfd1aa890b9f328400b1f92e3e966cdf.tar.xz mullvadvpn-a6bc1cc1dfd1aa890b9f328400b1f92e3e966cdf.zip | |
Add serialization and deserialization functions for google.protobuf.Any
| -rw-r--r-- | mullvad-management-interface/src/types.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/mullvad-management-interface/src/types.rs b/mullvad-management-interface/src/types.rs index b8ce589b88..ef1d353b11 100644 --- a/mullvad-management-interface/src/types.rs +++ b/mullvad-management-interface/src/types.rs @@ -1559,3 +1559,22 @@ impl From<FromProtobufTypeError> for crate::Status { } } } + +/// Converts any message to `google.protobuf.Any`. +fn to_proto_any<T: prost::Message>(type_name: &str, message: T) -> prost_types::Any { + prost_types::Any { + type_url: format!("type.googleapis.com/{type_name}"), + value: message.encode_to_vec(), + } +} + +/// Tries to convert a message from `google.protobuf.Any` to `T`. +fn try_from_proto_any<T: prost::Message + Default>( + type_name: &str, + any_value: prost_types::Any, +) -> Option<T> { + if any_value.type_url != format!("type.googleapis.com/{type_name}") { + return None; + } + T::decode(any_value.value.as_slice()).ok() +} |
