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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use protobuf::Message;
use protobuf::RepeatedField;
use std::error::Error as StdError;
use crate::protos;
use crate::protos::schema_state;
use crate::protos::{
FromBytes, FromNative, FromProto, IntoBytes, IntoNative, IntoProto, ProtoConversionError,
};
use crate::protocol::schema::state::PropertyValue;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LocationNamespace {
Gs1,
}
impl Default for LocationNamespace {
fn default() -> Self {
LocationNamespace::Gs1
}
}
impl FromProto<protos::location_state::Location_LocationNamespace> for LocationNamespace {
fn from_proto(
namespace: protos::location_state::Location_LocationNamespace,
) -> Result<Self, ProtoConversionError> {
match namespace {
protos::location_state::Location_LocationNamespace::GS1 => Ok(LocationNamespace::Gs1),
protos::location_state::Location_LocationNamespace::UNSET_TYPE => {
Err(ProtoConversionError::InvalidTypeError(
"Cannot convert Location_LocationType with type UNSET_TYPE".to_string(),
))
}
}
}
}
impl FromNative<LocationNamespace> for protos::location_state::Location_LocationNamespace {
fn from_native(namespace: LocationNamespace) -> Result<Self, ProtoConversionError> {
match namespace {
LocationNamespace::Gs1 => Ok(protos::location_state::Location_LocationNamespace::GS1),
}
}
}
impl IntoProto<protos::location_state::Location_LocationNamespace> for LocationNamespace {}
impl IntoNative<LocationNamespace> for protos::location_state::Location_LocationNamespace {}
#[derive(Debug, Clone, PartialEq)]
pub struct Location {
location_id: String,
namespace: LocationNamespace,
owner: String,
properties: Vec<PropertyValue>,
}
impl Location {
pub fn location_id(&self) -> &str {
&self.location_id
}
pub fn namespace(&self) -> &LocationNamespace {
&self.namespace
}
pub fn owner(&self) -> &str {
&self.owner
}
pub fn properties(&self) -> &[PropertyValue] {
&self.properties
}
pub fn into_builder(self) -> LocationBuilder {
LocationBuilder::new()
.with_location_id(self.location_id)
.with_namespace(self.namespace)
.with_owner(self.owner)
.with_properties(self.properties)
}
}
impl FromProto<protos::location_state::Location> for Location {
fn from_proto(
location: protos::location_state::Location,
) -> Result<Self, ProtoConversionError> {
Ok(Location {
location_id: location.get_location_id().to_string(),
namespace: LocationNamespace::from_proto(location.get_namespace())?,
owner: location.get_owner().to_string(),
properties: location
.get_properties()
.to_vec()
.into_iter()
.map(PropertyValue::from_proto)
.collect::<Result<Vec<PropertyValue>, ProtoConversionError>>()?,
})
}
}
impl FromNative<Location> for protos::location_state::Location {
fn from_native(location: Location) -> Result<Self, ProtoConversionError> {
let mut proto = protos::location_state::Location::new();
proto.set_location_id(location.location_id().to_string());
proto.set_namespace(location.namespace().into_proto()?);
proto.set_owner(location.owner().to_string());
proto.set_properties(RepeatedField::from_vec(
location
.properties()
.to_vec()
.into_iter()
.map(PropertyValue::into_proto)
.collect::<Result<Vec<schema_state::PropertyValue>, ProtoConversionError>>()?,
));
Ok(proto)
}
}
impl IntoBytes for Location {
fn into_bytes(self) -> Result<Vec<u8>, ProtoConversionError> {
let proto = self.into_proto()?;
let bytes = proto.write_to_bytes().map_err(|_| {
ProtoConversionError::SerializationError(
"Unable to get bytes from Location".to_string(),
)
})?;
Ok(bytes)
}
}
impl IntoProto<protos::location_state::Location> for Location {}
impl IntoNative<Location> for protos::location_state::Location {}
#[derive(Debug)]
pub enum LocationBuildError {
MissingField(String),
EmptyVec(String),
}
impl StdError for LocationBuildError {
fn description(&self) -> &str {
match *self {
LocationBuildError::MissingField(ref msg) => msg,
LocationBuildError::EmptyVec(ref msg) => msg,
}
}
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match *self {
LocationBuildError::MissingField(_) => None,
LocationBuildError::EmptyVec(_) => None,
}
}
}
impl std::fmt::Display for LocationBuildError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
LocationBuildError::MissingField(ref s) => write!(f, "missing field \"{}\"", s),
LocationBuildError::EmptyVec(ref s) => write!(f, "\"{}\" must not be empty", s),
}
}
}
#[derive(Default, Clone, PartialEq)]
pub struct LocationBuilder {
pub location_id: Option<String>,
pub namespace: Option<LocationNamespace>,
pub owner: Option<String>,
pub properties: Option<Vec<PropertyValue>>,
}
impl LocationBuilder {
pub fn new() -> Self {
LocationBuilder::default()
}
pub fn with_location_id(mut self, location_id: String) -> Self {
self.location_id = Some(location_id);
self
}
pub fn with_namespace(mut self, namespace: LocationNamespace) -> Self {
self.namespace = Some(namespace);
self
}
pub fn with_owner(mut self, owner: String) -> Self {
self.owner = Some(owner);
self
}
pub fn with_properties(mut self, properties: Vec<PropertyValue>) -> Self {
self.properties = Some(properties);
self
}
pub fn build(self) -> Result<Location, LocationBuildError> {
let location_id = self.location_id.ok_or_else(|| {
LocationBuildError::MissingField("'location_id' field is required".to_string())
})?;
let namespace = self.namespace.ok_or_else(|| {
LocationBuildError::MissingField("'namespace' field is required".to_string())
})?;
let owner = self.owner.ok_or_else(|| {
LocationBuildError::MissingField("'owner' field is required".to_string())
})?;
let properties = self.properties.ok_or_else(|| {
LocationBuildError::MissingField("'properties' field is required".to_string())
})?;
Ok(Location {
location_id,
namespace,
owner,
properties,
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LocationList {
locations: Vec<Location>,
}
impl LocationList {
pub fn locations(&self) -> &[Location] {
&self.locations
}
pub fn into_builder(self) -> LocationListBuilder {
LocationListBuilder::new().with_locations(self.locations)
}
}
impl FromProto<protos::location_state::LocationList> for LocationList {
fn from_proto(
location_list: protos::location_state::LocationList,
) -> Result<Self, ProtoConversionError> {
Ok(LocationList {
locations: location_list
.get_entries()
.to_vec()
.into_iter()
.map(Location::from_proto)
.collect::<Result<Vec<Location>, ProtoConversionError>>()?,
})
}
}
impl FromNative<LocationList> for protos::location_state::LocationList {
fn from_native(location_list: LocationList) -> Result<Self, ProtoConversionError> {
let mut location_list_proto = protos::location_state::LocationList::new();
location_list_proto.set_entries(RepeatedField::from_vec(
location_list
.locations()
.to_vec()
.into_iter()
.map(Location::into_proto)
.collect::<Result<Vec<protos::location_state::Location>, ProtoConversionError>>()?,
));
Ok(location_list_proto)
}
}
impl FromBytes<LocationList> for LocationList {
fn from_bytes(bytes: &[u8]) -> Result<LocationList, ProtoConversionError> {
let proto: protos::location_state::LocationList = Message::parse_from_bytes(bytes)
.map_err(|_| {
ProtoConversionError::SerializationError(
"Unable to get LocationList from bytes".to_string(),
)
})?;
proto.into_native()
}
}
impl IntoBytes for LocationList {
fn into_bytes(self) -> Result<Vec<u8>, ProtoConversionError> {
let proto = self.into_proto()?;
let bytes = proto.write_to_bytes().map_err(|_| {
ProtoConversionError::SerializationError(
"Unable to get bytes from LocationList".to_string(),
)
})?;
Ok(bytes)
}
}
impl IntoProto<protos::location_state::LocationList> for LocationList {}
impl IntoNative<LocationList> for protos::location_state::LocationList {}
#[derive(Default, Clone)]
pub struct LocationListBuilder {
pub locations: Option<Vec<Location>>,
}
impl LocationListBuilder {
pub fn new() -> Self {
LocationListBuilder::default()
}
pub fn with_locations(mut self, locations: Vec<Location>) -> LocationListBuilder {
self.locations = Some(locations);
self
}
pub fn build(self) -> Result<LocationList, LocationBuildError> {
let locations = self
.locations
.ok_or_else(|| LocationBuildError::MissingField("locations".to_string()))?;
let locations = {
if locations.is_empty() {
return Err(LocationBuildError::EmptyVec("locations".to_string()));
} else {
locations
}
};
Ok(LocationList { locations })
}
}