Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 191c3b1a3f | |||
| 59cd6d5365 | |||
| d96017383e |
@@ -4,5 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.35", features = ["derive"] }
|
clap = { version = "4.5.36", features = ["derive"] }
|
||||||
csv = "1.3.1"
|
csv = "1.3.1"
|
||||||
|
json = "0.12.4"
|
||||||
|
prettytable = "0.10.0"
|
||||||
|
|||||||
@@ -20,3 +20,9 @@ __Example of todo lists__ <br>
|
|||||||
| __ID__ | __Name__ | __category__ |
|
| __ID__ | __Name__ | __category__ |
|
||||||
| -------| ---------| --------------|
|
| -------| ---------| --------------|
|
||||||
| 5 | Finish Todo App | side-project |
|
| 5 | Finish Todo App | side-project |
|
||||||
|
|
||||||
|
# How to build
|
||||||
|
You can build this using cargo.
|
||||||
|
`cargo build`
|
||||||
|
|
||||||
|
The build will appear inside "target/debug/todo".
|
||||||
18
src/main.rs
18
src/main.rs
@@ -27,6 +27,16 @@ struct Args {
|
|||||||
category: Option<String>,
|
category: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// function to create the data file if it doesn't exist
|
||||||
|
fn ensure_data_file(main_file: &str) -> std::io::Result<()> {
|
||||||
|
if !std::path::Path::new(main_file).exists() {
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
let file = fs::File::create(main_file)?;
|
||||||
|
let initial_data: String = "{\"data\": []}".to_owned();
|
||||||
|
fs::write(main_file, &initial_data)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// parse arguments
|
// parse arguments
|
||||||
@@ -38,9 +48,11 @@ fn main() {
|
|||||||
|
|
||||||
// main data file name
|
// main data file name
|
||||||
let main_file: String = "data.json".to_owned();
|
let main_file: String = "data.json".to_owned();
|
||||||
|
let _ = ensure_data_file(&main_file);
|
||||||
let current_string: String = fs::read_to_string(&main_file)
|
let current_string: String = fs::read_to_string(&main_file)
|
||||||
.expect("File could not be found.");
|
.expect("File is opened to store data.");
|
||||||
let current_data: &mut JsonValue = &mut json::parse(¤t_string).unwrap()["data"];
|
let current_data: &mut JsonValue = &mut json::parse(¤t_string).unwrap()["data"];
|
||||||
|
// let current_data: &mut JsonValue = &mut json::parse(¤t_string).unwrap();
|
||||||
|
|
||||||
// for list commands
|
// for list commands
|
||||||
if args.command == "list" {
|
if args.command == "list" {
|
||||||
@@ -48,7 +60,7 @@ fn main() {
|
|||||||
// todo list --all --category _category_
|
// todo list --all --category _category_
|
||||||
if args.category != None {
|
if args.category != None {
|
||||||
let category__: String = args.category.to_owned().unwrap();
|
let category__: String = args.category.to_owned().unwrap();
|
||||||
println!("You are trying to list all tasks in category: {}", category__);
|
// println!("You are trying to list all tasks in category: {}", category__);
|
||||||
for index_ in 0..current_data.len() {
|
for index_ in 0..current_data.len() {
|
||||||
let curr: JsonValue = current_data[index_].clone();
|
let curr: JsonValue = current_data[index_].clone();
|
||||||
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
|
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
|
||||||
@@ -63,7 +75,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
// todo list --all
|
// todo list --all
|
||||||
else {
|
else {
|
||||||
println!("You are trying to print all tasks!");
|
// println!("You are trying to print all tasks!");
|
||||||
for index_ in 0..current_data.len() {
|
for index_ in 0..current_data.len() {
|
||||||
let curr: JsonValue = current_data[index_].clone();
|
let curr: JsonValue = current_data[index_].clone();
|
||||||
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
|
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
|
||||||
|
|||||||
Reference in New Issue
Block a user