From dbbbdad3faa18d1e5e12c911fbe2a94612fb73d8 Mon Sep 17 00:00:00 2001
From: Valentin Brandl
Date: Thu, 18 Aug 2022 14:07:07 +0200
Subject: [PATCH 1/2] Allow providing a branch name in generator
---
src/lib.rs | 6 ++++++
templates/generate.rs.html | 6 +++---
templates/index.rs.html | 2 ++
3 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
index baa9c7e..412b0be 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -55,6 +55,7 @@ struct GeneratorForm<'a> {
service: FormService,
user: Cow<'a, str>,
repo: Cow<'a, str>,
+ branch: Option>,
}
#[derive(Debug)]
@@ -447,6 +448,11 @@ async fn generate(
params.service.url(),
params.service.service(),
&repo,
+ params
+ .branch
+ .as_deref()
+ .filter(|s| !s.is_empty())
+ .unwrap_or("master"),
)?;
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
diff --git a/templates/generate.rs.html b/templates/generate.rs.html
index f4ad6dc..78194ec 100644
--- a/templates/generate.rs.html
+++ b/templates/generate.rs.html
@@ -1,7 +1,7 @@
@use super::base;
@use crate::statics::VersionInfo;
-@(version_info: VersionInfo, repo_count: usize, base_url: &str, url: &str, service: &str, path: &str)
+@(version_info: VersionInfo, repo_count: usize, base_url: &str, url: &str, service: &str, path: &str, branch: &str)
@:base("Hits-of-Code Badges", "Badge Generator", {
@@ -10,7 +10,7 @@ Here is the markdown for the badge for @url/@path
-[![Hits-of-Code](@base_url/@service/@path)](@base_url/@service/@path/view)
+[![Hits-of-Code](@base_url/@service/@path?branch=@branch)](@base_url/@service/@path/view?branch=@branch)
@@ -18,6 +18,6 @@ It will be rendered like this
-
+
}, version_info, repo_count)
diff --git a/templates/index.rs.html b/templates/index.rs.html
index 9dd2520..aebc661 100644
--- a/templates/index.rs.html
+++ b/templates/index.rs.html
@@ -80,6 +80,8 @@ the lines of
+
+
From d4248df45a28937cdec1cf319c2e02ebfd3a5c4b Mon Sep 17 00:00:00 2001
From: Valentin Brandl
Date: Fri, 19 Aug 2022 10:34:37 +0200
Subject: [PATCH 2/2] Use helper struct to reduce template parameters
---
src/lib.rs | 22 ++++++++++++----------
src/service.rs | 4 ++--
src/template.rs | 9 +++++++++
templates/generate.rs.html | 10 +++++-----
4 files changed, 28 insertions(+), 17 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
index 412b0be..9eb0bdc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -24,7 +24,7 @@ use crate::{
error::{Error, Result},
service::{Bitbucket, FormService, GitHub, Gitlab, Service, Sourcehut},
statics::{CLIENT, CSS, FAVICON, VERSION_INFO},
- template::RepoInfo,
+ template::{RepoGeneratorInfo, RepoInfo},
};
use actix_web::{
dev::Server,
@@ -438,21 +438,23 @@ async fn generate(
state: web::Data,
repo_count: web::Data,
) -> Result {
- let repo = format!("{}/{}", params.user, params.repo);
let mut buf = Vec::new();
+ let repo_info = RepoGeneratorInfo {
+ service: params.service,
+ user: ¶ms.user,
+ repo: ¶ms.repo,
+ branch: params
+ .branch
+ .as_deref()
+ .filter(|s| !s.is_empty())
+ .unwrap_or("master"),
+ };
templates::generate(
&mut buf,
VERSION_INFO,
repo_count.load(Ordering::Relaxed),
&state.settings.base_url,
- params.service.url(),
- params.service.service(),
- &repo,
- params
- .branch
- .as_deref()
- .filter(|s| !s.is_empty())
- .unwrap_or("master"),
+ &repo_info,
)?;
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
diff --git a/src/service.rs b/src/service.rs
index ce95c97..531bd9b 100644
--- a/src/service.rs
+++ b/src/service.rs
@@ -28,8 +28,8 @@ pub(crate) trait Service: Sized + 'static {
}
}
-#[derive(Deserialize, Serialize)]
-pub(crate) enum FormService {
+#[derive(Deserialize, Serialize, Clone, Copy)]
+pub enum FormService {
#[serde(rename = "github")]
GitHub,
#[serde(rename = "gitlab")]
diff --git a/src/template.rs b/src/template.rs
index ceae682..d90b05d 100644
--- a/src/template.rs
+++ b/src/template.rs
@@ -1,3 +1,5 @@
+use crate::service::FormService;
+
pub struct RepoInfo<'a> {
pub commit_url: &'a str,
pub commits: u64,
@@ -9,3 +11,10 @@ pub struct RepoInfo<'a> {
pub url: &'a str,
pub branch: &'a str,
}
+
+pub struct RepoGeneratorInfo<'a> {
+ pub service: FormService,
+ pub user: &'a str,
+ pub repo: &'a str,
+ pub branch: &'a str,
+}
diff --git a/templates/generate.rs.html b/templates/generate.rs.html
index 78194ec..2ce62cc 100644
--- a/templates/generate.rs.html
+++ b/templates/generate.rs.html
@@ -1,16 +1,16 @@
@use super::base;
-@use crate::statics::VersionInfo;
+@use crate::{statics::VersionInfo, template::RepoGeneratorInfo};
-@(version_info: VersionInfo, repo_count: usize, base_url: &str, url: &str, service: &str, path: &str, branch: &str)
+@(version_info: VersionInfo, repo_count: usize, base_url: &str, repo_info: &RepoGeneratorInfo)
@:base("Hits-of-Code Badges", "Badge Generator", {
-Here is the markdown for the badge for @url/@path
+Here is the markdown for the badge for @repo_info.user/@repo_info.repo
-[![Hits-of-Code](@base_url/@service/@path?branch=@branch)](@base_url/@service/@path/view?branch=@branch)
+[![Hits-of-Code](@base_url/@repo_info.service.service()/@repo_info.user/@repo_info.repo?branch=@repo_info.branch)](@base_url/@repo_info.service.service()/@repo_info.user/@repo_info.repo/view?branch=@repo_info.branch)
@@ -18,6 +18,6 @@ It will be rendered like this
-
+
}, version_info, repo_count)