todocli-nepiy 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Cargo.lock ADDED
@@ -0,0 +1,7 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "todocli"
7
+ version = "0.1.0"
package/Cargo.toml ADDED
@@ -0,0 +1,6 @@
1
+ [package]
2
+ name = "todocli"
3
+ version = "0.1.0"
4
+ edition = "2024"
5
+
6
+ [dependencies]
package/mytodolist ADDED
@@ -0,0 +1,2 @@
1
+ Sell a Laptop
2
+ Buy a new Phone
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "todocli-nepiy",
3
+ "version": "1.0.0",
4
+ "description": "main.rs",
5
+ "keywords": [
6
+ "todo"
7
+ ],
8
+ "homepage": "https://github.com/nepiy/Rust-TO-DO-CLI-App#readme",
9
+ "bugs": {
10
+ "url": "https://github.com/nepiy/Rust-TO-DO-CLI-App/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/nepiy/Rust-TO-DO-CLI-App.git"
15
+ },
16
+ "license": "ISC",
17
+ "author": "nepiy",
18
+ "type": "commonjs",
19
+ "main": "main.rs",
20
+ "scripts": {
21
+ "test": "cargo test"
22
+ }
23
+ }
package/src/lib.rs ADDED
@@ -0,0 +1,111 @@
1
+ // function collection for main.rs
2
+ use std::fs::File;
3
+ use std::io::{BufRead, BufReader, Write};
4
+ use std::io;
5
+
6
+ pub fn add_task(task: &str, list: &mut Vec<String>) -> Result<(), String> {
7
+
8
+ if list.iter().any(|x| x == task) {
9
+ return Err(format!("\"{task}\" already exists"));
10
+ }
11
+ list.push(task.to_string());
12
+ println!("Added {task} to the list");
13
+ Ok(())
14
+ }
15
+
16
+ pub fn load_data(filename: &str) -> Vec<String> {
17
+ match File::open(filename) {
18
+ Ok(file) => {
19
+ let reader = BufReader::new(file);
20
+ reader
21
+ .lines()
22
+ .filter_map(|x| x.ok())
23
+ .map(|x| x.trim().to_string())
24
+ .collect()
25
+ },
26
+ Err(_) => {
27
+ Vec::new()
28
+ }
29
+ }
30
+ }
31
+
32
+ pub fn save_data(filename: &str, list: &mut Vec<String>) -> io::Result<()> {
33
+ let mut file = File::create(filename)?;
34
+ if let Err(e) = writeln!(file, "-- Your tasks --") {
35
+ println!("Err: {e}");
36
+ }
37
+ for item in list {
38
+ writeln!(file, "{}", item)?;
39
+ }
40
+ Ok(())
41
+ }
42
+
43
+ pub fn remove_task(task: &str, list: &mut Vec<String>) -> Result<(), String> {
44
+ if !list.iter().any(|x| x == task) {
45
+ return Err(format!("\"{}\" isn't available in List", task));
46
+ }
47
+ list.retain(|x| x != task);
48
+ Ok(())
49
+ }
50
+
51
+ pub fn edit_task(task_from: &str, task_to: &str, list: &mut Vec<String>) -> Result<(), String> {
52
+ if !list.iter().any(|x| x == task_from) {
53
+ return Err(format!("\"{}\" isn't available in List", task_from));
54
+ }
55
+ for item in list {
56
+ if item == task_from {
57
+ *item = task_to.to_string();
58
+ }
59
+ }
60
+
61
+ Ok(())
62
+ }
63
+
64
+ pub fn is_completed(task: &str, list: &mut Vec<String>, completed_list: &mut Vec<String>) -> Result<(), String> {
65
+ if !list.iter().any(|x| x == task) {
66
+ return Err(format!("\"{}\" isn't available in List", task));
67
+ }
68
+
69
+ list.retain(|x| x != task);
70
+ completed_list.push(task.to_string());
71
+
72
+ Ok(())
73
+ }
74
+
75
+ #[cfg(test)]
76
+ mod tests {
77
+ use super::*;
78
+
79
+ #[test]
80
+ fn test_add() {
81
+ let mut list: Vec<String> = vec!["Apple".to_string()];
82
+ let a = "Apple";
83
+ let result = add_task(&a, &mut list);
84
+ assert!(result.is_ok());
85
+
86
+ }
87
+ #[test]
88
+ fn test_remove() {
89
+ let mut list: Vec<String> = vec!["Apple".to_string(), "Ball".to_string()];
90
+ let result = remove_task("Ball", &mut list);
91
+ assert_eq!(list.join(", "), "Apple");
92
+ assert!(result.is_ok());
93
+ }
94
+
95
+ #[test]
96
+ fn test_edit() {
97
+ let mut list: Vec<String> = vec!["Apple".to_string(), "Ball".to_string()];
98
+ let result = edit_task("Ball", "Cow", &mut list);
99
+
100
+ assert!(result.is_ok());
101
+ }
102
+
103
+ #[test]
104
+ fn complete_test() {
105
+ let mut list: Vec<String> = vec!["Apple".to_string(), "Ball".to_string()];
106
+ let mut completed: Vec<String> = vec![];
107
+ let result = is_completed("Ball", &mut list, &mut completed);
108
+ assert!(result.is_ok());
109
+ }
110
+
111
+ }
package/src/main.rs ADDED
@@ -0,0 +1,130 @@
1
+ use todocli::{add_task, is_completed, remove_task, edit_task, load_data, save_data};
2
+ use std::io;
3
+ fn main() {
4
+ 'outer_loop: loop {
5
+ let mut list: Vec<String> = vec![];
6
+ let mut completed_task: Vec<String> = vec![];
7
+
8
+ println!("Do you already have a list file ? \nIf you have press '1', if not press'0'");
9
+ let mut choice = String::new();
10
+ io::stdin().read_line(&mut choice).expect("1 or 0");
11
+ let choice: i32 = choice.trim().parse().unwrap();
12
+
13
+ if choice == 1 {
14
+ println!("Enter filename: ");
15
+
16
+ let mut filename = String::new();
17
+ io::stdin().read_line(&mut filename).unwrap();
18
+ let filename = filename.trim();
19
+ list = load_data(filename);
20
+
21
+ }
22
+
23
+ println!("Entering main menu");
24
+
25
+
26
+ 'main_menu: loop {
27
+ println!("
28
+ Main menu:
29
+ 1. Add Task
30
+ 2. Show Task list
31
+ 3. Edit Task
32
+ 4. Remove Task
33
+ 5. Completed Task
34
+ 6. Save List
35
+ 7. Exit the Application
36
+
37
+ Enter the option of your choice:
38
+ ");
39
+ let mut menu = String::new();
40
+ io::stdin().read_line(&mut menu).expect("1 or 0");
41
+ let menu: i32 = menu.trim().parse().expect(" a num ");
42
+
43
+ match menu {
44
+ 1 => {
45
+ println!("Enter Task: ");
46
+ let mut task = String::new();
47
+ io::stdin().read_line(&mut task).expect("a task");
48
+ let task = task.trim();
49
+ if let Err(e) = add_task(task, &mut list) {
50
+ println!("Error: {e}");
51
+ }
52
+ continue 'main_menu;
53
+ },
54
+
55
+ 2 => {
56
+ println!("{}", list.join(", "));
57
+ continue 'main_menu;
58
+ },
59
+
60
+ 3 => {
61
+ println!("Your list contains: {}", list.join(", "));
62
+ println!("Enter Task you want to edit: ");
63
+ let mut task = String::new();
64
+ io::stdin().read_line(&mut task).expect("a task");
65
+ let task = task.trim();
66
+
67
+ println!("Enter Task you want to edit to: ");
68
+ let mut task1 = String::new();
69
+ io::stdin().read_line(&mut task1).expect("a task");
70
+ let task1 = task1.trim();
71
+
72
+ if let Err(e) = edit_task(task, task1, &mut list) {
73
+ println!("Err: {e}");
74
+ }
75
+
76
+ continue 'main_menu;
77
+ },
78
+
79
+ 4 => {
80
+ println!("Your list contains: {}", list.join(", "));
81
+ println!("Enter Task you want to remove: ");
82
+ let mut task = String::new();
83
+ io::stdin().read_line(&mut task).expect("a task");
84
+ let task = task.trim();
85
+
86
+ if let Err(e) = remove_task(task, &mut list) {
87
+ println!("Error: {e}");
88
+ }
89
+
90
+ continue 'main_menu;
91
+ },
92
+
93
+ 5 => {
94
+ println!("Your list contains: {}", list.join(", "));
95
+ println!("Enter completed task: ");
96
+ let mut task = String::new();
97
+ io::stdin().read_line(&mut task).expect("a task");
98
+ let task = task.trim();
99
+
100
+ if let Err(e) = is_completed(task, &mut list, &mut completed_task) {
101
+ println!("Error: {e}");
102
+ }
103
+
104
+ continue 'main_menu;
105
+ },
106
+
107
+ 6 => {
108
+ println!("Enter filename to save: ");
109
+ let mut filename = String::new();
110
+ io::stdin().read_line(&mut filename).expect("a task");
111
+ let filename = filename.trim();
112
+
113
+ if let Err(e) = save_data(filename, &mut list) {
114
+ println!("Error {e}");
115
+ }
116
+ continue 'main_menu;
117
+ },
118
+
119
+ _ => {
120
+ println!("Exiting the platform");
121
+ use std::thread;
122
+ use std::time::Duration;
123
+ thread::sleep(Duration::from_secs(2));
124
+ break 'outer_loop;
125
+ }
126
+ }
127
+
128
+ }
129
+ }
130
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "folders": [
3
+ {
4
+ "path": "../../rust"
5
+ },
6
+ {
7
+ "path": ".."
8
+ }
9
+ ],
10
+ "settings": {}
11
+ }
package/whatisthis ADDED
@@ -0,0 +1,2 @@
1
+ Your tasks:
2
+ Bye bye