add ids formatter

This commit is contained in:
Abraham Toriz 2021-07-28 21:16:03 -05:00
parent 36e1ad8786
commit 5dd87616f8
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 12 additions and 0 deletions

View File

@ -10,6 +10,7 @@ use crate::models::Entry;
pub mod text;
pub mod csv;
pub mod json;
pub mod ids;
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
@ -17,6 +18,7 @@ pub enum Formatter {
Text,
Csv,
Json,
Ids,
Custom(String),
}
@ -38,6 +40,7 @@ impl Formatter {
Formatter::Text => text::print_formatted(entries, out, now, ids)?,
Formatter::Csv => csv::print_formatted(entries, out, ids)?,
Formatter::Json => json::print_formatted(entries, out)?,
Formatter::Ids => ids::print_formatted(entries, out)?,
Formatter::Custom(name) => {
panic!("attempted custom formatter with name {} which is not implemented", name);
}
@ -57,6 +60,7 @@ impl FromStr for Formatter {
"text" => Formatter::Text,
"csv" => Formatter::Csv,
"json" => Formatter::Json,
"ids" => Formatter::Ids,
custom_format => Formatter::Custom(custom_format.into()),
})
}

8
src/formatters/ids.rs Normal file
View File

@ -0,0 +1,8 @@
use std::io::Write;
use crate::models::Entry;
use crate::error::Result;
pub fn print_formatted<W: Write>(entries: Vec<Entry>, out: &mut W) -> Result<()> {
Ok(writeln!(out, "{}", entries.into_iter().map(|e| e.id.to_string()).collect::<Vec<_>>().join(" "))?)
}