implement format json

This commit is contained in:
Abraham Toriz 2021-07-26 20:32:13 -05:00
parent 8cc1eb1cef
commit 39bc7abf0d
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
6 changed files with 34 additions and 2 deletions

13
Cargo.lock generated
View File

@ -90,6 +90,7 @@ dependencies = [
"libc",
"num-integer",
"num-traits",
"serde",
"time",
"winapi",
]
@ -487,6 +488,17 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.8.17"
@ -597,6 +609,7 @@ dependencies = [
"regex",
"rusqlite",
"serde",
"serde_json",
"serde_yaml",
"tempfile",
"textwrap 0.14.2",

View File

@ -12,7 +12,7 @@ path = "src/main.rs"
[dependencies]
clap = "2"
chrono = "0.4"
chrono = { version = "0.4", features = ["serde"] }
rusqlite = { version = "0.25.3", features = ["chrono"] }
thiserror = "*"
directories = "*"
@ -26,6 +26,7 @@ csv = "1.1"
regex = "1.5"
lazy_static = "1.4"
tempfile = "3"
serde_json = "1.0"
[dev-dependencies]
pretty_assertions = "0.7.2"

View File

@ -69,6 +69,11 @@ some human times, for now restricted to time ago:
#[error("CSV Error: {0}")]
CSVError(#[from] csv::Error),
#[error("Could not serialize to json.
{0}")]
SerdeJsonError(#[from] serde_json::Error),
#[error("Corrupted data found in the database:
{0}

View File

@ -9,12 +9,14 @@ use crate::models::Entry;
pub mod text;
pub mod csv;
pub mod json;
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Formatter {
Text,
Csv,
Json,
Custom(String),
}
@ -35,6 +37,7 @@ impl Formatter {
match &self {
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::Custom(name) => {
panic!("attempted custom formatter with name {} which is not implemented", name);
}
@ -53,6 +56,7 @@ impl FromStr for Formatter {
Ok(match &*lower {
"text" => Formatter::Text,
"csv" => Formatter::Csv,
"json" => Formatter::Json,
custom_format => Formatter::Custom(custom_format.into()),
})
}

8
src/formatters/json.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(serde_json::to_writer(out, &entries)?)
}

View File

@ -1,6 +1,7 @@
use chrono::{DateTime, Utc};
use serde::Serialize;
#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct Entry {
pub id: u64,
pub note: Option<String>,