repo: add repository trait and base fs impl #13
5 changed files with 134 additions and 32 deletions
|
@ -1,6 +1,4 @@
|
||||||
use std::path::PathBuf;
|
use crate::repo::{self, Repository};
|
||||||
|
|
||||||
use crate::Task;
|
|
||||||
|
|
||||||
#[derive(clap::Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
|
@ -10,18 +8,24 @@ pub struct Args {
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Request {
|
pub fn execute(repo: impl Repository, args: Args) {
|
||||||
pub args: Args,
|
let res = repo.insert_task(repo::InsertTaskData {
|
||||||
pub tasks: Vec<Task>,
|
name: args.name,
|
||||||
pub tasks_file_path: PathBuf,
|
link: args.link,
|
||||||
}
|
});
|
||||||
|
|
||||||
pub fn execute(mut req: Request) {
|
match res {
|
||||||
tasks.push(Task { name, link });
|
Ok(task) => {
|
||||||
|
println!("Task was added successfully");
|
||||||
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
|
println!(" {}", task.name);
|
||||||
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
if let Some(link) = task.link {
|
||||||
.unwrap();
|
println!(" link: {}", link);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Cannot insert data: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
println!("added");
|
println!("added");
|
||||||
}
|
}
|
||||||
|
|
5
src/domain.rs
Normal file
5
src/domain.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
pub struct Task {
|
||||||
|
pub name: String,
|
||||||
|
pub link: Option<String>,
|
||||||
|
// created_at
|
||||||
|
}
|
24
src/main.rs
24
src/main.rs
|
@ -27,23 +27,22 @@
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use repo::fs::FsRepo;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::io::Write;
|
|
||||||
use xdg::BaseDirectories;
|
use xdg::BaseDirectories;
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
|
pub mod domain;
|
||||||
|
pub mod repo;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = cli::Args::parse();
|
let args = cli::Args::parse();
|
||||||
|
|
||||||
let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap();
|
let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap();
|
||||||
|
|
||||||
let finished_tasks_file_path = xdg_dirs.place_data_file("finished_data.json").unwrap();
|
let repo = FsRepo::new(xdg_dirs);
|
||||||
|
|
||||||
let tasks_file_path = xdg_dirs.place_data_file("data.json").unwrap();
|
let finished_tasks_file_path = xdg_dirs.place_data_file("finished_data.json").unwrap();
|
||||||
let mut tasks: Vec<Task> = std::fs::File::open(&tasks_file_path)
|
|
||||||
.map(|file| serde_json::from_reader(file).unwrap())
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
let current_task_info_file_path = xdg_dirs.place_data_file("current.json").unwrap();
|
let current_task_info_file_path = xdg_dirs.place_data_file("current.json").unwrap();
|
||||||
let current_task_info: Option<CurrentTaskInfo> =
|
let current_task_info: Option<CurrentTaskInfo> =
|
||||||
|
@ -53,11 +52,7 @@ fn main() {
|
||||||
|
|
||||||
match args.command {
|
match args.command {
|
||||||
cli::SubCommand::Add(args) => {
|
cli::SubCommand::Add(args) => {
|
||||||
cli::add::execute(cli::add::Request {
|
cli::add::execute(repo, args);
|
||||||
args,
|
|
||||||
tasks,
|
|
||||||
tasks_file_path,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
cli::SubCommand::Edit(args) => {
|
cli::SubCommand::Edit(args) => {
|
||||||
cli::edit::execute(cli::edit::Request {
|
cli::edit::execute(cli::edit::Request {
|
||||||
|
@ -118,13 +113,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone)]
|
|
||||||
pub struct Task {
|
|
||||||
name: String,
|
|
||||||
link: Option<String>,
|
|
||||||
// created_at
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct CurrentTaskInfo {
|
pub struct CurrentTaskInfo {
|
||||||
task_idx: usize,
|
task_idx: usize,
|
||||||
|
|
33
src/repo.rs
Executable file
33
src/repo.rs
Executable file
|
@ -0,0 +1,33 @@
|
||||||
|
pub mod fs;
|
||||||
|
|
||||||
|
use crate::domain;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
NotFound,
|
||||||
|
InvalidData,
|
||||||
|
InsertData,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::NotFound => f.write_str("Cannot find data"),
|
||||||
|
Error::InvalidData => f.write_str("Invalid data format"),
|
||||||
|
Error::InsertData => f.write_str("Cannot insert data"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for Error {}
|
||||||
|
|
||||||
|
pub struct InsertTaskData {
|
||||||
|
name: String,
|
||||||
|
link: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Repository {
|
||||||
|
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error>;
|
||||||
|
|
||||||
|
fn insert_task(&self, insert_data: InsertTaskData) -> Result<domain::Task, Error>;
|
||||||
|
}
|
72
src/repo/fs.rs
Normal file
72
src/repo/fs.rs
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use crate::domain;
|
||||||
|
use crate::repo::{Error, InsertTaskData, Repository};
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use xdg::BaseDirectories;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Clone)]
|
||||||
|
pub struct Task {
|
||||||
|
name: String,
|
||||||
|
link: Option<String>,
|
||||||
|
// created_at
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Task> for domain::Task {
|
||||||
|
fn from(repo: Task) -> Self {
|
||||||
|
domain::Task {
|
||||||
|
name: repo.name,
|
||||||
|
link: repo.link,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const DATA_FILE: &str = "data.json";
|
||||||
|
|
||||||
|
pub struct FsRepo {
|
||||||
|
xdg_dirs: BaseDirectories,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FsRepo {
|
||||||
|
pub fn new(xdg_dirs: BaseDirectories) -> Self {
|
||||||
|
Self { xdg_dirs }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Repository for FsRepo {
|
||||||
|
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error> {
|
||||||
|
self.get_tasks_impl()
|
||||||
|
.map(|tasks| tasks.into_iter().map(Task::into).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_task(&self, insert_data: InsertTaskData) -> Result<domain::Task, Error> {
|
||||||
|
let new_task = Task {
|
||||||
|
name: insert_data.name,
|
||||||
|
link: insert_data.link,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut tasks = self.get_tasks_impl()?;
|
||||||
|
tasks.push(new_task.clone());
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
Ok(new_task.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FsRepo {
|
||||||
|
fn get_tasks_impl(&self) -> Result<Vec<Task>, Error> {
|
||||||
|
let file_path = self.xdg_dirs.get_data_file(DATA_FILE);
|
||||||
|
File::open(&file_path)
|
||||||
|
.map_err(|_| Error::NotFound)
|
||||||
|
.and_then(|file| serde_json::from_reader(file).map_err(|_| Error::InvalidData))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue