Skip to content

Commit 8153336

Browse files
Merge pull request #3 from THARUN-BART/ci/cd-pipeline
refactor: adopt let-chains and explicit control flow; eliminate Clipp…
2 parents 76bbaad + edeafa8 commit 8153336

26 files changed

Lines changed: 138 additions & 134 deletions

.github/workflows/rust-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ jobs:
5050

5151
# 8️⃣ Make x.sh executable
5252
- name: Make script executable
53-
run: chmod +x CoTask/x.sh
53+
run: chmod +x x.sh
5454

5555
# 9️⃣ Build using your custom script
5656
- name: Build Project
57-
run: bash CoTask/x.sh build
57+
run: bash ./x.sh build

src/bin/cli.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use cotask::logic::{
2+
add_task, branch, checkout, diff, export, gc, import, init_repo, list_task, merge, rebase,
3+
resolve, revert, show_help, show_log, stash, tag,
4+
};
15
use std::env;
2-
use cotask::logic::{add_task, branch, checkout, diff, gc, init_repo,import,export,list_task, merge, revert, show_help, show_log,resolve, rebase,stash,tag};
36

47
fn main() {
58
let args: Vec<String> = env::args().collect();
@@ -38,18 +41,16 @@ fn main() {
3841
println!("Please provide task id.");
3942
return;
4043
}
41-
let id:usize = match args[2].parse() {
44+
let id: usize = match args[2].parse() {
4245
Ok(n) => n,
43-
Err(_) =>{
46+
Err(_) => {
4447
println!("Invalid task ID");
4548
return;
4649
}
47-
4850
};
4951
add_task::mark_done(id);
50-
5152
}
52-
53+
5354
"log" => show_log::show_log(),
5455

5556
"checkout" => {
@@ -99,7 +100,6 @@ fn main() {
99100
merge::merge_branch(&args[2]);
100101
}
101102

102-
103103
"branch" => {
104104
if args.len() < 3 {
105105
println!("Provide branch name.");
@@ -125,7 +125,6 @@ fn main() {
125125
}
126126
"--help" => {
127127
show_help::show_help();
128-
return;
129128
}
130129

131130
"tag" => {
@@ -140,7 +139,6 @@ fn main() {
140139
tag::list_tags();
141140
}
142141

143-
144142
"resolve" => {
145143
if args.len() < 4 {
146144
println!("Usage: resolve <task_id> done|undone");
@@ -160,18 +158,17 @@ fn main() {
160158
resolve::resolve(id, done);
161159
}
162160
"rebase" => {
163-
if args.len() < 3 {
164-
println!("Provide target branch.");
165-
return;
166-
}
161+
if args.len() < 3 {
162+
println!("Provide target branch.");
163+
return;
164+
}
167165

168-
rebase::rebase_onto(&args[2]);
169-
}
166+
rebase::rebase_onto(&args[2]);
167+
}
170168

171169
_ => {
172170
println!("Unknown command.\n");
173-
show_help::show_help();
171+
show_help::show_help();
174172
}
175-
176173
}
177174
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub mod logic;
2+
pub mod models;
23
pub mod storage;
3-
pub mod models;

src/logic/add_task.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::models::{commit_model::Commit, task_model::Task};
22
use crate::storage::{
33
commit::{load_commit, save_commit},
4-
head::{read_head_branch, read_branch_commit, write_branch_commit},
4+
head::{read_branch_commit, read_head_branch, write_branch_commit},
55
};
66

77
pub fn add_task(text: &str) {
@@ -41,7 +41,11 @@ pub fn add_task(text: &str) {
4141
let message = format!("Added task '{}'", text);
4242

4343
let new_commit = Commit {
44-
parents: if head_commit == 0 { vec![] } else { vec![head_commit] },
44+
parents: if head_commit == 0 {
45+
vec![]
46+
} else {
47+
vec![head_commit]
48+
},
4549
message,
4650
tasks,
4751
};

src/logic/branch.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::storage::head::{read_branch_commit, read_head_branch};
12
use std::fs;
2-
use crate::storage::{commit, head::{read_branch_commit, read_head_branch}};
33

44
pub fn create_branch(name: &str) {
55
let current_branch = match read_head_branch() {
@@ -62,15 +62,14 @@ pub fn delete_branch(name: &str) {
6262
println!("Repository not initialized.");
6363
return;
6464
}
65-
6665
};
6766
// Prevent deleting current branch
6867
if name == current_branch {
6968
println!("Cannot delete the current branch '{}'.", name);
7069
return;
7170
}
7271

73-
let path = format!(".cotask/refs/{}",name);
72+
let path = format!(".cotask/refs/{}", name);
7473
// Check branch exists
7574
if fs::metadata(&path).is_err() {
7675
println!("Branch '{}' does not exist.", name);
@@ -83,5 +82,5 @@ pub fn delete_branch(name: &str) {
8382
return;
8483
}
8584

86-
println!("Branch '{}' deleted.",name);
87-
}
85+
println!("Branch '{}' deleted.", name);
86+
}

src/logic/checkout.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::fs;
1+
use crate::logic::tag::read_tag_commit;
22
use crate::storage::{
33
commit::load_commit,
4-
head::{write_head_branch, read_head_branch},
4+
head::{read_head_branch, write_head_branch},
55
};
6-
use crate::logic::tag::read_tag_commit;
6+
use std::fs;
77

88
pub fn checkout_commit(commit_number: usize) {
99
if commit_number == 0 {
@@ -28,8 +28,10 @@ pub fn checkout_ref(name: &str) {
2828
// 1) Branch check
2929
let branch_path = format!(".cotask/refs/{}", name);
3030
if fs::metadata(&branch_path).is_ok() {
31-
if let Ok(current) = read_head_branch() {
32-
if current == name {
31+
if let Ok(current) = read_head_branch()
32+
&& current == name
33+
{
34+
{
3335
println!("Already on branch '{}'", name);
3436
return;
3537
}

src/logic/diff.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::collections::HashMap;
2-
use crate::storage::commit::load_commit;
31
use crate::models::task_model::Task;
2+
use crate::storage::commit::load_commit;
3+
use std::collections::HashMap;
44

55
pub fn diff(c1: usize, c2: usize) {
66
let commit1 = match load_commit(c1) {
@@ -48,13 +48,13 @@ pub fn diff(c1: usize, c2: usize) {
4848

4949
// Status changes
5050
for (id, task1) in &map1 {
51-
if let Some(task2) = map2.get(id) {
52-
if task1.completed != task2.completed {
53-
if task2.completed {
54-
println!("✓ Completed: {}. {}", task2.id, task2.text);
55-
} else {
56-
println!("↺ Marked incomplete: {}. {}", task2.id, task2.text);
57-
}
51+
if let Some(task2) = map2.get(id)
52+
&& task1.completed != task2.completed
53+
{
54+
if task2.completed {
55+
println!("✓ Completed: {}. {}", task2.id, task2.text);
56+
} else {
57+
println!("↺ Marked incomplete: {}. {}", task2.id, task2.text);
5858
}
5959
}
6060
}

src/logic/export.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1+
use crate::{models::export_model::RepoExport, storage::commit::load_commit};
12
use std::{collections::HashMap, fs};
2-
use crate::{
3-
models::export_model::RepoExport,
4-
storage::commit::load_commit,
5-
};
63

74
pub fn export_repo() {
85
let mut commits = HashMap::new();
@@ -13,10 +10,10 @@ pub fn export_repo() {
1310
if let Ok(entries) = fs::read_dir(".cotask/commits") {
1411
for e in entries.flatten() {
1512
let name = e.file_name().into_string().unwrap();
16-
if let Ok(num) = name.trim_end_matches(".json").parse::<usize>() {
17-
if let Ok(commit) = load_commit(num) {
18-
commits.insert(num, commit);
19-
}
13+
if let Ok(num) = name.trim_end_matches(".json").parse::<usize>()
14+
&& let Ok(commit) = load_commit(num)
15+
{
16+
commits.insert(num, commit);
2017
}
2118
}
2219
}
@@ -43,7 +40,12 @@ pub fn export_repo() {
4340

4441
let head = fs::read_to_string(".cotask/HEAD").unwrap();
4542

46-
let export = RepoExport { commits, branches, tags, head };
43+
let export = RepoExport {
44+
commits,
45+
branches,
46+
tags,
47+
head,
48+
};
4749

4850
let json = serde_json::to_string_pretty(&export).unwrap();
4951
fs::write("cotask_backup.json", json).unwrap();

src/logic/gc.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::collections::HashSet;
22
use std::fs;
33

44
use crate::storage::{
5-
head::{read_head_branch, read_branch_commit},
65
commit::load_commit,
6+
head::{read_branch_commit, read_head_branch},
77
};
88

99
fn collect_reachable(commit_number: usize, reachable: &mut HashSet<usize>) {
@@ -58,14 +58,12 @@ pub fn run_gc() {
5858
let mut deleted = 0;
5959

6060
for entry in paths.flatten() {
61-
if let Some(name) = entry.path().file_stem() {
62-
if let Ok(num) = name.to_string_lossy().parse::<usize>() {
63-
if !reachable.contains(&num) {
64-
if fs::remove_file(entry.path()).is_ok() {
65-
deleted += 1;
66-
}
67-
}
68-
}
61+
if let Some(name) = entry.path().file_stem()
62+
&& let Ok(num) = name.to_string_lossy().parse::<usize>()
63+
&& !reachable.contains(&num)
64+
&& fs::remove_file(entry.path()).is_ok()
65+
{
66+
deleted += 1;
6967
}
7068
}
7169

src/logic/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::fs;
21
use crate::models::export_model::RepoExport;
32
use crate::storage::commit::save_commit;
3+
use std::fs;
44

55
pub fn import_repo(file: &str) {
66
let data = fs::read_to_string(file).unwrap();

0 commit comments

Comments
 (0)