tas/src/repo.rs
Dmitriy Pleshevskiy eb78dad91a
Replace FS with Sqlite (#20)
- [x] Deps: add rusqlite
- [x] Deps: add time
- [x] Add created_at to the domain Task
- [x] Add schema and first migration
- [x] Check current version and run migration
- [x] Implement Repository trait
- [x] Add script to migrate from FS implementation

Closes #5
Closes #19

Reviewed-on: #20
Co-authored-by: Dmitriy Pleshevskiy <dmitriy@ideascup.me>
Co-committed-by: Dmitriy Pleshevskiy <dmitriy@ideascup.me>
2022-08-20 12:42:20 +00:00

89 lines
2.7 KiB
Rust
Executable file

//! Copyright (C) 2022, Dmitriy Pleshevskiy <dmitriy@ideascup.me>
//!
//! tas is free software: you can redistribute it and/or modify
//! it under the terms of the GNU General Public License as published by
//! the Free Software Foundation, either version 3 of the License, or
//! (at your option) any later version.
//!
//! tas is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with tas. If not, see <https://www.gnu.org/licenses/>.
//!
pub mod fs;
pub mod sqlite;
use std::path::PathBuf;
use crate::domain;
#[derive(Debug)]
pub enum Error {
Connect,
PrepareQuery,
QueryData,
NotFound,
InvalidData,
InsertData,
UpdateData,
RemoveData,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Connect => f.write_str("Cannot connect to the repository"),
Error::PrepareQuery => f.write_str("Cannot prepare query"),
Error::QueryData => f.write_str("Cannot query data"),
Error::NotFound => f.write_str("Cannot find data"),
Error::InvalidData => f.write_str("Invalid data format"),
Error::InsertData => f.write_str("Cannot insert data"),
Error::UpdateData => f.write_str("Cannot update data"),
Error::RemoveData => f.write_str("Cannot remove data"),
}
}
}
impl std::error::Error for Error {}
pub struct InsertTaskData {
pub name: String,
pub project: Option<String>,
pub link: Option<String>,
pub dir_path: Option<PathBuf>,
pub index: Option<usize>,
}
pub struct UpdateTaskData {
pub name: Option<String>,
pub project: Option<Option<String>>,
pub link: Option<Option<String>>,
pub dir_path: Option<Option<PathBuf>>,
}
pub trait Repository {
fn get_current_task_opt(&self) -> Result<Option<domain::CurrentTaskInfo>, Error>;
fn get_task_opt(&self, id: domain::TaskIdx) -> Result<Option<domain::Task>, Error>;
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error>;
fn remove_task(&self, id: domain::TaskIdx) -> Result<domain::Task, Error>;
fn update_task(
&self,
id: domain::TaskIdx,
update_data: UpdateTaskData,
) -> Result<domain::Task, Error>;
fn insert_task(&self, insert_data: InsertTaskData) -> Result<domain::Task, Error>;
fn start_task(&self, id: domain::TaskIdx) -> Result<domain::Task, Error>;
fn stop_task(&self) -> Result<domain::Task, Error>;
fn finish_task(&self) -> Result<domain::Task, Error>;
}