-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality to output application information in pretty formatting (human-readable) or plain json for integrating application output with other tools.
- Loading branch information
Showing
7 changed files
with
84 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use serde::Serialize; | ||
use tabled::Table; | ||
use tabled::Tabled; | ||
use crate::OutputFormat; | ||
|
||
pub trait Formattable: Serialize + Tabled + Sized { | ||
fn format(&self, format: OutputFormat) -> String { | ||
match format { | ||
OutputFormat::Pretty => { | ||
Table::new(std::iter::once(self)) | ||
.with(tabled::settings::Style::modern()) | ||
.to_string() | ||
} | ||
OutputFormat::Json => { | ||
serde_json::to_string_pretty(self).unwrap_or_else(|_| "Error serializing to JSON".to_string()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct FormattableVec<T: Formattable>(Vec<T>); | ||
|
||
impl<T: Formattable> FormattableVec<T> { | ||
pub fn new(items: Vec<T>) -> Self { | ||
Self(items) | ||
} | ||
|
||
pub fn format(&self, format: OutputFormat) -> String { | ||
match format { | ||
OutputFormat::Pretty => { | ||
if self.0.is_empty() { | ||
return "No items found.".to_string(); | ||
} | ||
Table::new(&self.0) | ||
.with(tabled::settings::Style::modern()) | ||
.to_string() | ||
} | ||
OutputFormat::Json => { | ||
serde_json::to_string_pretty(&self.0).unwrap_or_else(|_| "Error serializing to JSON".to_string()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters