~jshsj/rust_todo

7c8f9a09d0d463be5ca55632212a2cf1acea71cc — Joshua 1 year, 8 months ago
push wip
6 files changed, 440 insertions(+), 0 deletions(-)

A .gitignore
A Cargo.lock
A Cargo.toml
A src/main.rs
A src/tasks.rs
A tasks/finish_this.
A  => .gitignore +2 -0
@@ 1,2 @@
/target
**/*.rs.bk
\ No newline at end of file

A  => Cargo.lock +245 -0
@@ 1,245 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "autocfg"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "backtrace"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
 "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)",
 "rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
 "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "backtrace-sys"
version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)",
 "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "cc"
version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "chrono"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
 "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)",
 "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "failure"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)",
 "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "failure_derive"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
 "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
 "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
 "synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "itoa"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "libc"
version = "0.2.48"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "num-integer"
version = "0.1.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "num-traits"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "proc-macro2"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "quote"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "redox_syscall"
version = "0.1.51"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "rustc-demangle"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "ryu"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "serde"
version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "serde_derive 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "serde_derive"
version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
 "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
 "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "serde_json"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "syn"
version = "0.15.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
 "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "synstructure"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
 "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
 "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "time"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)",
 "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)",
 "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "todo"
version = "0.1.0"
dependencies = [
 "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "winapi"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[metadata]
"checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799"
"checksum backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b493b66e03090ebc4343eb02f94ff944e0cbc9ac6571491d170ba026741eb5"
"checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6"
"checksum cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)" = "4390a3b5f4f6bce9c1d0c00128379df433e53777fdd30e92f16a529332baec4e"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b"
"checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047"
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
"checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85"
"checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619"
"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7"
"checksum serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)" = "2e20fde37801e83c891a2dc4ebd3b81f0da4d1fb67a9e0a2a3b921e2536a58ee"
"checksum serde_derive 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)" = "633e97856567e518b59ffb2ad7c7a4fd4c5d91d9c7f32dd38a27b2bf7e8114ea"
"checksum serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "27dce848e7467aa0e2fcaf0a413641499c0b745452aaca1194d24dedde9e13c9"
"checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

A  => Cargo.toml +11 -0
@@ 1,11 @@
[package]
name = "todo"
version = "0.1.0"
authors = ["Joshua <kontakt@joshuastuebner.com>"]
edition = "2018"

[dependencies]
chrono = { version = "0.4", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
failure = "0.1.5"
\ No newline at end of file

A  => src/main.rs +21 -0
@@ 1,21 @@
#[macro_use] extern crate failure;

use std::fmt;
use std::io::{Read, Write, BufReader, BufRead};

use serde;
use serde::{Serialize, Deserialize};
extern crate chrono;
use chrono::{DateTime, Utc, TimeZone};

mod tasks;


fn main() {
    let mut task_manager = tasks::TaskManager::new();
    for task in task_manager.all_tasks() {
        println!("{}", task);
        
    }
}


A  => src/tasks.rs +160 -0
@@ 1,160 @@
use serde;
use serde::{Serialize, Deserialize};
use serde_json;
use chrono::{DateTime, Utc, TimeZone};
use std::fmt;
use std::fs::{read_dir, File};

use failure::Error;

static PATH_PREFIX: &str = "./tasks/";

#[derive(Serialize, Deserialize, Debug)]
pub struct Task {
    pub name: String,
    pub path: String,
    pub sub_tasks: Vec<SubTask>,
    pub done: bool,
    pub due_date: Option<DateTime<Utc>>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SubTask {
    pub name: String,
    pub done: bool,
    pub due_date: Option<DateTime<Utc>>,
}


impl Task {
    fn new(name: String, due_date: Option<DateTime<Utc>>) -> Result<Task, Error> {
        let path = convert_name_to_path(&name);
        let t = Task {
            name,
            path,
            sub_tasks: Vec::<SubTask>::new(),
            done: false,
            due_date,
        };
        t.save()?;
        Ok(t)
    }

    fn add_subtask(&mut self, name: String, due_date: Option<DateTime<Utc>>) -> Result<(), Error> {
        self.sub_tasks.push(SubTask::new(name, due_date));
        self.save()?;
        Ok(())
    }

    fn save(&self) -> Result<(), Error> {
        let file = File::create(format!("{}{}", PATH_PREFIX, self.path))
            .expect("Couldn't create file");
        serde_json::to_writer(file, &self)?;
        Ok(())
    }
}

impl SubTask {
    fn new(name: String, due_date: Option<DateTime<Utc>>) -> SubTask {
        SubTask {
            name,
            done: false,
            due_date,
        }
    }
}

impl fmt::Display for Task {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl fmt::Display for SubTask {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

pub struct TaskManager {
    tasklist: Vec<Task>
}

impl TaskManager {
    pub fn new() -> TaskManager {
        let mut t = TaskManager {
            tasklist: Vec::<Task>::new()
        };
        t.init();
        t
    }

    pub fn add_task(&mut self, name: String) -> Result<(), Error> {
        self.tasklist.push(Task::new(name, None)?);
        Ok(())
    }

    pub fn add_task_with_date(&mut self, name: String, due_date: DateTime<Utc>) -> Result<(), Error> {
        self.tasklist.push(Task::new(name, Some(due_date))?);
        Ok(())
    }

    pub fn add_subtask_to_task(&mut self, task_name: String, subtask_name: String) -> Result<(),Error> {
        let mut task = self.get_task_by_name(task_name)?;
        task.add_subtask(subtask_name, None)?;
        Ok(())

    }

    pub fn get_task_by_name(&mut self, task_name: String) -> Result<Task, Error> {
        let filtered: Vec<Task> = self.tasklist.as_ref().into_iter().filter(|t| t.name == task_name).collect();
        if filtered.is_empty() {
            bail!("No Item founds")
        }
        Ok(filtered[0])
    }

    pub fn all_tasks(&mut self) -> &Vec<Task> {
        &self.tasklist
    }

    fn init(&mut self) {
        let task_files = read_task_files();
        for task_file in task_files {
            //let t: Vec<&str> = task_file.split("/").collect();
            //let path = String::from(t[2]);
            &self.load_task(task_file);
        }
    }

    fn load_task(&mut self, path: String) {
        let file = File::open(&path).unwrap();
        let task: Task = serde_json::from_reader(file)
            .expect(format!("Can't parse file {}", path).as_str());
        self.tasklist.push(task);
    }
}

//HELPERS

fn convert_name_to_path(name: &str) -> String {
    let lowercase = name.to_lowercase();
    let lowercase: Vec<&str> = lowercase.split(" ").collect();

    lowercase.join("_")
}

fn convert_path_to_name(path: &str) -> String {
    String::from("test")
}

fn read_task_files() -> Vec<String> {
    let files = read_dir("./tasks").expect("directory doesn't exist");
    let mut task_files = Vec::<String>::new();

    for file in files {
        task_files.push(String::from(file.unwrap().path().to_str().unwrap()));
    }

    task_files
}
\ No newline at end of file

A  => tasks/finish_this. +1 -0
@@ 1,1 @@
{"name":"Finish this.","path":"finish_this.","sub_tasks":[],"done":false,"due_date":"2019-02-11T10:12:44.961489753Z"}
\ No newline at end of file