1use super::*;
3
4impl Any {
5 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 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}