Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy warnings and add a crates.io badge #2

Merged
merged 2 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
A simple, light and standalone pastebin, URL shortener and file-sharing service that hosts **fi**les, redirects **li**nks and stores **te**xts.

[![GitHub Actions](https://github.com/raftario/filite/workflows/Build/badge.svg)](https://github.com/raftario/filite/actions?workflowID=Build)
[![Crates.io](https://img.shields.io/crates/v/filite.svg)](https://crates.io/crates/filite)

## Features

Expand Down
9 changes: 5 additions & 4 deletions src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ macro_rules! common_select {
$q = $q.limit(limit);
}

match $f.asc {
false => $q = $q.order(created.desc()),
true => $q = $q.order(created.asc()),
}
$q = if $f.asc {
$q.order(created.asc())
} else {
$q.order(created.desc())
};
};
}

Expand Down
30 changes: 15 additions & 15 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ fn auth(
Err(_) => return Err(HttpResponse::BadRequest().body("Invalid Authorization header")),
};

match setup::hash(password).as_slice() == password_hash {
true => match String::from_utf8(user.to_vec()) {
if setup::hash(password).as_slice() == password_hash {
match String::from_utf8(user.to_vec()) {
Ok(u) => {
identity.remember(u);
Ok(())
}
Err(_) => Err(HttpResponse::BadRequest().body("Invalid Authorization header")),
},
false => Err(HttpResponse::Unauthorized()
}
} else {
Err(HttpResponse::Unauthorized()
.header("WWW-Authenticate", "Basic realm=\"filite\"")
.body("Unauthorized")),
.body("Unauthorized"))
}
}

Expand Down Expand Up @@ -97,7 +98,7 @@ fn match_find_error<T>(error: BlockingError<diesel::result::Error>) -> Result<T,
/// Formats a timestamp to the "Last-Modified" header format
fn timestamp_to_last_modified(timestamp: i32) -> String {
let datetime =
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(timestamp as i64, 0), Utc);
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(i64::from(timestamp), 0), Utc);
datetime.format("%a, %d %b %Y %H:%M:%S GMT").to_string()
}

Expand Down Expand Up @@ -233,14 +234,13 @@ pub fn get_config(

/// Logout route
pub fn logout(identity: Identity) -> impl Responder {
match identity.identity().is_some() {
true => {
identity.forget();
HttpResponse::Ok().body("Logged out")
}
false => HttpResponse::Unauthorized()
if identity.identity().is_some() {
identity.forget();
HttpResponse::Ok().body("Logged out")
} else {
HttpResponse::Unauthorized()
.header("WWW-Authenticate", "Basic realm=\"filite\"")
.body("Unauthorized"),
.body("Unauthorized")
}
}

Expand Down Expand Up @@ -402,7 +402,7 @@ pub mod links {
.and_then(move |_| future::result(parse_id(&path)))
.and_then(move |id| {
web::block(move || queries::links::replace(id, &body.forward, pool))
.then(|result| match_replace_result(result))
.then(match_replace_result)
})
.from_err()
}
Expand Down Expand Up @@ -460,7 +460,7 @@ pub mod texts {
.and_then(move |_| future::result(parse_id(&path)))
.and_then(move |id| {
web::block(move || queries::texts::replace(id, &body.contents, pool))
.then(|result| match_replace_result(result))
.then(match_replace_result)
})
.from_err()
}
Expand Down
14 changes: 4 additions & 10 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ impl Default for Config {
.expect("Can't convert database path to string")
.to_owned()
};
let pool_size = {
let n = num_cpus::get() as u32 / 2;
match n < 1 {
true => 1,
false => n,
}
};
let pool_size = std::cmp::max(1, num_cpus::get() as u32 / 2);
let files_dir = {
let mut path = get_data_dir();
path.push("files");
Expand Down Expand Up @@ -141,7 +135,7 @@ impl Config {
let mut result: Config = result.unwrap();

if result.files_dir.is_absolute() {
if let Err(_) = fs::create_dir_all(&result.files_dir) {
if fs::create_dir_all(&result.files_dir).is_err() {
return Err("Can't create files_dir.");
}

Expand All @@ -153,7 +147,7 @@ impl Config {
let mut data_dir = get_data_dir();
data_dir.push(&result.files_dir);

if let Err(_) = fs::create_dir_all(&data_dir) {
if fs::create_dir_all(&data_dir).is_err() {
return Err("Can't create files_dir.");
}

Expand Down Expand Up @@ -279,7 +273,7 @@ pub fn init() -> Config {
process::exit(1);
});

let mut config_path = data_dir.clone();
let mut config_path = data_dir;
config_path.push("config.toml");
eprintln!(
"Almost ready. To get started, edit the config file at {} and restart.",
Expand Down