Skip to content

Commit 7bc0b8f

Browse files
committed
refactor: [#116] eliminate map_err calls using From trait implementations
Implemented From trait conversions to enable automatic error conversions using the ? operator, removing verbose map_err calls throughout handlers. check.rs changes: - Added impl From<CheckAllToolsError> for CheckError - Added impl From<CheckSpecificToolError> for CheckError - Added impl From<DetectionError> for CheckAllToolsError - Added impl From<ParseToolNameError> for CheckSpecificToolError - Added impl From<DetectionError> for CheckSpecificToolError - Simplified handle_check: removed map_err, added Ok(()) return - Simplified check_all_tools: manager.check_all()? without map_err - Simplified check_specific_tool: parse_tool_name()? and detector.is_installed()? without map_err list.rs changes: - Added impl From<DetectionError> for ListError - Simplified handle_list: manager.check_all()? without map_err All error conversions now happen automatically via From traits, enabling clean idiomatic Rust error handling with the ? operator.
1 parent 36f9627 commit 7bc0b8f

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

packages/dependency-installer/src/handlers/check.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ pub enum CheckError {
3030
},
3131
}
3232

33+
impl From<CheckAllToolsError> for CheckError {
34+
fn from(source: CheckAllToolsError) -> Self {
35+
Self::CheckAllFailed { source }
36+
}
37+
}
38+
39+
impl From<CheckSpecificToolError> for CheckError {
40+
fn from(source: CheckSpecificToolError) -> Self {
41+
Self::CheckSpecificFailed { source }
42+
}
43+
}
44+
3345
/// Errors that can occur when checking all tools
3446
#[derive(Debug, Error)]
3547
pub enum CheckAllToolsError {
@@ -55,6 +67,12 @@ pub enum CheckAllToolsError {
5567
},
5668
}
5769

70+
impl From<DetectionError> for CheckAllToolsError {
71+
fn from(source: DetectionError) -> Self {
72+
Self::DependencyCheckFailed { source }
73+
}
74+
}
75+
5876
/// Errors that can occur when checking a specific tool
5977
#[derive(Debug, Error)]
6078
pub enum CheckSpecificToolError {
@@ -87,6 +105,18 @@ pub enum CheckSpecificToolError {
87105
},
88106
}
89107

108+
impl From<ParseToolNameError> for CheckSpecificToolError {
109+
fn from(source: ParseToolNameError) -> Self {
110+
Self::ParseFailed { source }
111+
}
112+
}
113+
114+
impl From<DetectionError> for CheckSpecificToolError {
115+
fn from(source: DetectionError) -> Self {
116+
Self::DetectionFailed { source }
117+
}
118+
}
119+
90120
/// Errors that can occur when parsing tool names
91121
#[derive(Debug, Error)]
92122
pub enum ParseToolNameError {
@@ -113,19 +143,18 @@ pub enum ParseToolNameError {
113143
/// - Internal error occurs during dependency checking
114144
pub fn handle_check(manager: &DependencyManager, tool: Option<String>) -> Result<(), CheckError> {
115145
match tool {
116-
Some(tool_name) => check_specific_tool(manager, &tool_name)
117-
.map_err(|source| CheckError::CheckSpecificFailed { source }),
118-
None => check_all_tools(manager).map_err(|source| CheckError::CheckAllFailed { source }),
146+
Some(tool_name) => check_specific_tool(manager, &tool_name)?,
147+
None => check_all_tools(manager)?,
119148
}
149+
150+
Ok(())
120151
}
121152

122153
fn check_all_tools(manager: &DependencyManager) -> Result<(), CheckAllToolsError> {
123154
info!("Checking all dependencies");
124155
println!("Checking dependencies...\n");
125156

126-
let results = manager
127-
.check_all()
128-
.map_err(|source| CheckAllToolsError::DependencyCheckFailed { source })?;
157+
let results = manager.check_all()?;
129158

130159
let mut missing_count = 0;
131160

@@ -168,14 +197,11 @@ fn check_specific_tool(
168197
info!(tool = tool_name, "Checking specific tool");
169198

170199
// Parse tool name to Dependency enum
171-
let dep = parse_tool_name(tool_name)
172-
.map_err(|source| CheckSpecificToolError::ParseFailed { source })?;
200+
let dep = parse_tool_name(tool_name)?;
173201

174202
let detector = manager.get_detector(dep);
175203

176-
let installed = detector
177-
.is_installed()
178-
.map_err(|source| CheckSpecificToolError::DetectionFailed { source })?;
204+
let installed = detector.is_installed()?;
179205

180206
if installed {
181207
info!(tool = detector.name(), "Tool is installed");

packages/dependency-installer/src/handlers/list.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ pub enum ListError {
2222
},
2323
}
2424

25+
impl From<DetectionError> for ListError {
26+
fn from(source: DetectionError) -> Self {
27+
Self::DependencyCheckFailed { source }
28+
}
29+
}
30+
2531
/// Handle the list command
2632
///
2733
/// # Errors
@@ -31,9 +37,7 @@ pub fn handle_list(manager: &DependencyManager) -> Result<(), ListError> {
3137
info!("Listing all available tools");
3238
println!("Available tools:\n");
3339

34-
let results = manager
35-
.check_all()
36-
.map_err(|source| ListError::DependencyCheckFailed { source })?;
40+
let results = manager.check_all()?;
3741

3842
for result in results {
3943
let status = if result.installed {

0 commit comments

Comments
 (0)