Skip to content

Commit 5b86ad5

Browse files
committed
Tweak rust code to be slightly more idiomatic
1 parent b707e10 commit 5b86ad5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/web/services/user/profile.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,19 @@ async fn save_changes(
247247
let cohort: Option<i64> = cohort.parse::<i64>().ok();
248248

249249
// Check that the current user role can switch to the submitted role.
250-
if form.template["context"]["roles"].get(role.to_string()).and_then(Value::as_bool).unwrap_or(false) {
251-
return Err(TelescopeError::BadRequest {
250+
form.template["context"]["roles"]
251+
// Index the selected role availability.
252+
.get(role.to_string())
253+
// Convert to bool, default as false.
254+
.and_then(Value::as_bool)
255+
.unwrap_or(false)
256+
// Create and propagate error on false.
257+
.then(|| ())
258+
.ok_or(TelescopeError::BadRequest {
252259
header: "Invalid Role Selection".into(),
253260
message: "The selected role is not available at this time".into(),
254261
show_status_code: false
255-
});
256-
}
262+
})?;
257263

258264
// Fill the form with the submitted info.
259265
form.template["context"]["first_name"] = json!(&first_name);

0 commit comments

Comments
 (0)