implement format json
This commit is contained in:
parent
8cc1eb1cef
commit
39bc7abf0d
|
@ -90,6 +90,7 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"num-integer",
|
"num-integer",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"serde",
|
||||||
"time",
|
"time",
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
@ -487,6 +488,17 @@ dependencies = [
|
||||||
"syn",
|
"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]]
|
[[package]]
|
||||||
name = "serde_yaml"
|
name = "serde_yaml"
|
||||||
version = "0.8.17"
|
version = "0.8.17"
|
||||||
|
@ -597,6 +609,7 @@ dependencies = [
|
||||||
"regex",
|
"regex",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"serde_yaml",
|
"serde_yaml",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"textwrap 0.14.2",
|
"textwrap 0.14.2",
|
||||||
|
|
|
@ -12,7 +12,7 @@ path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = "2"
|
clap = "2"
|
||||||
chrono = "0.4"
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
rusqlite = { version = "0.25.3", features = ["chrono"] }
|
rusqlite = { version = "0.25.3", features = ["chrono"] }
|
||||||
thiserror = "*"
|
thiserror = "*"
|
||||||
directories = "*"
|
directories = "*"
|
||||||
|
@ -26,6 +26,7 @@ csv = "1.1"
|
||||||
regex = "1.5"
|
regex = "1.5"
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
serde_json = "1.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_assertions = "0.7.2"
|
pretty_assertions = "0.7.2"
|
||||||
|
|
|
@ -69,6 +69,11 @@ some human times, for now restricted to time ago:
|
||||||
#[error("CSV Error: {0}")]
|
#[error("CSV Error: {0}")]
|
||||||
CSVError(#[from] csv::Error),
|
CSVError(#[from] csv::Error),
|
||||||
|
|
||||||
|
#[error("Could not serialize to json.
|
||||||
|
|
||||||
|
{0}")]
|
||||||
|
SerdeJsonError(#[from] serde_json::Error),
|
||||||
|
|
||||||
#[error("Corrupted data found in the database:
|
#[error("Corrupted data found in the database:
|
||||||
{0}
|
{0}
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,14 @@ use crate::models::Entry;
|
||||||
|
|
||||||
pub mod text;
|
pub mod text;
|
||||||
pub mod csv;
|
pub mod csv;
|
||||||
|
pub mod json;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "lowercase")]
|
#[serde(rename_all = "lowercase")]
|
||||||
pub enum Formatter {
|
pub enum Formatter {
|
||||||
Text,
|
Text,
|
||||||
Csv,
|
Csv,
|
||||||
|
Json,
|
||||||
Custom(String),
|
Custom(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +37,7 @@ impl Formatter {
|
||||||
match &self {
|
match &self {
|
||||||
Formatter::Text => text::print_formatted(entries, out, now, ids)?,
|
Formatter::Text => text::print_formatted(entries, out, now, ids)?,
|
||||||
Formatter::Csv => csv::print_formatted(entries, out, ids)?,
|
Formatter::Csv => csv::print_formatted(entries, out, ids)?,
|
||||||
|
Formatter::Json => json::print_formatted(entries, out)?,
|
||||||
Formatter::Custom(name) => {
|
Formatter::Custom(name) => {
|
||||||
panic!("attempted custom formatter with name {} which is not implemented", name);
|
panic!("attempted custom formatter with name {} which is not implemented", name);
|
||||||
}
|
}
|
||||||
|
@ -53,6 +56,7 @@ impl FromStr for Formatter {
|
||||||
Ok(match &*lower {
|
Ok(match &*lower {
|
||||||
"text" => Formatter::Text,
|
"text" => Formatter::Text,
|
||||||
"csv" => Formatter::Csv,
|
"csv" => Formatter::Csv,
|
||||||
|
"json" => Formatter::Json,
|
||||||
custom_format => Formatter::Custom(custom_format.into()),
|
custom_format => Formatter::Custom(custom_format.into()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)?)
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub note: Option<String>,
|
pub note: Option<String>,
|
||||||
|
|
Loading…
Reference in New Issue