1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::parse_from_bytes;
use crate::reflect::MessageDescriptor;
use crate::well_known_types::Any;
use crate::Message;
use crate::ProtobufResult;
impl Any {
fn type_url(type_url_prefix: &str, descriptor: &MessageDescriptor) -> String {
format!("{}/{}", type_url_prefix, descriptor.full_name())
}
fn get_type_name_from_type_url(type_url: &str) -> Option<&str> {
match type_url.rfind('/') {
Some(i) => Some(&type_url[i + 1..]),
None => None,
}
}
pub fn pack<M: Message>(message: &M) -> ProtobufResult<Any> {
Any::pack_dyn(message)
}
pub fn pack_dyn(message: &dyn Message) -> ProtobufResult<Any> {
Any::pack_with_type_url_prefix(message, "type.googleapis.com")
}
fn pack_with_type_url_prefix(
message: &dyn Message,
type_url_prefix: &str,
) -> ProtobufResult<Any> {
Ok(Any {
type_url: Any::type_url(type_url_prefix, message.descriptor()),
value: message.write_to_bytes()?,
..Default::default()
})
}
pub fn is<M: Message>(&self) -> bool {
self.is_dyn(M::descriptor_static())
}
pub fn is_dyn(&self, descriptor: &MessageDescriptor) -> bool {
match Any::get_type_name_from_type_url(&self.type_url) {
Some(type_name) => type_name == descriptor.full_name(),
None => false,
}
}
pub fn unpack<M: Message>(&self) -> ProtobufResult<Option<M>> {
if !self.is::<M>() {
return Ok(None);
}
Ok(Some(parse_from_bytes(&self.value)?))
}
pub fn unpack_dyn(
&self,
descriptor: &MessageDescriptor,
) -> ProtobufResult<Option<Box<dyn Message>>> {
if !self.is_dyn(descriptor) {
return Ok(None);
}
let mut message = descriptor.new_instance();
message.merge_from_bytes(&self.value)?;
message.check_initialized()?;
Ok(Some(message))
}
}