cargo clippy and fmt

This commit is contained in:
xenofem 2022-05-01 05:28:50 -04:00
parent 8275b940ac
commit bfe7fcde99
3 changed files with 71 additions and 50 deletions

View file

@ -1,7 +1,10 @@
use std::{collections::HashMap, io::ErrorKind, path::PathBuf, str::FromStr};
use log::{debug, error, info, warn};
use rand::{distributions::{Alphanumeric, DistString}, thread_rng, Rng};
use rand::{
distributions::{Alphanumeric, DistString},
thread_rng, Rng,
};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use tokio::{
@ -12,9 +15,9 @@ use tokio::{
const STATE_FILE_NAME: &str = "files.json";
const DEFAULT_STORAGE_DIR: &str = "storage";
const DEFAULT_MAX_LIFETIME: u32 = 30;
const GIGA: u64 = 1024*1024*1024;
const DEFAULT_MAX_UPLOAD_SIZE: u64 = 16*GIGA;
const DEFAULT_MAX_STORAGE_SIZE: u64 = 64*GIGA;
const GIGA: u64 = 1024 * 1024 * 1024;
const DEFAULT_MAX_UPLOAD_SIZE: u64 = 16 * GIGA;
const DEFAULT_MAX_STORAGE_SIZE: u64 = 64 * GIGA;
pub fn gen_storage_code() -> String {
if std::env::var("TRANSBEAM_MNEMONIC_CODES").as_deref() == Ok("false") {
@ -25,15 +28,23 @@ pub fn gen_storage_code() -> String {
}
pub fn is_valid_storage_code(s: &str) -> bool {
s.as_bytes().iter().all(|c| c.is_ascii_alphanumeric() || c == &b'-')
s.as_bytes()
.iter()
.all(|c| c.is_ascii_alphanumeric() || c == &b'-')
}
pub(crate) fn storage_dir() -> PathBuf {
PathBuf::from(std::env::var("TRANSBEAM_STORAGE_DIR").unwrap_or_else(|_| String::from(DEFAULT_STORAGE_DIR)))
PathBuf::from(
std::env::var("TRANSBEAM_STORAGE_DIR")
.unwrap_or_else(|_| String::from(DEFAULT_STORAGE_DIR)),
)
}
fn parse_env_var<T: FromStr>(var: &str, default: T) -> T {
std::env::var(var).ok().and_then(|val| val.parse::<T>().ok()).unwrap_or(default)
std::env::var(var)
.ok()
.and_then(|val| val.parse::<T>().ok())
.unwrap_or(default)
}
pub(crate) fn max_lifetime() -> u32 {
@ -194,7 +205,9 @@ impl FileStore {
) -> std::io::Result<Result<(), u64>> {
let remaining_size = max_total_size().saturating_sub(self.total_size());
let allowed_size = std::cmp::min(remaining_size, max_single_size());
if file.size > allowed_size { return Ok(Err(allowed_size)); }
if file.size > allowed_size {
return Ok(Err(allowed_size));
}
self.0.insert(key, file);
self.save().await.map(Ok)
}
@ -212,7 +225,7 @@ impl FileStore {
pub(crate) async fn remove_expired_files(&mut self) -> std::io::Result<()> {
info!("Checking for expired files");
let now = OffsetDateTime::now_utc();
for (key, file) in std::mem::replace(&mut self.0, HashMap::new()).into_iter() {
for (key, file) in std::mem::take(&mut self.0).into_iter() {
if file.expiry > now {
self.0.insert(key, file);
} else {