2022-08-18 11:37:52 +03:00
|
|
|
//! 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/>.
|
|
|
|
//!
|
2022-08-05 23:11:44 +03:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2022-08-16 22:24:21 +03:00
|
|
|
use std::path::PathBuf;
|
2022-08-05 23:11:44 +03:00
|
|
|
|
|
|
|
use crate::domain;
|
2022-08-05 23:40:26 +03:00
|
|
|
use crate::repo::{Error, InsertTaskData, Repository, UpdateTaskData};
|
2022-08-05 23:11:44 +03:00
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use xdg::BaseDirectories;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
|
|
|
pub struct Task {
|
|
|
|
name: String,
|
2022-08-16 16:36:53 +03:00
|
|
|
group: Option<String>,
|
2022-08-16 22:24:21 +03:00
|
|
|
link: Option<String>,
|
|
|
|
path: Option<String>,
|
2022-08-05 23:11:44 +03:00
|
|
|
// created_at
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Task> for domain::Task {
|
|
|
|
fn from(repo: Task) -> Self {
|
|
|
|
domain::Task {
|
|
|
|
name: repo.name,
|
2022-08-18 14:10:20 +03:00
|
|
|
project: repo.group,
|
2022-08-16 22:24:21 +03:00
|
|
|
link: repo.link,
|
2022-08-18 14:10:20 +03:00
|
|
|
dir_path: repo.path.map(PathBuf::from),
|
2022-08-20 15:42:20 +03:00
|
|
|
created_at: time::OffsetDateTime::now_utc(),
|
2022-08-05 23:11:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:43:44 +03:00
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
|
|
|
pub struct CurrentTaskInfo {
|
|
|
|
task_idx: usize,
|
|
|
|
task: Task,
|
|
|
|
// started_at
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<CurrentTaskInfo> for domain::CurrentTaskInfo {
|
|
|
|
fn from(repo: CurrentTaskInfo) -> Self {
|
|
|
|
domain::CurrentTaskInfo {
|
|
|
|
task_idx: repo.task_idx,
|
|
|
|
task: repo.task.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CURRENT_TASK_FILE: &str = "current.json";
|
2022-08-05 23:11:44 +03:00
|
|
|
const DATA_FILE: &str = "data.json";
|
2022-08-16 15:43:44 +03:00
|
|
|
const FINISHED_DATA_FILE: &str = "finished_data.json";
|
2022-08-05 23:11:44 +03:00
|
|
|
|
|
|
|
pub struct FsRepo {
|
|
|
|
xdg_dirs: BaseDirectories,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FsRepo {
|
|
|
|
pub fn new(xdg_dirs: BaseDirectories) -> Self {
|
|
|
|
Self { xdg_dirs }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Repository for FsRepo {
|
2022-08-16 15:43:44 +03:00
|
|
|
fn get_current_task_opt(&self) -> Result<Option<domain::CurrentTaskInfo>, Error> {
|
|
|
|
self.get_current_task_impl()
|
|
|
|
.map(|cur_task| cur_task.map(From::from))
|
|
|
|
}
|
|
|
|
|
2022-08-20 15:42:20 +03:00
|
|
|
fn get_task_opt(&self, id: domain::TaskIdx) -> Result<Option<domain::Task>, Error> {
|
2022-08-16 15:43:44 +03:00
|
|
|
let tasks = self.get_tasks_impl()?;
|
|
|
|
if id == 0 || id > tasks.len() {
|
|
|
|
return Err(Error::NotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(tasks[id - 1].clone().into()))
|
|
|
|
}
|
|
|
|
|
2022-08-05 23:11:44 +03:00
|
|
|
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error> {
|
|
|
|
self.get_tasks_impl()
|
|
|
|
.map(|tasks| tasks.into_iter().map(Task::into).collect())
|
|
|
|
}
|
|
|
|
|
2022-08-20 15:42:20 +03:00
|
|
|
fn remove_task(&self, id: domain::TaskIdx) -> Result<domain::Task, Error> {
|
2022-08-16 15:43:44 +03:00
|
|
|
let mut tasks = self.get_tasks_impl()?;
|
|
|
|
if id == 0 || id > tasks.len() {
|
|
|
|
return Err(Error::NotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
let removed = tasks.remove(id - 1);
|
|
|
|
self.save_tasks_impl(tasks)?;
|
|
|
|
|
|
|
|
Ok(removed.into())
|
|
|
|
}
|
|
|
|
|
2022-08-05 23:40:26 +03:00
|
|
|
fn update_task(
|
|
|
|
&self,
|
2022-08-20 15:42:20 +03:00
|
|
|
id: domain::TaskIdx,
|
2022-08-05 23:40:26 +03:00
|
|
|
update_data: UpdateTaskData,
|
|
|
|
) -> Result<domain::Task, Error> {
|
|
|
|
let mut tasks = self.get_tasks_impl()?;
|
|
|
|
if id == 0 || id > tasks.len() {
|
|
|
|
return Err(Error::NotFound);
|
|
|
|
}
|
|
|
|
|
2022-08-16 16:16:41 +03:00
|
|
|
let mut task = &mut tasks[id - 1];
|
2022-08-05 23:40:26 +03:00
|
|
|
if let Some(name) = update_data.name {
|
|
|
|
task.name = name;
|
|
|
|
}
|
|
|
|
|
2022-08-18 14:10:20 +03:00
|
|
|
if let Some(group) = update_data.project {
|
2022-08-16 22:24:21 +03:00
|
|
|
task.group = group;
|
|
|
|
}
|
|
|
|
|
2022-08-05 23:40:26 +03:00
|
|
|
if let Some(link) = update_data.link {
|
|
|
|
task.link = link;
|
|
|
|
}
|
|
|
|
|
2022-08-18 14:10:20 +03:00
|
|
|
if let Some(path) = update_data.dir_path {
|
2022-08-16 22:24:21 +03:00
|
|
|
task.path = path.and_then(|p| p.into_os_string().into_string().ok());
|
2022-08-16 16:47:51 +03:00
|
|
|
}
|
|
|
|
|
2022-08-05 23:40:26 +03:00
|
|
|
let new_task = task.clone();
|
|
|
|
|
|
|
|
self.save_tasks_impl(tasks)?;
|
|
|
|
|
|
|
|
Ok(new_task.into())
|
|
|
|
}
|
|
|
|
|
2022-08-05 23:11:44 +03:00
|
|
|
fn insert_task(&self, insert_data: InsertTaskData) -> Result<domain::Task, Error> {
|
|
|
|
let new_task = Task {
|
|
|
|
name: insert_data.name,
|
2022-08-18 14:10:20 +03:00
|
|
|
group: insert_data.project,
|
2022-08-16 22:24:21 +03:00
|
|
|
link: insert_data.link,
|
|
|
|
path: insert_data
|
2022-08-18 14:10:20 +03:00
|
|
|
.dir_path
|
2022-08-16 22:24:21 +03:00
|
|
|
.and_then(|p| p.into_os_string().into_string().ok()),
|
2022-08-05 23:11:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut tasks = self.get_tasks_impl()?;
|
2022-08-16 15:43:44 +03:00
|
|
|
|
|
|
|
match insert_data.index {
|
|
|
|
None => tasks.push(new_task.clone()),
|
|
|
|
Some(idx) => tasks.insert(idx, new_task.clone()),
|
|
|
|
}
|
2022-08-05 23:11:44 +03:00
|
|
|
|
2022-08-05 23:40:26 +03:00
|
|
|
self.save_tasks_impl(tasks)?;
|
2022-08-05 23:11:44 +03:00
|
|
|
|
|
|
|
Ok(new_task.into())
|
|
|
|
}
|
2022-08-16 15:43:44 +03:00
|
|
|
|
2022-08-20 15:42:20 +03:00
|
|
|
fn start_task(&self, id: domain::TaskIdx) -> Result<domain::Task, Error> {
|
2022-08-16 15:43:44 +03:00
|
|
|
let tasks = self.get_tasks_impl()?;
|
|
|
|
if id == 0 || id > tasks.len() {
|
|
|
|
return Err(Error::NotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
let task = &tasks[id - 1];
|
|
|
|
let mut cur_task = self.get_current_task_impl()?;
|
|
|
|
|
|
|
|
cur_task.replace(CurrentTaskInfo {
|
|
|
|
task_idx: id,
|
|
|
|
task: task.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
self.save_current_task_impl(cur_task)?;
|
|
|
|
Ok(task.clone().into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop_task(&self) -> Result<domain::Task, Error> {
|
|
|
|
let mut cur_task = self.get_current_task_impl()?;
|
|
|
|
let old = cur_task.take().ok_or(Error::NotFound)?;
|
|
|
|
self.save_current_task_impl(cur_task)?;
|
|
|
|
Ok(old.task.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish_task(&self) -> Result<domain::Task, Error> {
|
|
|
|
let mut cur_task = self.get_current_task_impl()?;
|
|
|
|
let old = cur_task.take().ok_or(Error::NotFound)?;
|
|
|
|
|
|
|
|
let mut finished_tasks = self.get_finished_tasks_impl()?;
|
|
|
|
let mut tasks = self.get_tasks_impl()?;
|
|
|
|
|
|
|
|
let task = tasks.remove(old.task_idx - 1);
|
|
|
|
finished_tasks.push(task.clone());
|
|
|
|
|
|
|
|
self.save_current_task_impl(cur_task)?;
|
|
|
|
self.save_tasks_impl(tasks)?;
|
|
|
|
self.save_finished_tasks_impl(finished_tasks)?;
|
|
|
|
|
|
|
|
Ok(task.into())
|
|
|
|
}
|
2022-08-05 23:11:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FsRepo {
|
2022-08-16 15:43:44 +03:00
|
|
|
fn get_current_task_impl(&self) -> Result<Option<CurrentTaskInfo>, Error> {
|
|
|
|
let file_path = self.xdg_dirs.get_data_file(CURRENT_TASK_FILE);
|
2022-08-16 16:06:25 +03:00
|
|
|
Ok(File::open(&file_path)
|
2022-08-16 15:43:44 +03:00
|
|
|
.ok()
|
2022-08-16 16:06:25 +03:00
|
|
|
.and_then(|file| serde_json::from_reader(file).ok()))
|
2022-08-16 15:43:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn save_current_task_impl(&self, cur_task: Option<CurrentTaskInfo>) -> Result<(), Error> {
|
|
|
|
let file_path = self
|
|
|
|
.xdg_dirs
|
|
|
|
.place_data_file(CURRENT_TASK_FILE)
|
|
|
|
.map_err(|_| Error::InsertData)?;
|
|
|
|
let mut file = File::create(&file_path).map_err(|_| Error::InsertData)?;
|
|
|
|
let new_data = serde_json::to_vec(&cur_task).map_err(|_| Error::InvalidData)?;
|
|
|
|
file.write_all(&new_data).map_err(|_| Error::InsertData)
|
|
|
|
}
|
|
|
|
|
2022-08-05 23:11:44 +03:00
|
|
|
fn get_tasks_impl(&self) -> Result<Vec<Task>, Error> {
|
|
|
|
let file_path = self.xdg_dirs.get_data_file(DATA_FILE);
|
|
|
|
File::open(&file_path)
|
2022-08-16 15:43:44 +03:00
|
|
|
.ok()
|
|
|
|
.map(|file| serde_json::from_reader(file).map_err(|_| Error::InvalidData))
|
|
|
|
.transpose()
|
|
|
|
.map(|tasks| tasks.unwrap_or_default())
|
2022-08-05 23:11:44 +03:00
|
|
|
}
|
2022-08-05 23:40:26 +03:00
|
|
|
|
|
|
|
fn save_tasks_impl(&self, tasks: Vec<Task>) -> Result<(), Error> {
|
|
|
|
let file_path = self
|
|
|
|
.xdg_dirs
|
|
|
|
.place_data_file(DATA_FILE)
|
|
|
|
.map_err(|_| Error::InsertData)?;
|
|
|
|
let mut file = File::create(&file_path).map_err(|_| Error::InsertData)?;
|
|
|
|
let new_data = serde_json::to_vec(&tasks).map_err(|_| Error::InvalidData)?;
|
|
|
|
file.write_all(&new_data).map_err(|_| Error::InsertData)
|
|
|
|
}
|
2022-08-16 15:43:44 +03:00
|
|
|
|
|
|
|
fn get_finished_tasks_impl(&self) -> Result<Vec<Task>, Error> {
|
|
|
|
let file_path = self.xdg_dirs.get_data_file(FINISHED_DATA_FILE);
|
|
|
|
File::open(&file_path)
|
|
|
|
.ok()
|
|
|
|
.map(|file| serde_json::from_reader(file).map_err(|_| Error::InvalidData))
|
|
|
|
.transpose()
|
|
|
|
.map(|tasks| tasks.unwrap_or_default())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_finished_tasks_impl(&self, tasks: Vec<Task>) -> Result<(), Error> {
|
|
|
|
let file_path = self
|
|
|
|
.xdg_dirs
|
|
|
|
.place_data_file(FINISHED_DATA_FILE)
|
|
|
|
.map_err(|_| Error::InsertData)?;
|
|
|
|
let mut file = File::create(&file_path).map_err(|_| Error::InsertData)?;
|
|
|
|
let new_data = serde_json::to_vec(&tasks).map_err(|_| Error::InvalidData)?;
|
|
|
|
file.write_all(&new_data).map_err(|_| Error::InsertData)
|
|
|
|
}
|
2022-08-05 23:11:44 +03:00
|
|
|
}
|