Skip to content

Commit 975f0f6

Browse files
committed
Update after fmt.
1 parent 31e7796 commit 975f0f6

File tree

8 files changed

+182
-104
lines changed

8 files changed

+182
-104
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ As I am starting my journey with Rust, here it goes a few reminders so I don't h
8181

8282
Before pushing to *crates.io*, run the following:
8383

84-
1. `cargo build`
85-
2. `cargo test`
86-
3. `cargo fmt`
84+
1. `cargo fmt`
85+
2. `cargo build`
86+
3. `cargo test`
8787
4. `cargo clippy`
8888

8989
If all good, `cargo package` and then `cargo publish`.

src/git_utils.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ pub async fn clone_repository(repository: &Repository) -> Result<Git2Repository,
88

99
// Remove existing folder if it exists
1010
if repository.path.exists() {
11-
fs::remove_dir_all(&repository.path)
12-
.await
13-
.map_err(|e| format!("Failed to remove existing directory {:?}: {}", repository.path, e))?;
11+
fs::remove_dir_all(&repository.path).await.map_err(|e| {
12+
format!(
13+
"Failed to remove existing directory {:?}: {}",
14+
repository.path, e
15+
)
16+
})?;
1417
}
1518

1619
tokio::task::spawn_blocking(move || {
17-
Git2Repository::clone(&repo_url, &path)
18-
.map_err(|e| format!("Git clone error: {}", e))
20+
Git2Repository::clone(&repo_url, &path).map_err(|e| format!("Git clone error: {}", e))
1921
})
2022
.await
2123
.map_err(|e| format!("Blocking task join error: {}", e))?

src/io_utils.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ pub async fn read_ignore_patterns(ignore_file: Option<PathBuf>) -> Result<Vec<St
2020
let content = fs::read_to_string(&path)
2121
.await
2222
.map_err(|e| format!("Failed to read ignore file {:?}: {}", path, e))?;
23-
return Ok(content.lines().map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect());
23+
return Ok(content
24+
.lines()
25+
.map(|s| s.trim().to_string())
26+
.filter(|s| !s.is_empty())
27+
.collect());
2428
} else {
25-
eprintln!("Warning: Ignore file {:?} not found. Proceeding without ignore patterns.", path);
29+
eprintln!(
30+
"Warning: Ignore file {:?} not found. Proceeding without ignore patterns.",
31+
path
32+
);
2633
}
2734
}
2835
Ok(Vec::new())
@@ -43,37 +50,35 @@ pub async fn write_content_to_file(path: &Path, content: &str) -> Result<(), Str
4350
/// The aliases are from the list of languages supported by Highlight.js.
4451
/// Returns an empty string if no alias is found.
4552
pub fn get_language_alias(path: &Path) -> &'static str {
46-
let extension = path.extension()
47-
.and_then(|ext| ext.to_str())
48-
.unwrap_or("");
53+
let extension = path.extension().and_then(|ext| ext.to_str()).unwrap_or("");
4954

5055
match extension.to_lowercase().as_str() {
51-
"sh" | "bash" => "bash",
52-
"c" => "c",
56+
"sh" | "bash" => "bash",
57+
"c" => "c",
5358
"cc" | "cxx" | "c++" | "cpp" => "cpp",
54-
"cs" => "csharp",
55-
"css" => "css",
56-
"go" => "go",
57-
"html" | "htm" => "xml",
58-
"java" => "java",
59-
"js" | "cjs" | "mjs" => "javascript",
60-
"json" => "json",
61-
"jsx" => "jsx",
62-
"kt" | "kts" => "kotlin",
63-
"md" | "markdown" => "markdown",
64-
"php" => "php",
65-
"py" => "python",
66-
"rb" => "ruby",
67-
"rs" => "rust",
68-
"scss" => "scss",
69-
"sql" => "sql",
70-
"swift" => "swift",
71-
"toml" => "toml",
72-
"ts" | "cts" | "mts" => "typescript",
73-
"tsx" => "tsx",
74-
"txt" => "",
75-
"yaml" | "yml" => "yaml",
76-
_ => "",
59+
"cs" => "csharp",
60+
"css" => "css",
61+
"go" => "go",
62+
"html" | "htm" => "xml",
63+
"java" => "java",
64+
"js" | "cjs" | "mjs" => "javascript",
65+
"json" => "json",
66+
"jsx" => "jsx",
67+
"kt" | "kts" => "kotlin",
68+
"md" | "markdown" => "markdown",
69+
"php" => "php",
70+
"py" => "python",
71+
"rb" => "ruby",
72+
"rs" => "rust",
73+
"scss" => "scss",
74+
"sql" => "sql",
75+
"swift" => "swift",
76+
"toml" => "toml",
77+
"ts" | "cts" | "mts" => "typescript",
78+
"tsx" => "tsx",
79+
"txt" => "",
80+
"yaml" | "yml" => "yaml",
81+
_ => "",
7782
}
7883
}
7984

