httpd: Clean up `json.rs`

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2023-01-19 14:46:14 +00:00 committed by Alexis Sellier
parent ccbcdd4998
commit 59ff46e49c
No known key found for this signature in database
1 changed files with 12 additions and 10 deletions

View File

@ -1,13 +1,14 @@
//! Utilities for building JSON responses of our API. //! Utilities for building JSON responses of our API.
use radicle_surf::{ use std::path::Path;
object::{Blob, Tree},
Commit, Stats, use serde_json::{json, Value};
};
use serde_json::json; use radicle_surf::object::{Blob, Tree};
use radicle_surf::{Commit, Stats};
/// Returns JSON of a commit. /// Returns JSON of a commit.
pub(crate) fn commit(commit: &Commit) -> serde_json::Value { pub(crate) fn commit(commit: &Commit) -> Value {
json!({ json!({
"id": commit.id, "id": commit.id,
"author": { "author": {
@ -25,7 +26,7 @@ pub(crate) fn commit(commit: &Commit) -> serde_json::Value {
} }
/// Returns JSON for a blob with a given `path`. /// Returns JSON for a blob with a given `path`.
pub(crate) fn blob(blob: &Blob, path: &str) -> serde_json::Value { pub(crate) fn blob(blob: &Blob, path: &str) -> Value {
json!({ json!({
"binary": blob.is_binary(), "binary": blob.is_binary(),
"content": blob.content(), "content": blob.content(),
@ -36,8 +37,8 @@ pub(crate) fn blob(blob: &Blob, path: &str) -> serde_json::Value {
} }
/// Returns JSON for a tree with a given `path` and `stats`. /// Returns JSON for a tree with a given `path` and `stats`.
pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> serde_json::Value { pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> Value {
let prefix = std::path::Path::new(path); let prefix = Path::new(path);
let entries = tree let entries = tree
.entries() .entries()
.iter() .iter()
@ -45,11 +46,12 @@ pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> serde_json::Value
json!({ json!({
"path": prefix.join(entry.name()), "path": prefix.join(entry.name()),
"name": entry.name(), "name": entry.name(),
"lastCommit": serde_json::Value::Null, "lastCommit": Value::Null,
"kind": if entry.is_tree() { "tree" } else { "blob" }, "kind": if entry.is_tree() { "tree" } else { "blob" },
}) })
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
json!({ json!({
"entries": &entries, "entries": &entries,
"lastCommit": commit(tree.commit()), "lastCommit": commit(tree.commit()),