Skip to content

Commit

Permalink
refactor: get rid of custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Jan 6, 2024
1 parent 7480a89 commit 512c1f3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 34 deletions.
17 changes: 8 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: MIT

use crate::utils::ChronoDate;
use anyhow::{anyhow, Result};
use chrono::prelude::*;
use clap::{crate_authors, crate_name, crate_version, Parser};
Expand Down Expand Up @@ -57,8 +56,8 @@ impl Default for Cli {
}

impl Cli {
pub fn validate_date(&self) -> Result<ChronoDate> {
let mut today: ChronoDate = Local::now().date_naive();
pub fn validate_date(&self) -> Result<chrono::NaiveDate> {
let mut today: chrono::NaiveDate = Local::now().date_naive();
let mut year: i32 = today.year();
let mut month: u32 = today.month();
let mut day: u32 = today.day();
Expand Down Expand Up @@ -147,13 +146,13 @@ mod tests {

#[test]
fn test_validate_date_defaults_to_now() {
let today: ChronoDate = Local::now().date_naive();
let today: chrono::NaiveDate = Local::now().date_naive();
let o: Cli = Cli::default();
assert_eq!(today, o.validate_date().unwrap());
}
#[test]
fn test_validate_date_default_to_now_with_custom_year() {
let today: ChronoDate = Local::now().date_naive().with_year(2007).unwrap();
let today: chrono::NaiveDate = Local::now().date_naive().with_year(2007).unwrap();
let o: Cli = Cli {
date: vec![String::from("2007")],
..Default::default()
Expand All @@ -162,7 +161,7 @@ mod tests {
}
#[test]
fn test_validate_date_defaults_to_now_with_custom_year_and_month() {
let today: ChronoDate = Local::now()
let today: chrono::NaiveDate = Local::now()
.date_naive()
.with_year(2007)
.unwrap()
Expand All @@ -176,7 +175,7 @@ mod tests {
}
#[test]
fn test_validate_date_defaults_to_now_with_custom_year_and_month_and_day() {
let today: ChronoDate = Local::now()
let today: chrono::NaiveDate = Local::now()
.date_naive()
.with_year(2007)
.unwrap()
Expand All @@ -192,7 +191,7 @@ mod tests {
}
#[test]
fn test_validate_date_defaults_to_now_with_ambiguous_arguments() {
let today: ChronoDate = Local::now().date_naive();
let today: chrono::NaiveDate = Local::now().date_naive();
let o: Cli = Cli {
date: vec![
String::from("2007"),
Expand Down Expand Up @@ -266,7 +265,7 @@ mod tests {
}
#[test]
fn test_validate_date_errors_with_non_existent_date() {
let today: ChronoDate = Local::now().date_naive();
let today: chrono::NaiveDate = Local::now().date_naive();
let o: Cli = Cli {
date: vec![String::from("2007"), String::from("2"), String::from("30")],
..Default::default()
Expand Down
5 changes: 2 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ use crate::cli::Cli;
use crate::config::{Config, Theme};
use crate::config::{Style, StyleType};
use crate::events::Event;
use crate::utils::ChronoDate;
use anyhow::Result;
use chrono::prelude::*;
use clap::Parser;

// A struct storing the combined settings of config file, theme, options, ...
pub struct Context {
pub usersetdate: ChronoDate,
pub usersetdate: chrono::NaiveDate,
pub opts: Cli,
pub config: Config,
pub eventstuple: Vec<(Event, Style)>,
Expand All @@ -37,7 +36,7 @@ impl Context {
StyleType::Light
};

let usersetdate: ChronoDate = match opts.validate_date() {
let usersetdate: chrono::NaiveDate = match opts.validate_date() {
Ok(x) => x,
Err(x) => return Err(x),
};
Expand Down
11 changes: 7 additions & 4 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
mod ics;
pub use ics::ReadFromIcsFile;

use crate::utils::ChronoDate;
use chrono::prelude::*;
use rrule::{RRuleSet, Tz};
use std::fmt;
Expand All @@ -16,7 +15,7 @@ pub enum EventDateTime {
date_time: chrono::NaiveDateTime,
offset: Option<chrono::offset::FixedOffset>,
},
Date(ChronoDate),
Date(chrono::NaiveDate),
}

impl EventDateTime {
Expand Down Expand Up @@ -50,11 +49,15 @@ impl Default for Event {
}

impl Event {
pub fn is_day(&self, date: &ChronoDate) -> bool {
pub fn is_day(&self, date: &chrono::NaiveDate) -> bool {
self.in_range(*date, *date)
}

pub fn in_range(&self, daterangebegin: ChronoDate, daterangeend: ChronoDate) -> bool {
pub fn in_range(
&self,
daterangebegin: chrono::NaiveDate,
daterangeend: chrono::NaiveDate,
) -> bool {
let timezone: Tz = Local::now().timezone().into();
let before = timezone
.with_ymd_and_hms(
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use events::{Events, ReadFromIcsFile};
use output::agenda::Agenda;
use output::calendar::Calendar;
use output::date::Date;
use utils::{ChronoDate, DateExtensions};
use utils::DateExtensions;

#[cfg(not(tarpaulin_include))]
fn main() {
let mut columns = 1;
let mut months: Vec<ChronoDate> = vec![];
let mut months: Vec<chrono::NaiveDate> = vec![];

let mut ctx: Context;
match Context::new() {
Expand All @@ -37,8 +37,8 @@ fn main() {
}
}

let mut daterangebegin: ChronoDate = ctx.usersetdate.first_day_of_month();
let mut daterangeend: ChronoDate = ctx.usersetdate.last_day_of_month();
let mut daterangebegin: chrono::NaiveDate = ctx.usersetdate.first_day_of_month();
let mut daterangeend: chrono::NaiveDate = ctx.usersetdate.last_day_of_month();

if ctx.opts.three {
daterangebegin = (ctx.usersetdate - Duration::weeks(4)).first_day_of_month();
Expand Down
8 changes: 4 additions & 4 deletions src/output/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//
// SPDX-License-Identifier: MIT

use crate::utils::{ChronoDate, DateExtensions, MonthFullWeeksIter};
use crate::utils::{DateExtensions, MonthFullWeeksIter};
use crate::Context;
use crate::Date;
use chrono::{Duration, NaiveDate};

use std::fmt;

pub struct Calendar<'a> {
pub dates: Vec<ChronoDate>,
pub dates: Vec<chrono::NaiveDate>,
pub columns: usize,
pub ctx: &'a Context,
}
Expand Down Expand Up @@ -93,7 +93,7 @@ impl fmt::Display for Calendar<'_> {

impl Calendar<'_> {
fn weekdays(&self) -> String {
let mut week: Vec<ChronoDate> = vec![];
let mut week: Vec<chrono::NaiveDate> = vec![];
let (s, e) = if self.ctx.opts.sunday {
(3, 9)
} else {
Expand Down Expand Up @@ -172,7 +172,7 @@ mod tests {
};
ctx.opts.year = true;

let mut dates: Vec<ChronoDate> = vec![];
let mut dates: Vec<chrono::NaiveDate> = vec![];
let daterangebegin = ctx.usersetdate.first_day_of_year();
let daterangeend = ctx.usersetdate.last_day_of_year();
let mut tmpdate = daterangebegin;
Expand Down
6 changes: 3 additions & 3 deletions src/output/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
// SPDX-License-Identifier: MIT

use crate::config::{DateProperty, Style, StyleType};
use crate::utils::{convertstyle, ChronoDate, DateExtensions};
use crate::utils::{convertstyle, DateExtensions};
use crate::Context;
use chrono::Datelike;

use std::fmt;

pub struct Date<'a> {
pub date: ChronoDate,
pub date: chrono::NaiveDate,
pub ctx: &'a Context,
pub firstdayofdisplayedmonth: ChronoDate,
pub firstdayofdisplayedmonth: chrono::NaiveDate,
}

impl Date<'_> {
Expand Down
2 changes: 0 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

mod date_extensions;
mod helpers;
mod types;

pub use date_extensions::{DateExtensions, DateRange, MonthFullWeeksIter};
pub use helpers::convertstyle;
pub use types::ChronoDate;
5 changes: 0 additions & 5 deletions src/utils/types.rs

This file was deleted.

0 comments on commit 512c1f3

Please sign in to comment.