Skip to content

Commit 863852e

Browse files
cursoragentlovasoa
andcommitted
Refactor: Improve code formatting and readability
Co-authored-by: contact <[email protected]>
1 parent ad4a25c commit 863852e

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/render.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ impl HeaderContext {
297297
}
298298

299299
async fn authentication(mut self, mut data: JsonValue) -> anyhow::Result<PageContext> {
300-
let password_hash = take_object_str_lower_or_upper(&mut data, "password_hash", "PASSWORD_HASH");
300+
let password_hash =
301+
take_object_str_lower_or_upper(&mut data, "password_hash", "PASSWORD_HASH");
301302
let password = take_object_str_lower_or_upper(&mut data, "password", "PASSWORD");
302303
if let (Some(password), Some(password_hash)) = (password, password_hash) {
303304
log::debug!("Authentication with password_hash = {password_hash:?}");
@@ -308,19 +309,20 @@ impl HeaderContext {
308309
}
309310
log::debug!("Authentication failed");
310311
// The authentication failed
311-
let http_response: HttpResponse = if let Some(link) = get_object_str_lower_or_upper(&data, "link", "LINK") {
312-
self.response
313-
.status(StatusCode::FOUND)
314-
.insert_header((header::LOCATION, link))
315-
.body(
316-
"Sorry, but you are not authorized to access this page. \
312+
let http_response: HttpResponse =
313+
if let Some(link) = get_object_str_lower_or_upper(&data, "link", "LINK") {
314+
self.response
315+
.status(StatusCode::FOUND)
316+
.insert_header((header::LOCATION, link))
317+
.body(
318+
"Sorry, but you are not authorized to access this page. \
317319
Redirecting to the login page...",
318-
)
319-
} else {
320-
anyhow::bail!(ErrorWithStatus {
321-
status: StatusCode::UNAUTHORIZED
322-
})
323-
};
320+
)
321+
} else {
322+
anyhow::bail!(ErrorWithStatus {
323+
status: StatusCode::UNAUTHORIZED
324+
})
325+
};
324326
self.has_status = true;
325327
Ok(PageContext::Close(http_response))
326328
}
@@ -407,18 +409,30 @@ fn take_object_str(json: &mut JsonValue, key: &str) -> Option<String> {
407409
}
408410

409411
#[inline]
410-
fn get_object_value_lower_or_upper<'a>(json: &'a JsonValue, lower: &str, upper: &str) -> Option<&'a JsonValue> {
412+
fn get_object_value_lower_or_upper<'a>(
413+
json: &'a JsonValue,
414+
lower: &str,
415+
upper: &str,
416+
) -> Option<&'a JsonValue> {
411417
json.as_object()
412418
.and_then(|obj| obj.get(lower).or_else(|| obj.get(upper)))
413419
}
414420

415421
#[inline]
416-
fn get_object_str_lower_or_upper<'a>(json: &'a JsonValue, lower: &str, upper: &str) -> Option<&'a str> {
422+
fn get_object_str_lower_or_upper<'a>(
423+
json: &'a JsonValue,
424+
lower: &str,
425+
upper: &str,
426+
) -> Option<&'a str> {
417427
get_object_value_lower_or_upper(json, lower, upper).and_then(JsonValue::as_str)
418428
}
419429

420430
#[inline]
421-
fn take_object_str_lower_or_upper(json: &mut JsonValue, lower: &str, upper: &str) -> Option<String> {
431+
fn take_object_str_lower_or_upper(
432+
json: &mut JsonValue,
433+
lower: &str,
434+
upper: &str,
435+
) -> Option<String> {
422436
if let Some(v) = json.get_mut(lower) {
423437
match v.take() {
424438
JsonValue::String(s) => return Some(s),
@@ -716,8 +730,9 @@ impl<W: std::io::Write> HtmlRenderContext<W> {
716730
let shell_row = rows_iter
717731
.next()
718732
.expect("shell row should exist at this point");
719-
let mut shell_component = get_object_str_lower_or_upper(&shell_row, "component", "COMPONENT")
720-
.expect("shell should exist");
733+
let mut shell_component =
734+
get_object_str_lower_or_upper(&shell_row, "component", "COMPONENT")
735+
.expect("shell should exist");
721736
if request_context.is_embedded && shell_component != FRAGMENT_SHELL_COMPONENT {
722737
log::warn!(
723738
"Embedded pages cannot use a shell component! Ignoring the '{shell_component}' component and its properties: {shell_row}"
@@ -948,7 +963,8 @@ fn handle_log_component(
948963
write!(&mut target, " statement {current_statement}")?;
949964
}
950965

951-
let message = get_object_str_lower_or_upper(data, "message", "MESSAGE").context("log: missing property 'message'")?;
966+
let message = get_object_str_lower_or_upper(data, "message", "MESSAGE")
967+
.context("log: missing property 'message'")?;
952968
log::log!(target: &target, log_level, "{message}");
953969
Ok(())
954970
}

0 commit comments

Comments
 (0)