Skip to content

Commit cac2bf3

Browse files
unhappychoiceclaude
andcommitted
refactor: use unwrap_or_default instead of unwrap_or_else
Replace unwrap_or_else(Vec::new) with unwrap_or_default() for better readability and performance. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9b193cf commit cac2bf3

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

src/domain/services/challenge_generator/challenge_generator.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{
2-
chunk_splitter::ChunkSplitter,
3-
code_character_counter::CodeCharacterCounter,
2+
chunk_splitter::ChunkSplitter, code_character_counter::CodeCharacterCounter,
43
progress_tracker::ProgressTracker,
54
};
65
use crate::domain::models::{Challenge, CodeChunk, DifficultyLevel};
@@ -40,10 +39,10 @@ impl ChallengeGenerator {
4039
let mut valid_chunks: Vec<_> = chunks
4140
.into_iter()
4241
.filter(|chunk| {
43-
!chunk.content.trim().is_empty() &&
44-
chunk.start_line > 0 &&
45-
chunk.end_line > 0 &&
46-
chunk.start_line <= chunk.end_line
42+
!chunk.content.trim().is_empty()
43+
&& chunk.start_line > 0
44+
&& chunk.end_line > 0
45+
&& chunk.start_line <= chunk.end_line
4746
})
4847
.collect();
4948

@@ -56,7 +55,9 @@ impl ChallengeGenerator {
5655

5756
let chunk_challenges: Vec<Challenge> = valid_chunks
5857
.par_iter()
59-
.inspect(|_| { progress_tracker.increment_and_report(progress); })
58+
.inspect(|_| {
59+
progress_tracker.increment_and_report(progress);
60+
})
6061
.flat_map(|chunk| {
6162
let code_char_count = self.character_counter.count_code_characters(chunk);
6263

@@ -80,27 +81,27 @@ impl ChallengeGenerator {
8081
code_char_count: usize,
8182
) -> Vec<Challenge> {
8283
let (_, max_chars) = difficulty.char_limits();
83-
84+
8485
match (difficulty, code_char_count > max_chars) {
8586
(DifficultyLevel::Zen | DifficultyLevel::Wild, _) | (_, false) => {
8687
Challenge::from_chunk(chunk, Some(*difficulty))
8788
.map(|challenge| vec![challenge])
88-
.unwrap_or_else(Vec::new)
89-
}
90-
(_, true) => {
91-
self.chunk_splitter.split(chunk, difficulty)
92-
.map(|(truncated_content, adjusted_comment_ranges, end_line)| {
93-
vec![Challenge::from_content_and_chunk(
94-
truncated_content,
95-
chunk,
96-
chunk.start_line,
97-
end_line,
98-
&adjusted_comment_ranges,
99-
Some(*difficulty),
100-
)]
101-
})
102-
.unwrap_or_else(Vec::new)
89+
.unwrap_or_default()
10390
}
91+
(_, true) => self
92+
.chunk_splitter
93+
.split(chunk, difficulty)
94+
.map(|(truncated_content, adjusted_comment_ranges, end_line)| {
95+
vec![Challenge::from_content_and_chunk(
96+
truncated_content,
97+
chunk,
98+
chunk.start_line,
99+
end_line,
100+
&adjusted_comment_ranges,
101+
Some(*difficulty),
102+
)]
103+
})
104+
.unwrap_or_default(),
104105
}
105106
}
106107
}

src/domain/services/source_code_parser/source_code_parser.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::domain::models::Language;
22
use crate::domain::models::{CodeChunk, ExtractionOptions};
3-
use crate::domain::services::source_code_parser::ChunkExtractor;
43
use crate::domain::services::source_code_parser::parsers::parse_with_thread_local;
4+
use crate::domain::services::source_code_parser::ChunkExtractor;
55
use crate::infrastructure::git::LocalGitRepositoryClient;
66
use crate::presentation::game::models::StepType;
77
use crate::presentation::game::screens::loading_screen::ProgressReporter;
@@ -45,8 +45,14 @@ impl SourceCodeParser {
4545
Self::read_and_parse_file(&git_root, &path, language).into_par_iter()
4646
})
4747
.flat_map(|(tree, content, file_path, git_root, language)| {
48-
ChunkExtractor::extract_chunks_from_tree(&tree, &content, &file_path, &git_root, language.as_ref())
49-
.unwrap_or(Vec::new())
48+
ChunkExtractor::extract_chunks_from_tree(
49+
&tree,
50+
&content,
51+
&file_path,
52+
&git_root,
53+
language.as_ref(),
54+
)
55+
.unwrap_or_default()
5056
})
5157
.collect();
5258

@@ -63,9 +69,7 @@ impl SourceCodeParser {
6369
.first()
6470
.map(|(first_file, _)| first_file)
6571
.and_then(|path| LocalGitRepositoryClient::get_repository_root(path))
66-
.ok_or_else(|| {
67-
GitTypeError::ExtractionFailed("Git repository not found".to_string())
68-
})
72+
.ok_or_else(|| GitTypeError::ExtractionFailed("Git repository not found".to_string()))
6973
}
7074

7175
fn filter_and_sort_files(
@@ -111,11 +115,18 @@ impl SourceCodeParser {
111115
}
112116
}
113117

118+
#[allow(clippy::type_complexity)]
114119
fn read_and_parse_file(
115120
git_root: &Path,
116121
file_path: &Path,
117122
language: Box<dyn Language>,
118-
) -> Option<(tree_sitter::Tree, String, PathBuf, PathBuf, Box<dyn Language>)> {
123+
) -> Option<(
124+
tree_sitter::Tree,
125+
String,
126+
PathBuf,
127+
PathBuf,
128+
Box<dyn Language>,
129+
)> {
119130
let content = fs::read_to_string(file_path).ok()?;
120131
let tree = parse_with_thread_local(language.name(), &content)?;
121132

0 commit comments

Comments
 (0)