@@ -10,14 +10,13 @@ use std::{
1010} ;
1111
1212// non-std crates
13- use anyhow:: { Context , Result , anyhow} ;
1413use clang_tools_manager:: utils:: normalize_path;
1514use regex:: Regex ;
1615use serde:: Deserialize ;
1716
1817// project-specific modules/crates
1918use super :: MakeSuggestions ;
20- use crate :: { cli:: ClangParams , common_fs:: FileObj } ;
19+ use crate :: { cli:: ClangParams , common_fs:: FileObj , error :: ClangCaptureError } ;
2120
2221/// Used to deserialize a json compilation database's translation unit.
2322///
@@ -145,17 +144,18 @@ const NOTE_HEADER: &str = r"^(.+):(\d+):(\d+):\s(\w+):(.*)\[([a-zA-Z\d\-\.]+),?[
145144fn parse_tidy_output (
146145 tidy_stdout : & [ u8 ] ,
147146 database_json : & Option < Vec < CompilationUnit > > ,
148- ) -> Result < TidyAdvice > {
149- let note_header = Regex :: new ( NOTE_HEADER )
150- . with_context ( || "Failed to compile RegExp pattern for note header" ) ?;
151- let fixed_note = Regex :: new ( r"^.+:(\d+):\d+:\snote: FIX-IT applied suggested code changes$" )
152- . with_context ( || "Failed to compile RegExp pattern for fixed note" ) ?;
147+ ) -> Result < TidyAdvice , ClangCaptureError > {
148+ let note_header = Regex :: new ( NOTE_HEADER ) ?;
149+ let fixed_note = Regex :: new ( r"^.+:(\d+):\d+:\snote: FIX-IT applied suggested code changes$" ) ?;
153150 let mut found_fix = false ;
154151 let mut notification = None ;
155152 let mut result = Vec :: new ( ) ;
156- let cur_dir = current_dir ( ) . with_context ( || "Failed to access current working directory" ) ?;
153+ let cur_dir = current_dir ( ) . map_err ( ClangCaptureError :: UnknownWorkingDirectory ) ?;
157154 for line in String :: from_utf8 ( tidy_stdout. to_vec ( ) )
158- . with_context ( || "Failed to convert clang-tidy stdout to UTF-8 string" ) ?
155+ . map_err ( |e| ClangCaptureError :: NonUtf8Output {
156+ task : "convert clang-tidy stdout" . to_string ( ) ,
157+ source : e,
158+ } ) ?
159159 . lines ( )
160160 {
161161 if let Some ( captured) = note_header. captures ( line) {
@@ -267,11 +267,11 @@ pub fn tally_tidy_advice(files: &[Arc<Mutex<FileObj>>]) -> Result<u64, String> {
267267pub fn run_clang_tidy (
268268 file : & mut MutexGuard < FileObj > ,
269269 clang_params : & ClangParams ,
270- ) -> Result < Vec < ( log:: Level , std :: string :: String ) > > {
270+ ) -> Result < Vec < ( log:: Level , String ) > , ClangCaptureError > {
271271 let cmd_path = clang_params
272272 . clang_tidy_command
273273 . as_ref ( )
274- . ok_or ( anyhow ! ( "clang-tidy command not located " ) ) ?;
274+ . ok_or ( ClangCaptureError :: ToolPathUnknown ( "clang-tidy" ) ) ?;
275275 let mut cmd = Command :: new ( cmd_path) ;
276276 let mut logs = vec ! [ ] ;
277277 if !clang_params. tidy_checks . is_empty ( ) {
@@ -300,12 +300,12 @@ pub fn run_clang_tidy(
300300 None
301301 } else {
302302 cmd. arg ( "--fix-errors" ) ;
303- Some ( fs :: read_to_string ( & file . name ) . with_context ( || {
304- format ! (
305- "Failed to cache file's original content before applying clang-tidy changes: {}" ,
306- file_name . clone ( )
307- )
308- } ) ? )
303+ Some (
304+ fs :: read_to_string ( & file . name ) . map_err ( |e| ClangCaptureError :: ReadFileFailed {
305+ file_name : file_name . clone ( ) ,
306+ source : e ,
307+ } ) ? ,
308+ )
309309 } ;
310310 if !clang_params. style . is_empty ( ) {
311311 cmd. args ( [ "--format-style" , clang_params. style . as_str ( ) ] ) ;
@@ -322,12 +322,12 @@ pub fn run_clang_tidy(
322322 . join( " " )
323323 ) ,
324324 ) ) ;
325- let output = cmd. output ( ) . with_context ( || {
326- format ! (
327- "Failed to execute clang-tidy on file: {}" ,
328- file_name . clone ( )
329- )
330- } ) ?;
325+ let output = cmd
326+ . output ( )
327+ . map_err ( |e| ClangCaptureError :: FailedToRunCommand {
328+ task : format ! ( "execute clang-tidy on file {file_name}" ) ,
329+ source : e ,
330+ } ) ?;
331331 logs. push ( (
332332 log:: Level :: Debug ,
333333 format ! (
@@ -354,13 +354,20 @@ pub fn run_clang_tidy(
354354 if let Some ( tidy_advice) = & mut file. tidy_advice {
355355 // cache file changes in a buffer and restore the original contents for further analysis
356356 tidy_advice. patched =
357- Some ( fs:: read ( & file_name) . with_context ( || {
358- format ! ( "Failed to read changes from clang-tidy: {file_name}" )
359- } ) ?) ;
357+ Some (
358+ fs:: read ( & file_name) . map_err ( |e| ClangCaptureError :: ReadFileFailed {
359+ file_name : file_name. clone ( ) ,
360+ source : e,
361+ } ) ?,
362+ ) ;
360363 }
361364 // original_content is guaranteed to be Some() value at this point
362- fs:: write ( & file_name, original_content)
363- . with_context ( || format ! ( "Failed to restore file's original content: {file_name}" ) ) ?;
365+ fs:: write ( & file_name, original_content) . map_err ( |e| {
366+ ClangCaptureError :: WriteFileFailed {
367+ file_name,
368+ source : e,
369+ }
370+ } ) ?;
364371 }
365372 Ok ( logs)
366373}
0 commit comments