@@ -302,15 +302,15 @@ fn write_if_changed(path: &Path, content: &str, name: &str, verbose: u8) -> Resu
302302 }
303303 Ok ( false )
304304 } else {
305- fs :: write ( path, content)
305+ atomic_write ( path, content)
306306 . with_context ( || format ! ( "Failed to write {}: {}" , name, path. display( ) ) ) ?;
307307 if verbose > 0 {
308308 eprintln ! ( "Updated {}: {}" , name, path. display( ) ) ;
309309 }
310310 Ok ( true )
311311 }
312312 } else {
313- fs :: write ( path, content)
313+ atomic_write ( path, content)
314314 . with_context ( || format ! ( "Failed to write {}: {}" , name, path. display( ) ) ) ?;
315315 if verbose > 0 {
316316 eprintln ! ( "Created {}: {}" , name, path. display( ) ) ;
@@ -682,8 +682,7 @@ fn patch_settings_json_command(
682682 }
683683 }
684684
685- // Deep-merge hook
686- insert_hook_entry ( & mut root, hook_command) ;
685+ insert_hook_entry ( & mut root, hook_command) ?;
687686
688687 // Backup original
689688 if settings_path. exists ( ) {
@@ -748,38 +747,35 @@ fn clean_double_blanks(content: &str) -> String {
748747
749748/// Deep-merge RTK hook entry into settings.json
750749/// Creates hooks.PreToolUse structure if missing, preserves existing hooks
751- fn insert_hook_entry ( root : & mut serde_json:: Value , hook_command : & str ) {
752- // Ensure root is an object
750+ fn insert_hook_entry ( root : & mut serde_json:: Value , hook_command : & str ) -> Result < ( ) > {
753751 let root_obj = match root. as_object_mut ( ) {
754752 Some ( obj) => obj,
755753 None => {
756754 * root = serde_json:: json!( { } ) ;
757- root. as_object_mut ( )
758- . expect ( "Just created object, must succeed" )
755+ root. as_object_mut ( ) . expect ( "just-created json object" )
759756 }
760757 } ;
761758
762- // Use entry() API for idiomatic insertion
763759 let hooks = root_obj
764760 . entry ( "hooks" )
765761 . or_insert_with ( || serde_json:: json!( { } ) )
766762 . as_object_mut ( )
767- . expect ( "hooks must be an object" ) ;
763+ . context ( "hooks value is not an object" ) ? ;
768764
769765 let pre_tool_use = hooks
770766 . entry ( PRE_TOOL_USE_KEY )
771767 . or_insert_with ( || serde_json:: json!( [ ] ) )
772768 . as_array_mut ( )
773- . expect ( "PreToolUse must be an array" ) ;
769+ . context ( "PreToolUse value is not an array" ) ? ;
774770
775- // Append RTK hook entry
776771 pre_tool_use. push ( serde_json:: json!( {
777772 "matcher" : "Bash" ,
778773 "hooks" : [ {
779774 "type" : "command" ,
780775 "command" : hook_command
781776 } ]
782777 } ) ) ;
778+ Ok ( ( ) )
783779}
784780
785781/// Check if RTK hook is already present in settings.json
@@ -1622,8 +1618,7 @@ fn patch_cursor_hooks_json(path: &Path, verbose: u8) -> Result<bool> {
16221618 return Ok ( false ) ;
16231619 }
16241620
1625- // Insert the RTK preToolUse entry
1626- insert_cursor_hook_entry ( & mut root) ;
1621+ insert_cursor_hook_entry ( & mut root) ?;
16271622
16281623 // Backup if exists
16291624 if path. exists ( ) {
@@ -1664,35 +1659,34 @@ fn cursor_hook_already_present(root: &serde_json::Value) -> bool {
16641659}
16651660
16661661/// Insert RTK preToolUse entry into Cursor hooks.json
1667- fn insert_cursor_hook_entry ( root : & mut serde_json:: Value ) {
1662+ fn insert_cursor_hook_entry ( root : & mut serde_json:: Value ) -> Result < ( ) > {
16681663 let root_obj = match root. as_object_mut ( ) {
16691664 Some ( obj) => obj,
16701665 None => {
16711666 * root = serde_json:: json!( { "version" : 1 } ) ;
1672- root. as_object_mut ( )
1673- . expect ( "Just created object, must succeed" )
1667+ root. as_object_mut ( ) . expect ( "just-created json object" )
16741668 }
16751669 } ;
16761670
1677- // Ensure version key
16781671 root_obj. entry ( "version" ) . or_insert ( serde_json:: json!( 1 ) ) ;
16791672
16801673 let hooks = root_obj
16811674 . entry ( "hooks" )
16821675 . or_insert_with ( || serde_json:: json!( { } ) )
16831676 . as_object_mut ( )
1684- . expect ( "hooks must be an object" ) ;
1677+ . context ( "hooks value is not an object" ) ? ;
16851678
16861679 let pre_tool_use = hooks
16871680 . entry ( "preToolUse" )
16881681 . or_insert_with ( || serde_json:: json!( [ ] ) )
16891682 . as_array_mut ( )
1690- . expect ( "preToolUse must be an array" ) ;
1683+ . context ( "preToolUse value is not an array" ) ? ;
16911684
16921685 pre_tool_use. push ( serde_json:: json!( {
16931686 "command" : CURSOR_HOOK_COMMAND ,
16941687 "matcher" : "Shell"
16951688 } ) ) ;
1689+ Ok ( ( ) )
16961690}
16971691
16981692/// Remove Cursor RTK artifacts: hook script + hooks.json entry
@@ -2745,7 +2739,7 @@ More notes
27452739 let mut json_content = serde_json:: json!( { } ) ;
27462740 let hook_command = "/Users/test/.claude/hooks/rtk-rewrite.sh" ;
27472741
2748- insert_hook_entry ( & mut json_content, hook_command) ;
2742+ insert_hook_entry ( & mut json_content, hook_command) . unwrap ( ) ;
27492743
27502744 // Should create full structure
27512745 assert ! ( json_content. get( "hooks" ) . is_some( ) ) ;
@@ -2777,7 +2771,7 @@ More notes
27772771 } ) ;
27782772
27792773 let hook_command = "/Users/test/.claude/hooks/rtk-rewrite.sh" ;
2780- insert_hook_entry ( & mut json_content, hook_command) ;
2774+ insert_hook_entry ( & mut json_content, hook_command) . unwrap ( ) ;
27812775
27822776 let pre_tool_use = json_content[ "hooks" ] [ "PreToolUse" ] . as_array ( ) . unwrap ( ) ;
27832777 assert_eq ! ( pre_tool_use. len( ) , 2 ) ; // Should have both hooks
@@ -2800,7 +2794,7 @@ More notes
28002794 } ) ;
28012795
28022796 let hook_command = "/Users/test/.claude/hooks/rtk-rewrite.sh" ;
2803- insert_hook_entry ( & mut json_content, hook_command) ;
2797+ insert_hook_entry ( & mut json_content, hook_command) . unwrap ( ) ;
28042798
28052799 // Should preserve all other keys
28062800 assert_eq ! ( json_content[ "env" ] [ "PATH" ] , "/custom/path" ) ;
@@ -3003,7 +2997,7 @@ More notes
30032997 #[ test]
30042998 fn test_insert_cursor_hook_entry_empty ( ) {
30052999 let mut json_content = serde_json:: json!( { "version" : 1 } ) ;
3006- insert_cursor_hook_entry ( & mut json_content) ;
3000+ insert_cursor_hook_entry ( & mut json_content) . unwrap ( ) ;
30073001
30083002 let hooks = json_content[ "hooks" ] [ "preToolUse" ] . as_array ( ) . unwrap ( ) ;
30093003 assert_eq ! ( hooks. len( ) , 1 ) ;
@@ -3027,7 +3021,7 @@ More notes
30273021 }
30283022 } ) ;
30293023
3030- insert_cursor_hook_entry ( & mut json_content) ;
3024+ insert_cursor_hook_entry ( & mut json_content) . unwrap ( ) ;
30313025
30323026 let pre_tool_use = json_content[ "hooks" ] [ "preToolUse" ] . as_array ( ) . unwrap ( ) ;
30333027 assert_eq ! ( pre_tool_use. len( ) , 2 ) ;
0 commit comments