From 953feee4c4f4bf3bb7ed645e59bf7a3967185d21 Mon Sep 17 00:00:00 2001 From: mokurin000 <1348292515a@gmail.com> Date: Fri, 1 Aug 2025 16:25:27 +0800 Subject: [PATCH] refactor: extract `json_display` --- sdgb-cli/src/main.rs | 14 +++++--------- sdgb-cli/src/utils/mod.rs | 9 +++++++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/sdgb-cli/src/main.rs b/sdgb-cli/src/main.rs index 27fa953..7816155 100644 --- a/sdgb-cli/src/main.rs +++ b/sdgb-cli/src/main.rs @@ -1,9 +1,6 @@ -use std::{ - io::stdout, - sync::{ - Arc, - atomic::{AtomicBool, Ordering}, - }, +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, }; use nyquest_preset::nyquest::ClientBuilder; @@ -28,7 +25,7 @@ use spdlog::{error, info, warn}; use crate::{ commands::Cli, - utils::{human_readable_display, login_action}, + utils::{human_readable_display, json_display, login_action}, }; #[cfg(feature = "fetchall")] @@ -139,8 +136,7 @@ async fn main() -> Result<(), Box> { } if !human_readable { - let lock = stdout().lock(); - serde_json::to_writer_pretty(lock, &resp)?; + json_display(resp)?; } } diff --git a/sdgb-cli/src/utils/mod.rs b/sdgb-cli/src/utils/mod.rs index 01171b7..b16b26e 100644 --- a/sdgb-cli/src/utils/mod.rs +++ b/sdgb-cli/src/utils/mod.rs @@ -48,6 +48,12 @@ pub async fn login_action( Ok(return_data) } +pub fn json_display(value: impl Serialize) -> Result<(), Box> { + let lock = stdout().lock(); + serde_json::to_writer_pretty(lock, &value)?; + Ok(()) +} + pub fn human_readable_display( value: impl Display + Serialize, human_readable: bool, @@ -55,8 +61,7 @@ pub fn human_readable_display( if human_readable { println!("{value}"); } else { - let lock = stdout().lock(); - serde_json::to_writer_pretty(lock, &value)?; + json_display(value)?; } Ok(())