Skip to content

Commit

Permalink
Implemented add and hide_category in SQLite datafile
Browse files Browse the repository at this point in the history
  • Loading branch information
mfep committed Dec 12, 2023
1 parent 5980d7c commit 0312481
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
test.db*
35 changes: 35 additions & 0 deletions src/datafile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,36 @@ pub enum SuccessfulUpdate {
ReplacedExisting,
}

/// Result from the call to `add_category`
#[derive(Debug, PartialEq)]
pub enum AddCategoryResult {
/// Created a new category
AddedNew,

/// Made a previously hidden category visible again
Unhide,

/// The category is already present and is visible
AlreadyPresent,
}

/// Result from the call to `hide_category`
#[derive(Debug, PartialEq)]
pub enum HideCategoryResult {
/// The specified category was visible previously and was hidden
Hidden,

/// The specified category is already hidden, nothing was changed
AlreadyHidden,

/// The specified category does not exist
NonExistingCategory,
}

/// Represents a connection to the diary database.
pub trait DiaryDataConnection {
fn into_any(self: Box<Self>) -> Box<dyn std::any::Any>;

/// Calculates the occurences of all habits over multiple periods of date ranges.
fn calculate_data_counts_per_iter(
&self,
Expand Down Expand Up @@ -47,7 +75,14 @@ pub trait DiaryDataConnection {
/// Returns if the database contains any records.
fn is_empty(&self) -> Result<bool>;

/// Returns the earliest and latest date among the database records.
fn get_date_range(&self) -> Result<(NaiveDate, NaiveDate)>;

/// Adds or unhides the specified category in the database.
fn add_category(&self, name: &str) -> Result<AddCategoryResult>;

/// Hides the specified category in the database.
fn hide_category(&self, name: &str) -> Result<HideCategoryResult>;
}

/// Tries to read data file to memory.
Expand Down
12 changes: 12 additions & 0 deletions src/datafile/csv_datafile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ fn calculate_data_counts(data: &DiaryDataCsv, from: &NaiveDate, to: &NaiveDate)
}

impl DiaryDataConnection for DiaryDataCsv {
fn into_any(self: Box<Self>) -> Box<dyn std::any::Any> {
self
}

fn calculate_data_counts_per_iter(
&self,
date_ranges: &[(NaiveDate, NaiveDate)],
Expand Down Expand Up @@ -157,6 +161,14 @@ impl DiaryDataConnection for DiaryDataCsv {
*self.data.last_key_value().unwrap().0,
))
}

fn add_category(&self, _name: &str) -> Result<super::AddCategoryResult> {
todo!();
}

fn hide_category(&self, _name: &str) -> Result<super::HideCategoryResult> {
todo!();
}
}

impl Drop for DiaryDataCsv {
Expand Down
Loading

0 comments on commit 0312481

Please sign in to comment.