From f033c6889e0071b29e75c551586e8e5da1b556a3 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Fri, 26 Jan 2024 12:34:47 +0100 Subject: Use serde_json::json! instead of manually generating JSON --- Cargo.lock | 1 + common/src/lib.rs | 21 --------------------- git-lfs-authenticate/Cargo.toml | 1 + git-lfs-authenticate/src/main.rs | 20 ++++++++++++-------- 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 = [ "anyhow", "chrono", "common", + "serde_json", ] [[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> fmt::Display for HexFmt { } } -pub struct EscJsonFmt<'a>(pub &'a str); - -impl<'a> fmt::Display for EscJsonFmt<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let EscJsonFmt(buf) = self; - for c in buf.chars() { - match c { - '"' => f.write_str("\\\"")?, // quote - '\\' => f.write_str("\\\\")?, // backslash - '\x08' => f.write_str("\\b")?, // backspace - '\x0C' => f.write_str("\\f")?, // form feed - '\n' => f.write_str("\\n")?, // line feed - '\r' => f.write_str("\\r")?, // carriage return - '\t' => f.write_str("\\t")?, // horizontal tab - _ => f.write_char(c)?, - }; - } - Ok(()) - } -} - #[derive(Debug, Copy, Clone)] pub struct Digest { 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" anyhow = "1.0" chrono = "0.4" common = { path = "../common" } +serde_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 @@ use anyhow::{anyhow, bail, Result}; use chrono::Utc; +use serde_json::json; use std::{process::ExitCode, time::Duration}; fn main() -> ExitCode { @@ -39,14 +40,17 @@ fn main() -> ExitCode { return ExitCode::FAILURE; }; - println!( - "{{\"header\":{{\"Authorization\":\"Gitolfs3-Hmac-Sha256 {tag} {}\"}},\ - \"expires_at\":\"{}\",\"href\":\"{}{}/info/lfs\"}}", - expires_at.timestamp(), - common::EscJsonFmt(&expires_at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)), - common::EscJsonFmt(&config.href_base), - common::EscJsonFmt(&repo_name), - ); + let response = json!({ + "header": { + "Authorization": format!( + "Gitolfs3-Hmac-Sha256 {tag} {}", + expires_at.timestamp() + ), + }, + "expires_at": expires_at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true), + "href": format!("{}{}/info/lfs", config.href_base, repo_name), + }); + println!("{}", response.to_string()); ExitCode::SUCCESS } -- cgit v1.2.3