Skip to content

Commit

Permalink
chore: add rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Oct 17, 2024
1 parent e3a5700 commit 2f2359a
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hard_tabs = false
max_width = 80
newline_style = "Unix"
tab_spaces = 4
use_field_init_shorthand = true
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"release:publish": "clean-publish",
"release:version": "changelogen --output changelog.md --release --push",
"test": "pnpm run /^test:/",
"test:format": "prettier --check \"**/*.{svelte,js,ts,css,json,md,yml}\"",
"test:format": "pnpm run /^test:format:/",
"test:format:js": "prettier --check \"**/*.{svelte,js,ts,css,json,md,yml}\"",
"test:format:rust": "cargo fmt -- --check",
"test:css": "stylelint **/*.{svelte,css}",
"test:js": "eslint \"**/*.{svelte,js,ts}\"",
"test:spelling": "cspell \"**/*\"",
Expand Down
12 changes: 8 additions & 4 deletions src/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub async fn blame(path: &str, line: u32) -> Option<BlameData> {
author: String::new(),
};

let mut key_map: HashMap<&str, Box<dyn FnMut(&mut BlameData, String)>> = HashMap::new();
let mut key_map: HashMap<&str, Box<dyn FnMut(&mut BlameData, String)>> =
HashMap::new();
key_map.insert("author-mail ", Box::new(|b, v| b.author_mail = v));
key_map.insert("author-time ", Box::new(|b, v| b.author_time = v));
key_map.insert("author-tz ", Box::new(|b, v| b.author_tz = v));
Expand All @@ -50,14 +51,17 @@ pub async fn blame(path: &str, line: u32) -> Option<BlameData> {
for current_line in lines {
for (key, setter) in &mut key_map {
if current_line.starts_with(key) {
let value = current_line.replacen(key, "", 1).trim().to_string();
let value =
current_line.replacen(key, "", 1).trim().to_string();
setter(&mut blame_data, value);
}
}

if current_line.len() >= 40 && current_line.chars().take(40).all(|c| c.is_ascii_hexdigit())
if current_line.len() >= 40
&& current_line.chars().take(40).all(|c| c.is_ascii_hexdigit())
{
blame_data.commit = current_line.split_whitespace().next().unwrap().to_string();
blame_data.commit =
current_line.split_whitespace().next().unwrap().to_string();
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/copy_dir_recursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub async fn copy_dir_recursive(src: &Path, dst: &Path) -> io::Result<()> {
Ok(())
}

fn copy_dir_recursive_boxed<'a>(src: &'a Path, dst: &'a Path) -> BoxFuture<'a, io::Result<()>> {
fn copy_dir_recursive_boxed<'a>(
src: &'a Path,
dst: &'a Path,
) -> BoxFuture<'a, io::Result<()>> {
Box::pin(copy_dir_recursive(src, dst))
}
3 changes: 2 additions & 1 deletion src/get_files_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub async fn get_files_list(
};

let output = exec(&git_command).await?;
let files: Vec<String> = output.lines().map(|line| line.to_string()).collect();
let files: Vec<String> =
output.lines().map(|line| line.to_string()).collect();
Ok(files)
}
18 changes: 10 additions & 8 deletions src/get_todoctor_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ pub async fn get_todoctor_version() -> Option<String> {

let package_json_path = parent_dir.join("package.json");

let package_json_content = match fs::read_to_string(&package_json_path).await {
Ok(content) => content,
Err(e) => {
eprintln!("Error reading package.json: {:?}", e);
return None;
}
};
let package_json_content =
match fs::read_to_string(&package_json_path).await {
Ok(content) => content,
Err(e) => {
eprintln!("Error reading package.json: {:?}", e);
return None;
}
};

let package_json: Value = match serde_json::from_str(&package_json_content) {
let package_json: Value = match serde_json::from_str(&package_json_content)
{
Ok(json) => json,
Err(e) => {
eprintln!("Error parsing package.json: {:?}", e);
Expand Down
89 changes: 62 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ async fn main() {
comments
.into_iter()
.filter_map(|comment| {
if let Some(comment_kind) = identify_todo_comment(&comment.text) {
if let Some(comment_kind) =
identify_todo_comment(&comment.text)
{
Some(TodoData {
path: source_file_name.clone(),
comment: comment.text.clone(),
Expand All @@ -93,7 +95,10 @@ async fn main() {
.collect::<Vec<TodoData>>()
}
Err(e) => {
eprintln!("Error reading file {}: {:?}", source_file_name, e);
eprintln!(
"Error reading file {}: {:?}",
source_file_name, e
);
vec![]
}
}
Expand All @@ -113,9 +118,13 @@ async fn main() {
.map(|todo| {
tokio::spawn(async move {
if let Ok(source_code) = fs::read_to_string(&todo.path).await {
if let Some(line) = get_line_from_position(todo.start, &source_code) {
if let Some(blame_data) = blame(&todo.path, line).await {
let prepared_blame: PreparedBlameData = prepare_blame_data(blame_data);
if let Some(line) =
get_line_from_position(todo.start, &source_code)
{
if let Some(blame_data) = blame(&todo.path, line).await
{
let prepared_blame: PreparedBlameData =
prepare_blame_data(blame_data);
return Some(TodoWithBlame {
comment: todo.comment.trim().to_string(),
path: todo.path.clone(),
Expand Down Expand Up @@ -143,7 +152,8 @@ async fn main() {
let mut history: Vec<(String, String)> = get_history(Some(3)).await;
let mut todo_count: usize = todos_with_blame.len();

let temp_file = File::create("todo_history_temp.json").expect("Failed to create temp file");
let temp_file = File::create("todo_history_temp.json")
.expect("Failed to create temp file");
let mut writer = BufWriter::new(temp_file);

if history.len() > 1 {
Expand Down Expand Up @@ -181,22 +191,40 @@ async fn main() {
let semaphore = semaphore.clone();
let commit_hash = commit_hash.clone();
tokio::spawn(async move {
let permit = semaphore.acquire_owned().await.unwrap();
if let Ok(diff_output) =
exec(&["git", "diff", commit_hash.as_str(), "--", &file_path]).await
let permit =
semaphore.acquire_owned().await.unwrap();
if let Ok(diff_output) = exec(&[
"git",
"diff",
commit_hash.as_str(),
"--",
&file_path,
])
.await
{
for line in diff_output.lines() {
if line.starts_with("+") || line.starts_with("-") {
let line_uppercase = line.to_uppercase();
if line.starts_with("+")
|| line.starts_with("-")
{
let line_uppercase =
line.to_uppercase();

let added = line.starts_with("+")
&& TODO_KEYWORDS.iter().any(|keyword| {
line_uppercase.contains(&keyword.to_uppercase())
});
&& TODO_KEYWORDS.iter().any(
|keyword| {
line_uppercase.contains(
&keyword.to_uppercase(),
)
},
);
let removed = line.starts_with("-")
&& TODO_KEYWORDS.iter().any(|keyword| {
line_uppercase.contains(&keyword.to_uppercase())
});
&& TODO_KEYWORDS.iter().any(
|keyword| {
line_uppercase.contains(
&keyword.to_uppercase(),
)
},
);

if added {
return Some((true, file_path));
Expand Down Expand Up @@ -232,8 +260,10 @@ async fn main() {
date: date.clone(),
};

let json_entry = serde_json::to_string(&todo_history).expect("Failed to serialize");
writeln!(writer, "{}", json_entry).expect("Failed to write to temp file");
let json_entry = serde_json::to_string(&todo_history)
.expect("Failed to serialize");
writeln!(writer, "{}", json_entry)
.expect("Failed to write to temp file");
}
}
}
Expand All @@ -242,12 +272,14 @@ async fn main() {

println!("\nFound {} todos", todo_count);

let temp_file = File::open("todo_history_temp.json").expect("Failed to open temp file");
let temp_file =
File::open("todo_history_temp.json").expect("Failed to open temp file");
let reader = BufReader::new(temp_file);

for line in reader.lines() {
let entry: TodoHistory = serde_json::from_str(&line.expect("Error reading line"))
.expect("Error deserializing JSON");
let entry: TodoHistory =
serde_json::from_str(&line.expect("Error reading line"))
.expect("Error deserializing JSON");
println!(
"Date: {}, TODO count: {}, Commit {}",
entry.date, entry.todos_count, entry.commit
Expand All @@ -263,9 +295,10 @@ async fn main() {
.await
.expect("Error creating directory");

let current_directory =
get_current_directory().expect("Error: Could not get current directory.");
let project_name = get_project_name().unwrap_or_else(|| "Unknown Project".to_string());
let current_directory = get_current_directory()
.expect("Error: Could not get current directory.");
let project_name =
get_project_name().unwrap_or_else(|| "Unknown Project".to_string());
let version = get_todoctor_version()
.await
.unwrap_or_else(|| "Unknown Version".to_string());
Expand All @@ -277,7 +310,8 @@ async fn main() {
"version": version,
});

let json_string: String = serde_json::to_string(&json_data).expect("Error serializing data");
let json_string: String =
serde_json::to_string(&json_data).expect("Error serializing data");

let current_exe_path: PathBuf =
get_current_exe_path().expect("Error: Could not get current exe path.");
Expand All @@ -297,7 +331,8 @@ async fn main() {
.expect("Error reading index.html");

if let Some(pos) = index_content.find("</head>") {
let script_tag: String = format!("<script>window.data = {};</script>", json_string);
let script_tag: String =
format!("<script>window.data = {};</script>", json_string);
index_content.insert_str(pos, &script_tag);

fs::write(&index_path, index_content)
Expand Down
3 changes: 2 additions & 1 deletion src/prepare_blame_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub fn prepare_blame_data(data: BlameData) -> PreparedBlameData {

let timezone_offset_hours: i32 = data.author_tz[0..3].parse().unwrap();
let timezone_offset_minutes: i32 = data.author_tz[3..5].parse().unwrap();
let total_offset_minutes = timezone_offset_hours * 60 + timezone_offset_minutes;
let total_offset_minutes =
timezone_offset_hours * 60 + timezone_offset_minutes;

date = date + Duration::minutes(total_offset_minutes as i64);

Expand Down
4 changes: 2 additions & 2 deletions tests/check_git_repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use todoctor::check_git_repository::check_git_repository;
use std::path::Path;
use todoctor::check_git_repository::check_git_repository;

#[tokio::test]
async fn test_is_git_repository_true() {
Expand All @@ -19,4 +19,4 @@ async fn test_is_git_repository_false() {

let result = check_git_repository(current_dir_str).await;
assert!(!result);
}
}

0 comments on commit 2f2359a

Please sign in to comment.