Skip to content

Commit 732c079

Browse files
committed
Apply review changes
1 parent a7354bd commit 732c079

File tree

3 files changed

+27
-42
lines changed

3 files changed

+27
-42
lines changed

src/transport/grpc/convert.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,21 @@ pub(crate) fn map_property_to_astarte_type(
8585
Ok(Some(individual.try_into()?))
8686
}
8787

88-
/// Map a list of properties that need to be set, unset value will result in an error of the conversion
88+
/// Map a list of properties, unset properties will be ignored
8989
pub(crate) fn map_set_stored_properties(
9090
mut message_hub_properties: astarte_message_hub_proto::StoredProperties,
9191
) -> Result<Vec<StoredProp>, MessageHubProtoError> {
9292
message_hub_properties
9393
.interface_properties
9494
.iter_mut()
9595
.flat_map(|(name, prop_data)| {
96-
prop_data.properties.iter().map(|p| {
96+
prop_data.properties.iter().filter_map(|p| {
9797
let path = p.path.clone();
98-
let value: AstarteType = p.clone().try_into()?;
98+
let value: AstarteType =
99+
match map_property_to_astarte_type(p.clone()).transpose()? {
100+
Ok(s) => s,
101+
Err(e) => return Some(Err(e)),
102+
};
99103

100104
let res = StoredProp {
101105
interface: name.clone(),
@@ -105,7 +109,7 @@ pub(crate) fn map_set_stored_properties(
105109
ownership: prop_data.ownership().into(),
106110
};
107111

108-
Ok(res)
112+
Some(Ok(res))
109113
})
110114
})
111115
.try_collect()
@@ -120,16 +124,6 @@ impl From<astarte_message_hub_proto::Ownership> for Ownership {
120124
}
121125
}
122126

123-
/// Construct an sdk astarte type from a property that is required to be set
124-
impl TryFrom<astarte_message_hub_proto::Property> for AstarteType {
125-
type Error = MessageHubProtoError;
126-
127-
fn try_from(property: astarte_message_hub_proto::Property) -> Result<Self, Self::Error> {
128-
map_property_to_astarte_type(property)
129-
.and_then(|value| value.ok_or(MessageHubProtoError::ExpectedSetProperty))
130-
}
131-
}
132-
133127
impl TryFrom<ProtoIndividualData> for AstarteType {
134128
type Error = MessageHubProtoError;
135129

@@ -472,6 +466,8 @@ mod test {
472466
};
473467
use chrono::{DateTime, Utc};
474468

469+
use crate::transport::grpc::convert::map_property_to_astarte_type;
470+
475471
use super::*;
476472

477473
#[test]
@@ -1356,26 +1352,23 @@ mod test {
13561352
}
13571353

13581354
#[test]
1359-
fn map_property_to_astarte_type() {
1355+
fn map_property_to_astarte_type_ok() {
13601356
let value: AstarteType = AstarteType::String("test".to_owned());
13611357

13621358
let prop = make_messagehub_property("/path11", Some(value.clone()));
13631359

1364-
let astarte_type = AstarteType::try_from(prop).unwrap();
1360+
let astarte_type = map_property_to_astarte_type(prop).unwrap().unwrap();
13651361

13661362
assert_eq!(value, astarte_type);
13671363
}
13681364

13691365
#[test]
1370-
fn map_property_to_astarte_type_error() {
1366+
fn map_property_to_astarte_type_none() {
13711367
let prop = make_messagehub_property("/path11", None);
13721368

1373-
let astarte_type_err = AstarteType::try_from(prop);
1369+
let astarte_type_err = map_property_to_astarte_type(prop);
13741370

1375-
assert!(matches!(
1376-
astarte_type_err,
1377-
Err(MessageHubProtoError::ExpectedSetProperty)
1378-
));
1371+
assert!(matches!(astarte_type_err, Ok(None)));
13791372
}
13801373

13811374
#[test]

src/transport/grpc/store.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ impl GrpcStore {
6969
})
7070
.await
7171
.map(tonic::Response::into_inner)
72-
.map_err(GrpcStoreError::from_status)
73-
.and_then(|p| {
74-
convert::map_set_stored_properties(p).map_err(GrpcStoreError::from_conversion)
75-
})
72+
.map_err(GrpcStoreError::from)
73+
.and_then(|p| Ok(convert::map_set_stored_properties(p)?))
7674
}
7775
}
7876

@@ -87,16 +85,12 @@ pub enum GrpcStoreError {
8785
Status(tonic::Status),
8886
/// Error while converting a proto received value to an internal type
8987
#[error("Error while converting a proto received value to an internal type: {0}")]
90-
Conversion(MessageHubProtoError),
88+
Conversion(#[from] MessageHubProtoError),
9189
}
9290

93-
impl GrpcStoreError {
94-
fn from_status(status: tonic::Status) -> Self {
95-
Self::Status(status)
96-
}
97-
98-
fn from_conversion(conversion: MessageHubProtoError) -> Self {
99-
Self::Conversion(conversion)
91+
impl From<tonic::Status> for GrpcStoreError {
92+
fn from(value: tonic::Status) -> Self {
93+
Self::Status(value)
10094
}
10195
}
10296

@@ -133,10 +127,10 @@ impl PropertyStore for GrpcStore {
133127
path: property.path().to_owned(),
134128
})
135129
.await
136-
.map_err(GrpcStoreError::from_status)
130+
.map_err(GrpcStoreError::from)
137131
.map(tonic::Response::into_inner)?;
138132

139-
convert::map_property_to_astarte_type(property).map_err(GrpcStoreError::from_conversion)
133+
Ok(convert::map_property_to_astarte_type(property)?)
140134
}
141135

142136
async fn unset_prop(&self, _property: &PropertyMapping<'_>) -> Result<(), Self::Err> {
@@ -180,10 +174,8 @@ impl PropertyStore for GrpcStore {
180174
})
181175
.await
182176
.map(tonic::Response::into_inner)
183-
.map_err(GrpcStoreError::from_status)
184-
.and_then(|p| {
185-
convert::map_set_stored_properties(p).map_err(GrpcStoreError::from_conversion)
186-
})
177+
.map_err(GrpcStoreError::from)
178+
.and_then(|p| Ok(convert::map_set_stored_properties(p)?))
187179
}
188180

189181
async fn delete_interface(&self, _interface: &PropertyInterface<'_>) -> Result<(), Self::Err> {

src/transport/mqtt/config/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl MqttConfig {
278278
Credential::ParingToken { pairing_token } => {
279279
debug!("pairing token provided, retrieving credentials secret");
280280

281-
let Some(dir) = config.writable_dir.clone() else {
281+
let Some(dir) = &config.writable_dir else {
282282
return Err(MqttError::NoStorePairingToken);
283283
};
284284

@@ -294,7 +294,7 @@ impl MqttConfig {
294294
/// Register the device and stores the credentials secret in the given directory
295295
async fn read_secret_or_register(
296296
&self,
297-
store_dir: PathBuf,
297+
store_dir: &Path,
298298
pairing_token: &str,
299299
) -> Result<String, PairingError> {
300300
let credential_file = store_dir.join(CREDENTIAL_FILE);

0 commit comments

Comments
 (0)