Skip to content

Commit 13060f4

Browse files
committed
Use perro macros
1 parent 0e0acc2 commit 13060f4

File tree

6 files changed

+85
-100
lines changed

6 files changed

+85
-100
lines changed

crow/src/lib.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use graphql::perro::permanent_failure;
1+
use graphql::perro::{ensure, permanent_failure};
22
use graphql::schema::list_uncompleted_topups::{topup_status_enum, ListUncompletedTopupsTopup};
33
use graphql::schema::{
44
hide_topup, list_uncompleted_topups, register_notification_token, register_topup, HideTopup,
@@ -77,12 +77,13 @@ impl OfferManager {
7777
let access_token = self.auth.query_token()?;
7878
let client = build_client(Some(&access_token))?;
7979
let data = post_blocking::<RegisterTopup>(&client, &self.backend_url, variables)?;
80-
if !matches!(
81-
data.register_topup,
82-
Some(register_topup::RegisterTopupRegisterTopup { .. })
83-
) {
84-
return Err(permanent_failure("Backend rejected topup registration"));
85-
}
80+
ensure!(
81+
matches!(
82+
data.register_topup,
83+
Some(register_topup::RegisterTopupRegisterTopup { .. })
84+
),
85+
permanent_failure("Backend rejected topup registration")
86+
);
8687
Ok(())
8788
}
8889

@@ -100,17 +101,12 @@ impl OfferManager {
100101
let client = build_client(Some(&access_token))?;
101102
let data =
102103
post_blocking::<RegisterNotificationToken>(&client, &self.backend_url, variables)?;
103-
if !matches!(
104+
ensure!(matches!(
104105
data.register_notification_token,
105106
Some(
106107
register_notification_token::RegisterNotificationTokenRegisterNotificationToken { .. }
107108
)
108-
) {
109-
return Err(permanent_failure(
110-
"Backend rejected notification token registration",
111-
));
112-
}
113-
109+
), permanent_failure("Backend rejected notification token registration"));
114110
Ok(())
115111
}
116112

@@ -147,8 +143,7 @@ fn to_topup_info(topup: ListUncompletedTopupsTopup) -> graphql::Result<TopupInfo
147143
let topup_value_minor_units = (topup.amount_user_currency * 100_f64).round() as u64;
148144
let exchange_fee_rate_permyriad = (topup.exchange_fee_rate * 10_000_f64).round() as u16;
149145
let exchange_fee_minor_units = (topup.exchange_fee_user_currency * 100_f64).round() as u64;
150-
let expires_at = topup.expires_at;
151-
let expires_at = match expires_at {
146+
let expires_at = match topup.expires_at {
152147
Some(e) => Some(parse_from_rfc3339(&e)?),
153148
None => None,
154149
};
@@ -160,19 +155,17 @@ fn to_topup_info(topup: ListUncompletedTopupsTopup) -> graphql::Result<TopupInfo
160155
topup_status_enum::REFUNDED => TopupStatus::REFUNDED,
161156
topup_status_enum::SETTLED => TopupStatus::SETTLED,
162157
topup_status_enum::REFUND_HIDDEN => {
163-
return Err(runtime_error(
158+
runtime_error!(
164159
graphql::GraphQlRuntimeErrorCode::CorruptData,
165-
"The backend returned the unexpected status: REFUND_HIDDEN".to_string(),
166-
))
160+
"The backend returned the unexpected status: REFUND_HIDDEN",
161+
);
167162
}
168163
topup_status_enum::Other(_) => {
169-
return Err(runtime_error(
164+
runtime_error!(
170165
graphql::GraphQlRuntimeErrorCode::CorruptData,
171-
format!(
172-
"The backend returned an unknown topup status: {:?}",
173-
topup.status
174-
),
175-
))
166+
"The backend returned an unknown topup status: {:?}",
167+
topup.status
168+
);
176169
}
177170
};
178171

graphql/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ log = "0.4.17"
1010
reqwest = { version = "0.11", default-features = false, features = ["json", "blocking", "rustls-tls"]}
1111
serde = { version = "1.0", features = ["derive"]}
1212

13-
perro = {git = "https://github.com/getlipa/perro", tag = "v1.1.0" }
13+
perro = {git = "https://github.com/getlipa/perro", tag = "v1.2.0" }

graphql/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ pub fn post_blocking<Query: graphql_client::GraphQLQuery>(
6767
Err(e) => {
6868
if is_502_status(e.status()) || e.to_string().contains("502") {
6969
// checking for the error containing 502 because reqwest is unexpectedly returning a decode error instead of status error
70-
return Err(runtime_error(
70+
runtime_error!(
7171
GraphQlRuntimeErrorCode::RemoteServiceUnavailable,
7272
"The remote server returned status 502",
73-
));
73+
);
7474
}
75-
return Err(runtime_error(
75+
runtime_error!(
7676
GraphQlRuntimeErrorCode::NetworkError,
7777
"Failed to execute the query",
78-
));
78+
);
7979
}
8080
};
8181
get_response_data(response, backend_url)
@@ -91,15 +91,15 @@ pub async fn post<Query: graphql_client::GraphQLQuery>(
9191
Err(e) => {
9292
if is_502_status(e.status()) || e.to_string().contains("502") {
9393
// checking for the error containing 502 because reqwest is unexpectedly returning a decode error instead of status error
94-
return Err(runtime_error(
94+
runtime_error!(
9595
GraphQlRuntimeErrorCode::RemoteServiceUnavailable,
9696
"The remote server returned status 502",
97-
));
97+
);
9898
}
99-
return Err(runtime_error(
99+
runtime_error!(
100100
GraphQlRuntimeErrorCode::NetworkError,
101101
"Failed to execute the query",
102-
));
102+
);
103103
}
104104
};
105105
get_response_data(response, backend_url)

honey-badger/src/asynchronous/provider.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::signing::sign;
33

44
use crate::provider::{add_bitcoin_message_prefix, add_hex_prefix};
55
use crate::{AuthLevel, TermsAndConditions};
6-
use graphql::perro::{invalid_input, permanent_failure, runtime_error, OptionToError};
6+
use graphql::perro::{ensure, invalid_input, permanent_failure, runtime_error, OptionToError};
77
use graphql::reqwest::Client;
88
use graphql::schema::*;
99
use graphql::{build_async_client, post};
@@ -68,25 +68,25 @@ impl AuthProvider {
6868
terms: TermsAndConditions,
6969
) -> Result<()> {
7070
info!("Accepting T&C ({:?})...", terms);
71-
if self.auth_level != AuthLevel::Pseudonymous {
72-
return Err(invalid_input(
73-
"Accepting T&C not supported for auth levels other than Pseudonymous",
74-
));
75-
}
71+
ensure!(
72+
self.auth_level == AuthLevel::Pseudonymous,
73+
invalid_input("Accepting T&C not supported for auth levels other than Pseudonymous")
74+
);
7675

7776
let variables = accept_terms_and_conditions::Variables {
7877
service_provider: terms.into(),
7978
};
8079
let client = build_async_client(Some(&access_token))?;
8180
let data = post::<AcceptTermsAndConditions>(&client, &self.backend_url, variables).await?;
82-
if !matches!(
83-
data.accept_terms_conditions,
84-
Some(accept_terms_and_conditions::AcceptTermsAndConditionsAcceptTermsConditions { .. })
85-
) {
86-
return Err(permanent_failure(
87-
"Backend rejected accepting Terms and Conditions",
88-
));
89-
}
81+
ensure!(
82+
matches!(
83+
data.accept_terms_conditions,
84+
Some(
85+
accept_terms_and_conditions::AcceptTermsAndConditionsAcceptTermsConditions { .. }
86+
)
87+
),
88+
permanent_failure("Backend rejected accepting Terms and Conditions")
89+
);
9090
Ok(())
9191
}
9292

@@ -226,12 +226,10 @@ impl AuthProvider {
226226

227227
if let Some(access_expires_at) = result.access_expires_at.as_ref() {
228228
let access_expires_at = parse_from_rfc3339(access_expires_at)?;
229-
if SystemTime::now() > access_expires_at {
230-
return Err(runtime_error(
231-
GraphQlRuntimeErrorCode::AccessExpired,
232-
"Access expired",
233-
));
234-
}
229+
ensure!(
230+
SystemTime::now() <= access_expires_at,
231+
runtime_error(GraphQlRuntimeErrorCode::AccessExpired, "Access expired")
232+
);
235233
}
236234
info!("Owner: {:?}", result.owner_wallet_pub_key_id);
237235
Ok(result.owner_wallet_pub_key_id.clone())

honey-badger/src/provider.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::secrets::KeyPair;
22
use crate::signing::sign;
33

4-
use graphql::perro::{invalid_input, permanent_failure, runtime_error, OptionToError};
4+
use graphql::perro::{ensure, invalid_input, permanent_failure, runtime_error, OptionToError};
55
use graphql::reqwest::blocking::Client;
66
use graphql::schema::*;
77
use graphql::{build_client, post_blocking};
@@ -89,26 +89,27 @@ impl AuthProvider {
8989
terms: TermsAndConditions,
9090
) -> Result<()> {
9191
info!("Accepting T&C ({:?})...", terms);
92-
if self.auth_level != AuthLevel::Pseudonymous {
93-
return Err(invalid_input(
94-
"Accepting T&C not supported for auth levels other than Pseudonymous",
95-
));
96-
}
92+
ensure!(
93+
self.auth_level == AuthLevel::Pseudonymous,
94+
invalid_input("Accepting T&C not supported for auth levels other than Pseudonymous")
95+
);
9796

9897
let variables = accept_terms_and_conditions::Variables {
9998
service_provider: terms.into(),
10099
};
101100
let client = build_client(Some(&access_token))?;
102101
let data =
103102
post_blocking::<AcceptTermsAndConditions>(&client, &self.backend_url, variables)?;
104-
if !matches!(
105-
data.accept_terms_conditions,
106-
Some(accept_terms_and_conditions::AcceptTermsAndConditionsAcceptTermsConditions { .. })
107-
) {
108-
return Err(permanent_failure(
109-
"Backend rejected accepting Terms and Conditions",
110-
));
111-
}
103+
ensure!(
104+
matches!(
105+
data.accept_terms_conditions,
106+
Some(
107+
accept_terms_and_conditions::AcceptTermsAndConditionsAcceptTermsConditions { .. }
108+
)
109+
),
110+
permanent_failure("Backend rejected accepting Terms and Conditions")
111+
);
112+
112113
Ok(())
113114
}
114115

@@ -243,12 +244,10 @@ impl AuthProvider {
243244

244245
if let Some(access_expires_at) = result.access_expires_at.as_ref() {
245246
let access_expires_at = parse_from_rfc3339(access_expires_at)?;
246-
if SystemTime::now() > access_expires_at {
247-
return Err(runtime_error(
248-
GraphQlRuntimeErrorCode::AccessExpired,
249-
"Access expired",
250-
));
251-
}
247+
ensure!(
248+
SystemTime::now() <= access_expires_at,
249+
runtime_error(GraphQlRuntimeErrorCode::AccessExpired, "Access expired")
250+
);
252251
}
253252
info!("Owner: {:?}", result.owner_wallet_pub_key_id);
254253
Ok(result.owner_wallet_pub_key_id.clone())

squirrel/src/lib.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use graphql::errors::*;
2-
use graphql::perro::{runtime_error, MapToError, OptionToError};
2+
use graphql::perro::{MapToError, OptionToError};
33
use graphql::schema::*;
44
use graphql::{build_async_client, post};
55
use honey_badger::asynchronous::Auth;
@@ -43,31 +43,26 @@ impl RemoteBackupClient {
4343
};
4444
let data = post::<RecoverBackup>(&client, &self.backend_url, variables).await?;
4545

46-
match data.recover_backup {
47-
None => Err(runtime_error(
48-
GraphQlRuntimeErrorCode::ObjectNotFound,
49-
"No backup found with the provided schema name",
50-
)),
51-
Some(d) => {
52-
let encrypted_backup =
53-
graphql_hex_decode(&d.encrypted_backup.ok_or_runtime_error(
54-
GraphQlRuntimeErrorCode::ObjectNotFound,
55-
"No backup found with the provided schema name",
56-
)?)
57-
.map_to_runtime_error(
58-
GraphQlRuntimeErrorCode::CorruptData,
59-
"Encrypted backup invalid hex",
60-
)?;
61-
let schema_version = d.schema_version.ok_or_permanent_failure(
62-
"Backend returned encrypted backup but no schema version",
63-
)?;
64-
Ok(Backup {
65-
encrypted_backup,
66-
schema_name: schema_name.to_string(),
67-
schema_version,
68-
})
69-
}
70-
}
46+
let d = data.recover_backup.ok_or_runtime_error(
47+
GraphQlRuntimeErrorCode::ObjectNotFound,
48+
"No backup found with the provided schema name",
49+
)?;
50+
let encrypted_backup = graphql_hex_decode(&d.encrypted_backup.ok_or_runtime_error(
51+
GraphQlRuntimeErrorCode::ObjectNotFound,
52+
"No backup found with the provided schema name",
53+
)?)
54+
.map_to_runtime_error(
55+
GraphQlRuntimeErrorCode::CorruptData,
56+
"Encrypted backup invalid hex",
57+
)?;
58+
let schema_version = d
59+
.schema_version
60+
.ok_or_permanent_failure("Backend returned encrypted backup but no schema version")?;
61+
Ok(Backup {
62+
encrypted_backup,
63+
schema_name: schema_name.to_string(),
64+
schema_version,
65+
})
7166
}
7267
}
7368

0 commit comments

Comments
 (0)