2023-09-24 05:38:22 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
2023-09-24 05:52:27 +02:00
|
|
|
/// Data that can be printed to the console.
|
2023-09-24 05:38:22 +02:00
|
|
|
pub trait Output {
|
2023-09-24 05:52:27 +02:00
|
|
|
/// A human readable representation of the data.
|
2023-09-24 05:38:22 +02:00
|
|
|
fn human_readable(&self) -> String;
|
2023-09-24 05:52:27 +02:00
|
|
|
/// A `json` representation of the data.
|
2023-09-24 05:38:22 +02:00
|
|
|
fn json(&self) -> Result<String, serde_json::Error>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Output for T
|
|
|
|
|
where
|
|
|
|
|
T: ToString + Serialize,
|
|
|
|
|
{
|
|
|
|
|
fn human_readable(&self) -> String {
|
|
|
|
|
self.to_string()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn json(&self) -> Result<String, serde_json::Error> {
|
|
|
|
|
serde_json::to_string_pretty(&self)
|
|
|
|
|
}
|
|
|
|
|
}
|