♻️ refactor: flatten out indentation

main
_ 2021-04-03 14:27:17 +00:00
parent ac289e18f7
commit e5bf5f7d1f
1 changed files with 36 additions and 35 deletions

View File

@ -119,6 +119,8 @@ async fn serve_file (
) )
-> Result <Response, FileServerError> -> Result <Response, FileServerError>
{ {
use range::Parsed::*;
let file_md = file.metadata ().await.map_err (FileServerError::CantGetFileMetadata)?; let file_md = file.metadata ().await.map_err (FileServerError::CantGetFileMetadata)?;
#[cfg (unix)] #[cfg (unix)]
@ -134,41 +136,40 @@ async fn serve_file (
let range_header = headers.get ("range").and_then (|v| std::str::from_utf8 (v).ok ()); let range_header = headers.get ("range").and_then (|v| std::str::from_utf8 (v).ok ());
Ok (match range::check (range_header, file_len) { let range = match range::check (range_header, file_len) {
range::Parsed::NotSatisfiable (file_len) => Response::RangeNotSatisfiable (file_len), NotSatisfiable (file_len) => return Ok (Response::RangeNotSatisfiable (file_len)),
range::Parsed::Valid (range) => { Valid (range) => range,
if uri.query () == Some ("as_markdown") { };
const MAX_BUF_SIZE: u32 = 1_000_000;
if uri.query () == Some ("as_markdown") {
if range.range_requested { const MAX_BUF_SIZE: u32 = 1_000_000;
return Ok (Response::InvalidQuery);
} if range.range_requested {
return Ok (Response::InvalidQuery);
if file_len > MAX_BUF_SIZE.into () { }
Response::MarkdownErr (markdown::Error::TooBig)
} if file_len > MAX_BUF_SIZE.into () {
else { return Ok (Response::MarkdownErr (markdown::Error::TooBig));
let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().expect ("Couldn't fit u32 into usize")]; }
let bytes_read = file.read (&mut buffer).await?; else {
buffer.truncate (bytes_read); let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().expect ("Couldn't fit u32 into usize")];
let bytes_read = file.read (&mut buffer).await?;
match render_styled (&buffer) { buffer.truncate (bytes_read);
Ok (x) => Response::MarkdownPreview (x),
Err (x) => Response::MarkdownErr (x), return Ok (match render_styled (&buffer) {
} Ok (x) => Response::MarkdownPreview (x),
} Err (x) => Response::MarkdownErr (x),
} });
else { }
let file = file.into (); }
Response::ServeFile (ServeFileParams { let file = file.into ();
file,
send_body, Ok (Response::ServeFile (ServeFileParams {
range, file,
}) send_body,
} range,
}, }))
})
} }
async fn serve_api ( async fn serve_api (