-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change to "kind of" a repository pattern
NOTE: THIS IS A BREAKING CHANGE The main aim of this was to take all the item handling out of main.rs which I think we've accomplished. There's still ALOT of duplication in there, so that's the next big change
- Loading branch information
1 parent
1587e24
commit 80d147b
Showing
5 changed files
with
123 additions
and
135 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "todo" | ||
version = "0.2.2" | ||
version = "0.3.0" | ||
authors = ["Todd Hainsworth <[email protected]>"] | ||
|
||
[dependencies] | ||
|
@@ -10,3 +10,4 @@ serde_derive = "1.0" | |
dirs = "4.0" | ||
colored = "2.0" | ||
clap = "2.32.0" | ||
home = "0.5.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use serde_json; | ||
use std::fs; | ||
use std::io::Result; | ||
use home::home_dir; | ||
|
||
const TODO_FILENAME: &'static str = ".todos"; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct Item { | ||
pub id: usize, | ||
pub text: String, | ||
pub completed: bool, | ||
} | ||
|
||
impl Item { | ||
pub fn new(id: usize, text: &str, completed: bool) -> Self { | ||
Self { | ||
id, | ||
text: String::from(text), | ||
completed, | ||
} | ||
} | ||
} | ||
|
||
impl Default for Item { | ||
fn default() -> Self { | ||
Item::new(0, "", false) | ||
} | ||
} | ||
|
||
pub struct ItemRepository { | ||
items: Vec<Item>, | ||
} | ||
|
||
impl ItemRepository { | ||
pub fn new() -> Result<Self> { | ||
let item_json = ItemRepository::load_items()?; | ||
let items: Vec<Item> = serde_json::from_str(&item_json)?; | ||
Ok(Self { items }) | ||
} | ||
|
||
pub fn delete(&mut self, id: usize) { | ||
self.items.retain(|item| item.id != id) | ||
} | ||
|
||
pub fn publish(self) -> Result<()> { | ||
let path = Self::get_todo_file_path(); | ||
let buf = serde_json::to_string(&self.items).unwrap(); | ||
fs::write(path, buf) | ||
} | ||
|
||
pub fn toggle(&mut self, id: usize) { | ||
self.items | ||
.iter_mut() | ||
.find(|item| item.id == id) | ||
.map(|item| item.completed = !item.completed); | ||
} | ||
|
||
pub fn update_text(&mut self, id: usize, text: &str) { | ||
self.items | ||
.iter_mut() | ||
.find(|item| item.id == id) | ||
.map(|item| item.text = text.to_string()); | ||
} | ||
|
||
pub fn add(&mut self, text: &str) { | ||
let id = self.items.iter().map(|item| item.id).max().unwrap_or(0) + 1; | ||
self.items.push(Item::new(id, text, false)) | ||
} | ||
|
||
pub fn items(&mut self) -> &mut Vec<Item> { | ||
self.items.as_mut() | ||
} | ||
|
||
fn load_items() -> Result<String> { | ||
fs::read_to_string(Self::get_todo_file_path()) | ||
} | ||
|
||
fn get_todo_file_path() -> String { | ||
// unwrapping because if this were to fail then there's something _really_ wrong with the users setup... | ||
let home = home_dir().unwrap(); | ||
format!("{}/{}", home.display(), TODO_FILENAME) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.