Fix some pedantic lints

This commit is contained in:
Valentin Brandl 2023-06-13 10:22:37 +02:00
parent b07c732475
commit 361eacbe1d
8 changed files with 50 additions and 27 deletions

View File

@ -39,7 +39,10 @@ impl<'a> CacheState<'a> {
Ok(cache
.entries
.get(branch)
.map(|c| {
.map_or_else(
// TODO: get rid of clone
|| CacheState::NoneForBranch(cache.clone()),
|c| {
if c.head == head {
trace!("Cache is up to date");
CacheState::Current {
@ -57,8 +60,7 @@ impl<'a> CacheState<'a> {
}
}
})
// TODO: get rid of clone
.unwrap_or_else(|| CacheState::NoneForBranch(cache.clone())))
)
} else {
Ok(CacheState::No)
}

View File

@ -18,6 +18,12 @@ pub struct Settings {
}
impl Settings {
/// Load the configuration from file and environment.
///
/// # Errors
///
/// * File cannot be read or parsed
/// * Environment variables cannot be parsed
pub fn load() -> Result<Self, ConfigError> {
Config::builder()
.add_source(File::with_name("hoc.toml").required(false))

View File

@ -22,7 +22,7 @@ use crate::{
cache::CacheState,
config::Settings,
error::{Error, Result},
service::{Bitbucket, FormService, GitHub, Gitlab, Service, Sourcehut},
service::{Bitbucket, FormValue, GitHub, Gitlab, Service, Sourcehut},
statics::{CLIENT, VERSION_INFO},
template::{RepoGeneratorInfo, RepoInfo},
};
@ -53,7 +53,7 @@ include!(concat!(env!("OUT_DIR"), "/templates.rs"));
#[derive(Deserialize, Serialize)]
struct GeneratorForm<'a> {
service: FormService,
service: FormValue,
user: Cow<'a, str>,
repo: Cow<'a, str>,
branch: Option<Cow<'a, str>>,
@ -309,7 +309,7 @@ pub(crate) async fn json_hoc<T: Service>(
let branch = branch.branch.as_deref().unwrap_or("master");
let rc_clone = repo_count.clone();
let mapper = move |r| match r {
HocResult::NotFound => p404(rc_clone),
HocResult::NotFound => p404(&rc_clone),
HocResult::Hoc {
hoc, head, commits, ..
} => Ok(HttpResponse::Ok().json(JsonResponse {
@ -345,7 +345,7 @@ pub(crate) async fn calculate_hoc<T: Service>(
let rc_clone = repo_count.clone();
let label = query.label.clone();
let mapper = move |r| match r {
HocResult::NotFound => p404(rc_clone),
HocResult::NotFound => p404(&rc_clone),
HocResult::Hoc { hoc_pretty, .. } => {
let badge_opt = BadgeOptions {
subject: label,
@ -386,7 +386,7 @@ async fn overview<T: Service>(
let base_url = state.settings.base_url.clone();
let rc_clone = repo_count.clone();
let mapper = move |r| match r {
HocResult::NotFound => p404(rc_clone),
HocResult::NotFound => p404(&rc_clone),
HocResult::Hoc {
hoc,
commits,
@ -413,7 +413,7 @@ async fn overview<T: Service>(
VERSION_INFO,
rc_clone.load(Ordering::Relaxed),
repo_info,
label,
&label,
)?;
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
@ -423,11 +423,13 @@ async fn overview<T: Service>(
}
#[get("/health_check")]
#[allow(clippy::unused_async)]
async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}
#[get("/")]
#[allow(clippy::unused_async)]
async fn index(
state: web::Data<State>,
repo_count: web::Data<AtomicUsize>,
@ -443,6 +445,7 @@ async fn index(
}
#[post("/generate")]
#[allow(clippy::unused_async)]
async fn generate(
params: web::Form<GeneratorForm<'_>>,
state: web::Data<State>,
@ -470,20 +473,22 @@ async fn generate(
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
}
fn p404(repo_count: web::Data<AtomicUsize>) -> Result<HttpResponse> {
fn p404(repo_count: &web::Data<AtomicUsize>) -> Result<HttpResponse> {
let mut buf = Vec::new();
templates::p404_html(&mut buf, VERSION_INFO, repo_count.load(Ordering::Relaxed))?;
Ok(HttpResponse::NotFound().content_type("text/html").body(buf))
}
#[allow(clippy::unused_async)]
async fn async_p404(repo_count: web::Data<AtomicUsize>) -> Result<HttpResponse> {
p404(repo_count)
p404(&repo_count)
}
/// A duration to add to current time for a far expires header.
static FAR: Duration = Duration::from_secs(180 * 24 * 60 * 60);
#[get("/static/{filename}")]
#[allow(clippy::unused_async)]
async fn static_file(
path: web::Path<String>,
repo_count: web::Data<AtomicUsize>,
@ -496,11 +501,13 @@ async fn static_file(
.content_type(data.mime.clone())
.body(data.content)
})
.map(Result::Ok)
.unwrap_or_else(|| p404(repo_count))
.map_or_else(
|| p404(&repo_count),
Result::Ok)
}
#[get("/favicon.ico")]
#[allow(clippy::unused_async)]
async fn favicon32() -> HttpResponse {
let data = &template_statics::favicon32_png;
HttpResponse::Ok()
@ -508,6 +515,7 @@ async fn favicon32() -> HttpResponse {
.body(data.content)
}
#[allow(clippy::unused_async)]
async fn start_server(listener: TcpListener, settings: Settings) -> std::io::Result<Server> {
let workers = settings.workers;
let repo_count =
@ -536,6 +544,11 @@ async fn start_server(listener: TcpListener, settings: Settings) -> std::io::Res
.run())
}
/// Start the server.
///
/// # Errors
///
/// * server cannot bind to `listener`
pub async fn run(listener: TcpListener, settings: Settings) -> std::io::Result<Server> {
let span = info_span!("hoc", version = env!("CARGO_PKG_VERSION"));
let _ = span.enter();

View File

@ -29,7 +29,7 @@ pub(crate) trait Service: Sized + 'static {
}
#[derive(Deserialize, Serialize, Clone, Copy)]
pub enum FormService {
pub enum FormValue {
#[serde(rename = "github")]
GitHub,
#[serde(rename = "gitlab")]
@ -40,22 +40,22 @@ pub enum FormService {
Sourcehut,
}
impl FormService {
impl FormValue {
pub(crate) fn url(&self) -> &str {
match self {
FormService::GitHub => "github.com",
FormService::Gitlab => "gitlab.com",
FormService::Bitbucket => "bitbucket.org",
FormService::Sourcehut => "git.sr.ht",
FormValue::GitHub => "github.com",
FormValue::Gitlab => "gitlab.com",
FormValue::Bitbucket => "bitbucket.org",
FormValue::Sourcehut => "git.sr.ht",
}
}
pub(crate) fn service(&self) -> &str {
match self {
FormService::GitHub => "github",
FormService::Gitlab => "gitlab",
FormService::Bitbucket => "bitbucket",
FormService::Sourcehut => "sourcehut",
FormValue::GitHub => "github",
FormValue::Gitlab => "gitlab",
FormValue::Bitbucket => "bitbucket",
FormValue::Sourcehut => "sourcehut",
}
}
}

View File

@ -1,3 +1,4 @@
#[derive(Clone, Copy)]
pub struct VersionInfo<'a> {
pub commit: &'a str,
pub version: &'a str,

View File

@ -1,5 +1,6 @@
use crate::service::FormService;
use crate::service::FormValue;
#[derive(Clone, Copy)]
pub struct RepoInfo<'a> {
pub commit_url: &'a str,
pub commits: u64,
@ -13,7 +14,7 @@ pub struct RepoInfo<'a> {
}
pub struct RepoGeneratorInfo<'a> {
pub service: FormService,
pub service: FormValue,
pub user: &'a str,
pub repo: &'a str,
pub branch: &'a str,

View File

@ -1,4 +1,4 @@
@use super::statics::*;
@use super::statics::tacit_css_min_css;
@use crate::statics::VersionInfo;
@(title: &str, header: &str, content: Content, version_info: VersionInfo, repo_count: usize)

View File

@ -2,7 +2,7 @@
@use crate::statics::VersionInfo;
@use crate::template::RepoInfo;
@(version_info: VersionInfo, repo_count: usize, repo_info: RepoInfo, label: String)
@(version_info: VersionInfo, repo_count: usize, repo_info: RepoInfo, label: &str)
@:base_html("Hits-of-Code Badges", "Overview", {