@@ -111,4 +116,4 @@ mod tests {
111116
assert_eq!(get_language_alias(&PathBuf::from("unrecognized.xyz")), "");
112117
assert_eq!(get_language_alias(&PathBuf::from("no_extension")), "");
113118
}
114-
}
119+
}

src/lib.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
pub mod repository;
2-
pub mod io_utils;
31
pub mod git_utils;
2+
pub mod io_utils;
43
pub mod processing;
4+
pub mod repository;
55

6-
use repository::Repository;
7-
use processing::process_single_repository;
6+
use futures::future::join_all;
87
use io_utils::{ensure_directories, read_ignore_patterns};
9-
use tokio::fs;
8+
use processing::process_single_repository;
9+
use repository::Repository;
1010
use std::path::PathBuf;
11-
use futures::future::join_all;
1211
use std::sync::Arc;
12+
use tokio::fs;
1313

1414
/// Processes a list of GitHub URLs concurrently, downloads and processes content,
1515
/// and prepares it for AI tools.
@@ -19,7 +19,10 @@ pub async fn process_github_urls(
1919
merge_files: bool,
2020
ignore_file: Option<PathBuf>,
2121
) -> Result<Vec<PathBuf>, String> {
22-
println!("Library received URLs: {:?}, no_headers: {}, merge_files: {}", urls, no_headers, merge_files);
22+
println!(
23+
"Library received URLs: {:?}, no_headers: {}, merge_files: {}",
24+
urls, no_headers, merge_files
25+
);
2326

2427
// Prepare directories
2528
let download_dir = PathBuf::from("./temp_repos");
@@ -30,13 +33,17 @@ pub async fn process_github_urls(
3033
let ignore_patterns = Arc::new(read_ignore_patterns(ignore_file).await?);
3134

3235
// Spawn processing tasks
33-
let tasks: Vec<_> = urls.iter().map(|url| {
34-
let repository = Repository::new(&download_dir, url);
35-
let ignore_patterns = Arc::clone(&ignore_patterns);
36-
tokio::spawn(async move {
37-
process_single_repository(repository, no_headers, merge_files, ignore_patterns).await
36+
let tasks: Vec<_> = urls
37+
.iter()
38+
.map(|url| {
39+
let repository = Repository::new(&download_dir, url);
40+
let ignore_patterns = Arc::clone(&ignore_patterns);
41+
tokio::spawn(async move {
42+
process_single_repository(repository, no_headers, merge_files, ignore_patterns)
43+
.await
44+
})
3845
})
39-
}).collect();
46+
.collect();
4047

4148
let results = join_all(tasks).await;
4249

src/main.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{Parser};
1+
use clap::Parser;
22
use git2prompt::process_github_urls;
33
use std::path::PathBuf;
44

@@ -21,7 +21,11 @@ struct Args {
2121

2222
/// Path to a file containing a list of files/folders to ignore.
2323
/// Each line in the file should be a path or pattern to be excluded.
24-
#[clap(long, value_name = "PATH TO IGNORE FILE", default_value = ".git2promptignore")]
24+
#[clap(
25+
long,
26+
value_name = "PATH TO IGNORE FILE",
27+
default_value = ".git2promptignore"
28+
)]
2529
ignore_file: PathBuf,
2630
}
2731

@@ -35,7 +39,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3539
println!("Merge into a single output file: {}", args.merge_files);
3640

3741
// Call the library function to process the URLs, wrapping the ignore file path in Some
38-
match process_github_urls(args.urls, args.no_headers, args.merge_files, Some(args.ignore_file)).await {
42+
match process_github_urls(
43+
args.urls,
44+
args.no_headers,
45+
args.merge_files,
46+
Some(args.ignore_file),
47+
)
48+
.await
49+
{
3950
Ok(output_paths) => {
4051
println!("Processing complete. Output files created:");
4152
for path in output_paths {

src/processing.rs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::repository::Repository;
21
use crate::git_utils::clone_repository;
3-
use crate::io_utils::{write_content_to_file, get_language_alias};
2+
use crate::io_utils::{get_language_alias, write_content_to_file};
3+
use crate::repository::Repository;
4+
use std::path::PathBuf;
5+
use std::sync::Arc;
46
use tokio::fs;
57
use walkdir::WalkDir;
6-
use std::sync::Arc;
7-
use std::path::PathBuf;
88

99
/// Process a single repository: clone and process files
1010
pub async fn process_single_repository(
@@ -13,11 +13,19 @@ pub async fn process_single_repository(
1313
merge_files: bool,
1414
ignore_patterns: Arc<Vec<String>>,
1515
) -> Result<Repository, String> {
16-
println!("Preparing to clone {} to {:?}", repository.url, repository.path);
16+
println!(
17+
"Preparing to clone {} to {:?}",
18+
repository.url, repository.path
19+
);
1720
clone_repository(&repository).await?;
18-
println!("Successfully cloned {} to {:?}", repository.name, repository.path);
19-
20-
let content = process_repository_files(&repository.path, no_headers, merge_files, &ignore_patterns).await?;
21+
println!(
22+
"Successfully cloned {} to {:?}",
23+
repository.name, repository.path
24+
);
25+
26+
let content =
27+
process_repository_files(&repository.path, no_headers, merge_files, &ignore_patterns)
28+
.await?;
2129
repository.content = Some(content);
2230

2331
Ok(repository)
@@ -39,15 +47,18 @@ pub async fn process_repository_files(
3947
let with_headers = !no_headers;
4048

4149
if let Ok(content) = fs::read_to_string(path).await {
42-
let relative_path = path.strip_prefix(repo_path)
50+
let relative_path = path
51+
.strip_prefix(repo_path)
4352
.map_err(|e| format!("Failed to strip prefix: {}", e))?;
4453
let alias = get_language_alias(path);
4554

4655
if with_headers {
4756
if merge_files {
48-
combined_content.push_str(&format!("### File: {}\n", relative_path.display()));
57+
combined_content
58+
.push_str(&format!("### File: {}\n", relative_path.display()));
4959
} else {
50-
combined_content.push_str(&format!("## File: {}\n", relative_path.display()));
60+
combined_content
61+
.push_str(&format!("## File: {}\n", relative_path.display()));
5162
}
5263
}
5364
combined_content.push_str(&format!("```{}\n", alias));
@@ -97,23 +108,43 @@ pub async fn handle_results(
97108
}
98109

99110
/// Check if a file should be processed
100-
fn is_valid_file(path: &std::path::Path, repo_path: &std::path::Path, ignore_patterns: &[String]) -> bool {
101-
if path.components().any(|c| c.as_os_str() == ".git") { return false; }
102-
if ignore_due_to_pattern(path, repo_path, ignore_patterns) { return false; }
103-
if !path.is_file() { return false; }
111+
fn is_valid_file(
112+
path: &std::path::Path,
113+
repo_path: &std::path::Path,
114+
ignore_patterns: &[String],
115+
) -> bool {
116+
if path.components().any(|c| c.as_os_str() == ".git") {
117+
return false;
118+
}
119+
if ignore_due_to_pattern(path, repo_path, ignore_patterns) {
120+
return false;
121+
}
122+
if !path.is_file() {
123+
return false;
124+
}
104125

105126
if let Some(ext) = path.extension().and_then(|s| s.to_str())
106-
&& matches!(ext, "png" | "jpg" | "jpeg" | "gif" | "zip" | "tar" | "gz" | "bin" | "o" | "so" | "dll") {
127+
&& matches!(
128+
ext,
129+
"png" | "jpg" | "jpeg" | "gif" | "zip" | "tar" | "gz" | "bin" | "o" | "so" | "dll"
130+
)
131+
{
107132
return false;
108133
}
109134

110135
true
111136
}
112137

113-
fn ignore_due_to_pattern(path: &std::path::Path, repo_path: &std::path::Path, ignore_patterns: &[String]) -> bool {
138+
fn ignore_due_to_pattern(
139+
path: &std::path::Path,
140+
repo_path: &std::path::Path,
141+
ignore_patterns: &[String],
142+
) -> bool {
114143
let relative_path_str = match path.strip_prefix(repo_path) {
115144
Ok(p) => p.to_string_lossy().replace("\\", "/"),
116145
Err(_) => return false,
117146
};
118-
ignore_patterns.iter().any(|pattern| relative_path_str.contains(&pattern.replace("\\", "/")))
147+
ignore_patterns
148+
.iter()
149+
.any(|pattern| relative_path_str.contains(&pattern.replace("\\", "/")))
119150
}

src/repository.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ impl Repository {
1414
let name = repo_url.replace("/", "-");
1515
let path = base_download_dir.join(&name);
1616

17-
Self { url, name, path, content: None }
17+
Self {
18+
url,
19+
name,
20+
path,
21+
content: None,
22+
}
1823
}
1924
}

0 commit comments

Comments
 (0)