aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2024-01-26 12:34:47 +0100
committerLibravatar Rutger Broekhoff2024-01-26 12:34:47 +0100
commitf033c6889e0071b29e75c551586e8e5da1b556a3 (patch)
tree7ab82ed0c11b6d4a1d45d3526015ecd631790be5
parentf146743061ba8170569bf18518202df9a43c09f3 (diff)
downloadgitolfs3-f033c6889e0071b29e75c551586e8e5da1b556a3.tar.gz
gitolfs3-f033c6889e0071b29e75c551586e8e5da1b556a3.zip
Use serde_json::json! instead of manually generating JSON
-rw-r--r--Cargo.lock1
-rw-r--r--common/src/lib.rs21
-rw-r--r--git-lfs-authenticate/Cargo.toml1
-rw-r--r--git-lfs-authenticate/src/main.rs20
4 files changed, 14 insertions, 29 deletions
diff --git a/Cargo.lock b/Cargo.lock
index cc204f1..946d704 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -878,6 +878,7 @@ dependencies = [
878 "anyhow", 878 "anyhow",
879 "chrono", 879 "chrono",
880 "common", 880 "common",
881 "serde_json",
881] 882]
882 883
883[[package]] 884[[package]]
diff --git a/common/src/lib.rs b/common/src/lib.rs
index 0a538a5..c26150d 100644
--- a/common/src/lib.rs
+++ b/common/src/lib.rs
@@ -254,27 +254,6 @@ impl<B: AsRef<[u8]>> fmt::Display for HexFmt<B> {
254 } 254 }
255} 255}
256 256
257pub struct EscJsonFmt<'a>(pub &'a str);
258
259impl<'a> fmt::Display for EscJsonFmt<'a> {
260 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
261 let EscJsonFmt(buf) = self;
262 for c in buf.chars() {
263 match c {
264 '"' => f.write_str("\\\"")?, // quote
265 '\\' => f.write_str("\\\\")?, // backslash
266 '\x08' => f.write_str("\\b")?, // backspace
267 '\x0C' => f.write_str("\\f")?, // form feed
268 '\n' => f.write_str("\\n")?, // line feed
269 '\r' => f.write_str("\\r")?, // carriage return
270 '\t' => f.write_str("\\t")?, // horizontal tab
271 _ => f.write_char(c)?,
272 };
273 }
274 Ok(())
275 }
276}
277
278#[derive(Debug, Copy, Clone)] 257#[derive(Debug, Copy, Clone)]
279pub struct Digest<const N: usize> { 258pub struct Digest<const N: usize> {
280 inner: [u8; N], 259 inner: [u8; N],
diff --git a/git-lfs-authenticate/Cargo.toml b/git-lfs-authenticate/Cargo.toml
index f4ab4d7..15feba8 100644
--- a/git-lfs-authenticate/Cargo.toml
+++ b/git-lfs-authenticate/Cargo.toml
@@ -7,3 +7,4 @@ edition = "2021"
7anyhow = "1.0" 7anyhow = "1.0"
8chrono = "0.4" 8chrono = "0.4"
9common = { path = "../common" } 9common = { path = "../common" }
10serde_json = "1"
diff --git a/git-lfs-authenticate/src/main.rs b/git-lfs-authenticate/src/main.rs
index accc37f..3101c92 100644
--- a/git-lfs-authenticate/src/main.rs
+++ b/git-lfs-authenticate/src/main.rs
@@ -1,5 +1,6 @@
1use anyhow::{anyhow, bail, Result}; 1use anyhow::{anyhow, bail, Result};
2use chrono::Utc; 2use chrono::Utc;
3use serde_json::json;
3use std::{process::ExitCode, time::Duration}; 4use std::{process::ExitCode, time::Duration};
4 5
5fn main() -> ExitCode { 6fn main() -> ExitCode {
@@ -39,14 +40,17 @@ fn main() -> ExitCode {
39 return ExitCode::FAILURE; 40 return ExitCode::FAILURE;
40 }; 41 };
41 42
42 println!( 43 let response = json!({
43 "{{\"header\":{{\"Authorization\":\"Gitolfs3-Hmac-Sha256 {tag} {}\"}},\ 44 "header": {
44 \"expires_at\":\"{}\",\"href\":\"{}{}/info/lfs\"}}", 45 "Authorization": format!(
45 expires_at.timestamp(), 46 "Gitolfs3-Hmac-Sha256 {tag} {}",
46 common::EscJsonFmt(&expires_at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)), 47 expires_at.timestamp()
47 common::EscJsonFmt(&config.href_base), 48 ),
48 common::EscJsonFmt(&repo_name), 49 },
49 ); 50 "expires_at": expires_at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true),
51 "href": format!("{}{}/info/lfs", config.href_base, repo_name),
52 });
53 println!("{}", response.to_string());
50 54
51 ExitCode::SUCCESS 55 ExitCode::SUCCESS
52} 56}