Use an enum for dialect (#80)

This commit is contained in:
Yuvi Panda 2021-05-09 16:09:39 +05:30 committed by GitHub
parent 19bd00f620
commit a04865d9d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View file

@ -19,9 +19,28 @@ pub enum SearchMode {
FullText,
}
// FIXME: Can use upstream Dialect enum if https://github.com/stevedonovan/chrono-english/pull/16 is merged
#[derive(Clone, Debug, Deserialize, Copy)]
pub enum Dialect {
#[serde(rename = "us")]
Us,
#[serde(rename = "uk")]
Uk,
}
impl From<Dialect> for chrono_english::Dialect {
fn from(d: Dialect) -> chrono_english::Dialect {
match d {
Dialect::Uk => chrono_english::Dialect::Uk,
Dialect::Us => chrono_english::Dialect::Us,
}
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct Settings {
pub dialect: String,
pub dialect: Dialect,
pub auto_sync: bool,
pub sync_address: String,
pub sync_frequency: String,

View file

@ -2,7 +2,7 @@ use std::collections::HashMap;
use chrono::prelude::*;
use chrono::Duration;
use chrono_english::{parse_date_string, Dialect};
use chrono_english::parse_date_string;
use cli_table::{format::Justify, print_stdout, Cell, Style, Table};
use eyre::{eyre, Result};
@ -84,10 +84,7 @@ impl Cmd {
words.join(" ")
};
let start = match settings.dialect.to_lowercase().as_str() {
"uk" => parse_date_string(&words, Local::now(), Dialect::Uk)?,
_ => parse_date_string(&words, Local::now(), Dialect::Us)?,
};
let start = parse_date_string(&words, Local::now(), settings.dialect.into())?;
let end = start + Duration::days(1);
let history = db.range(start.into(), end.into()).await?;