122 lines
3.4 KiB
Rust
122 lines
3.4 KiB
Rust
use std::str::FromStr;
|
|
use std::io::Write;
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use crate::error;
|
|
use crate::models::Entry;
|
|
use crate::commands::Facts;
|
|
|
|
use strum_macros::{EnumVariantNames};
|
|
use strum::VariantNames;
|
|
|
|
pub mod text;
|
|
pub mod csv;
|
|
pub mod json;
|
|
pub mod ids;
|
|
pub mod ical;
|
|
pub mod custom;
|
|
pub mod chart;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, EnumVariantNames)]
|
|
#[serde(rename_all = "lowercase")]
|
|
#[strum(serialize_all = "kebab_case")]
|
|
pub enum Formatter {
|
|
Text,
|
|
Csv,
|
|
Json,
|
|
Ids,
|
|
Ical,
|
|
Chart,
|
|
Custom(String),
|
|
}
|
|
|
|
impl Default for Formatter {
|
|
fn default() -> Formatter {
|
|
Formatter::Text
|
|
}
|
|
}
|
|
|
|
impl Formatter {
|
|
/// 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.
|
|
pub fn print_formatted<O, E>(&self, entries: Vec<Entry>, out: &mut O, err: &mut E, facts: &Facts, ids: bool) -> error::Result<()>
|
|
where
|
|
O: Write,
|
|
E: Write,
|
|
{
|
|
match &self {
|
|
Formatter::Text => text::print_formatted(entries, out, facts, ids)?,
|
|
Formatter::Csv => csv::print_formatted(entries, out, ids)?,
|
|
Formatter::Json => json::print_formatted(entries, out)?,
|
|
Formatter::Ids => ids::print_formatted(entries, out)?,
|
|
Formatter::Ical => ical::print_formatted(entries, out, facts.now)?,
|
|
Formatter::Chart => chart::print_formatted(entries, out, facts)?,
|
|
Formatter::Custom(name) => custom::print_formatted(name, entries, out, err, facts)?,
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Gets Formatter from prefix
|
|
///
|
|
/// from Formatter variants try to match with starts_with.
|
|
/// Returns error if it doesn't match.
|
|
pub fn autocomplete(s: &str) -> error::Result<Formatter> {
|
|
if s.len() > 0 {
|
|
for formatter in Formatter::VARIANTS {
|
|
if formatter.starts_with(s) {
|
|
return Formatter::from_str(formatter)
|
|
}
|
|
}
|
|
}
|
|
Err(error::Error::InvalidAutocompleteFormatter(s.to_string()))
|
|
}
|
|
}
|
|
|
|
impl FromStr for Formatter {
|
|
type Err = error::Error;
|
|
|
|
fn from_str(s: &str) -> error::Result<Formatter> {
|
|
Ok(match s.to_lowercase().as_str() {
|
|
"text" => Formatter::Text,
|
|
"csv" => Formatter::Csv,
|
|
"json" => Formatter::Json,
|
|
"ids" => Formatter::Ids,
|
|
"ical" => Formatter::Ical,
|
|
"chart" => Formatter::Chart,
|
|
custom_format => Formatter::Custom(custom_format.into()),
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn valid_autocomplete() {
|
|
let format = Formatter::autocomplete("t");
|
|
assert_eq!(format.unwrap(), Formatter::Text);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_autocomplete() {
|
|
let format = Formatter::autocomplete("fail");
|
|
assert!(matches!(format, Err(error::Error::InvalidAutocompleteFormatter(_))));
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_autocomplete_empty() {
|
|
let format = Formatter::autocomplete("");
|
|
assert!(matches!(format, Err(error::Error::InvalidAutocompleteFormatter(_))));
|
|
}
|
|
|
|
// TODO: autocomplete for custom formatter
|
|
}
|