2021-06-21 17:38:51 -05:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::io::Write;
|
|
|
|
|
2021-06-18 11:27:19 -05:00
|
|
|
use serde::{Serialize, Deserialize};
|
2021-06-30 13:27:42 -05:00
|
|
|
use chrono::{DateTime, Utc};
|
2021-06-18 11:27:19 -05:00
|
|
|
|
2021-06-21 17:38:51 -05:00
|
|
|
use crate::error;
|
|
|
|
use crate::models::Entry;
|
|
|
|
|
2021-06-30 13:27:42 -05:00
|
|
|
pub mod text;
|
2021-06-30 18:51:34 -05:00
|
|
|
pub mod csv;
|
2021-07-26 20:32:13 -05:00
|
|
|
pub mod json;
|
2021-07-28 21:16:03 -05:00
|
|
|
pub mod ids;
|
2021-07-29 11:02:00 -05:00
|
|
|
pub mod ical;
|
2021-06-28 19:32:29 -05:00
|
|
|
|
2021-06-18 11:27:19 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
pub enum Formatter {
|
|
|
|
Text,
|
2021-06-30 18:51:34 -05:00
|
|
|
Csv,
|
2021-07-26 20:32:13 -05:00
|
|
|
Json,
|
2021-07-28 21:16:03 -05:00
|
|
|
Ids,
|
2021-07-29 11:02:00 -05:00
|
|
|
Ical,
|
2021-06-21 17:38:51 -05:00
|
|
|
Custom(String),
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:04:54 -05:00
|
|
|
impl Default for Formatter {
|
|
|
|
fn default() -> Formatter {
|
|
|
|
Formatter::Text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:38:51 -05:00
|
|
|
impl Formatter {
|
2021-06-24 00:07:52 -05:00
|
|
|
/// Prints the given entries to the specified output device.
|
|
|
|
///
|
|
|
|
/// the current time is given as the `now` argument and the offset from UTC
|
|
|
|
/// to the local timezone is given in `offset` to prevent this function from
|
|
|
|
/// using a secondary effect to retrieve the time and conver dates. This
|
|
|
|
/// also makes it easier to test.
|
2021-07-15 11:16:17 -05:00
|
|
|
pub fn print_formatted<W: Write>(&self, entries: Vec<Entry>, out: &mut W, now: DateTime<Utc>, ids: bool) -> error::Result<()> {
|
2021-06-21 17:38:51 -05:00
|
|
|
match &self {
|
2021-07-15 11:16:17 -05:00
|
|
|
Formatter::Text => text::print_formatted(entries, out, now, ids)?,
|
2021-06-30 18:51:34 -05:00
|
|
|
Formatter::Csv => csv::print_formatted(entries, out, ids)?,
|
2021-07-26 20:32:13 -05:00
|
|
|
Formatter::Json => json::print_formatted(entries, out)?,
|
2021-07-28 21:16:03 -05:00
|
|
|
Formatter::Ids => ids::print_formatted(entries, out)?,
|
2021-07-30 12:08:22 -05:00
|
|
|
Formatter::Ical => ical::print_formatted(entries, out, now)?,
|
2021-06-21 17:38:51 -05:00
|
|
|
Formatter::Custom(name) => {
|
2021-06-30 19:36:08 -05:00
|
|
|
panic!("attempted custom formatter with name {} which is not implemented", name);
|
2021-06-21 17:38:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Formatter {
|
|
|
|
type Err = error::Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> error::Result<Formatter> {
|
|
|
|
let lower = s.to_lowercase();
|
|
|
|
|
|
|
|
Ok(match &*lower {
|
|
|
|
"text" => Formatter::Text,
|
2021-06-30 19:36:08 -05:00
|
|
|
"csv" => Formatter::Csv,
|
2021-07-26 20:32:13 -05:00
|
|
|
"json" => Formatter::Json,
|
2021-07-28 21:16:03 -05:00
|
|
|
"ids" => Formatter::Ids,
|
2021-07-29 11:02:00 -05:00
|
|
|
"ical" => Formatter::Ical,
|
2021-06-21 17:38:51 -05:00
|
|
|
custom_format => Formatter::Custom(custom_format.into()),
|
|
|
|
})
|
|
|
|
}
|
2021-06-18 11:27:19 -05:00
|
|
|
}
|