proto_types/
any.rs

1// From (prost-types)[https://github.com/tokio-rs/prost/blob/master/prost-types/src/any.rs]
2use super::*;
3
4impl Any {
5  /// Serialize the given message type `M` as [`Any`].
6  pub fn from_msg<M>(msg: &M) -> Result<Self, EncodeError>
7  where
8    M: Name,
9  {
10    let type_url = M::type_url();
11
12    let mut value = Vec::new();
13
14    Message::encode(msg, &mut value)?;
15
16    Ok(Any { type_url, value })
17  }
18
19  /// Decode the given message type `M` from [`Any`], validating that it has
20  /// the expected type URL.
21  pub fn to_msg<M>(&self) -> Result<M, DecodeError>
22  where
23    M: Default + Name + Sized,
24  {
25    let expected_type_url = M::type_url();
26
27    if let (Some(expected), Some(actual)) = (
28      TypeUrl::new(&expected_type_url),
29      TypeUrl::new(&self.type_url),
30    )
31      && expected == actual {
32        return M::decode(self.value.as_slice());
33      }
34
35    let mut err = DecodeError::new(format!(
36      "expected type URL: \"{}\" (got: \"{}\")",
37      expected_type_url, &self.type_url
38    ));
39
40    err.push("unexpected type URL", "type_url");
41
42    Err(err)
43  }
44}
45
46impl Name for Any {
47  const PACKAGE: &'static str = PACKAGE_PREFIX;
48
49  const NAME: &'static str = "Any";
50
51  fn type_url() -> String {
52    type_url_for::<Self>()
53  }
54}