feat: implement ping command"
This commit is contained in:
1 parent
fb0bbae1f0
commit
8045814b3b
3 files changed
+313
-10
No files matched your search
+30
-1
@@ -1,3 +1,5 @@
|
||||
use std::io::Write;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{CliError, CliResult};
|
||||
@@ -13,6 +15,12 @@ struct ErrorEnvelope<'a> {
|
||||
pub error: ErrorBody<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SuccessResponse {
|
||||
pub ok: bool,
|
||||
}
|
||||
|
||||
pub fn render_json<T: Serialize>(value: &T, pretty: bool) -> CliResult<String> {
|
||||
if pretty {
|
||||
serde_json::to_string_pretty(value).map_err(|_| CliError::runtime("Failed to serialize output"))
|
||||
@@ -32,6 +40,16 @@ pub fn render_error(err: &CliError, pretty: bool) -> CliResult<String> {
|
||||
render_json(&envelope, pretty)
|
||||
}
|
||||
|
||||
pub fn write_json<W: Write, T: Serialize>(
|
||||
writer: &mut W,
|
||||
value: &T,
|
||||
pretty: bool,
|
||||
) -> CliResult<()> {
|
||||
let rendered = render_json(value, pretty)?;
|
||||
writeln!(writer, "{rendered}").map_err(|_| CliError::runtime("Failed to write output"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -75,5 +93,16 @@ mod tests {
|
||||
));
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_json_when_called_with_writer_it_should_write_formatted_string() {
|
||||
let data = json!({"ok": true});
|
||||
let mut buffer = Vec::new();
|
||||
|
||||
let result = write_json(&mut buffer, &data, false);
|
||||
assert_eq!(result, Ok(()));
|
||||
|
||||
let output_str = String::from_utf8(buffer).unwrap();
|
||||
assert_eq!(output_str, "{\"ok\":true}\n");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user