2121//! Contains conversion traits to convert the Astarte types in the protobuf format to the
2222//! Astarte types from the Astarte device SDK.
2323
24+ use std:: collections:: HashMap ;
2425use std:: num:: TryFromIntError ;
2526
2627use astarte_message_hub_proto:: astarte_data:: AstarteData as ProtoData ;
@@ -32,6 +33,7 @@ use astarte_message_hub_proto::{
3233use chrono:: TimeZone ;
3334use itertools:: Itertools ;
3435
36+ use super :: { GrpcError , GrpcPayload } ;
3537use crate :: aggregate:: AstarteObject ;
3638use crate :: interface:: Ownership ;
3739use crate :: store:: StoredProp ;
@@ -42,8 +44,6 @@ use crate::{
4244} ;
4345use crate :: { DeviceEvent , Value } ;
4446
45- use super :: { GrpcError , GrpcPayload } ;
46-
4747/// Error returned by the Message Hub types conversions.
4848#[ non_exhaustive]
4949#[ derive( thiserror:: Error , Debug ) ]
@@ -108,6 +108,38 @@ pub(crate) fn map_set_stored_properties(
108108 . try_collect ( )
109109}
110110
111+ /// Map a list of stored properties to , unset value will result in an error of the conversion
112+ pub fn map_stored_properties_to_proto (
113+ props : Vec < StoredProp > ,
114+ ) -> astarte_message_hub_proto:: StoredProperties {
115+ let interface_properties =
116+ props
117+ . into_iter ( )
118+ . fold ( HashMap :: new ( ) , |mut interface_properties, prop| {
119+ let entry_prop = interface_properties
120+ . entry ( prop. interface )
121+ . or_insert_with ( || astarte_message_hub_proto:: InterfaceProperties {
122+ ownership : astarte_message_hub_proto:: Ownership :: from ( prop. ownership )
123+ as i32 ,
124+ version_major : prop. interface_major ,
125+ properties : vec ! [ ] ,
126+ } ) ;
127+
128+ let property = astarte_message_hub_proto:: Property {
129+ path : prop. path ,
130+ data : Some ( prop. value . into ( ) ) ,
131+ } ;
132+
133+ entry_prop. properties . push ( property) ;
134+
135+ interface_properties
136+ } ) ;
137+
138+ astarte_message_hub_proto:: StoredProperties {
139+ interface_properties,
140+ }
141+ }
142+
111143impl From < astarte_message_hub_proto:: Ownership > for Ownership {
112144 fn from ( value : astarte_message_hub_proto:: Ownership ) -> Self {
113145 match value {
@@ -117,6 +149,25 @@ impl From<astarte_message_hub_proto::Ownership> for Ownership {
117149 }
118150}
119151
152+ impl From < Ownership > for astarte_message_hub_proto:: Ownership {
153+ fn from ( value : Ownership ) -> Self {
154+ match value {
155+ Ownership :: Device => astarte_message_hub_proto:: Ownership :: Device ,
156+ Ownership :: Server => astarte_message_hub_proto:: Ownership :: Server ,
157+ }
158+ }
159+ }
160+
161+ /// Construct an sdk astarte type from a property that is required to be set
162+ impl TryFrom < astarte_message_hub_proto:: Property > for AstarteType {
163+ type Error = MessageHubProtoError ;
164+
165+ fn try_from ( property : astarte_message_hub_proto:: Property ) -> Result < Self , Self :: Error > {
166+ map_property_to_astarte_type ( property)
167+ . and_then ( |e| e. ok_or ( MessageHubProtoError :: ExpectedField ( "value" ) ) )
168+ }
169+ }
170+
120171impl TryFrom < astarte_message_hub_proto:: AstarteData > for AstarteType {
121172 type Error = MessageHubProtoError ;
122173
0 commit comments