@@ -555,7 +555,7 @@ def update_version_in_file(verbose: int, next_version_str: str, file: str, file_
555555 elif file == "setup.py" :
556556 update_file (str (file_path ), r"version=['\"].*?['\"]" , f"version='{ next_version_str } '" , verbose , show_diff = show_diff )
557557 elif file == "Cargo.toml" :
558- update_file (str (file_path ), r'version\s*=\s*".*?"' , f'version = " { next_version_str } "' , verbose , show_diff = show_diff )
558+ update_cargo_toml_version (str (file_path ), next_version_str , verbose , show_diff = show_diff )
559559 elif file in ("VERSION" , "VERSION.txt" ):
560560 update_file (str (file_path ), None , next_version_str , verbose , show_diff = show_diff )
561561
@@ -564,7 +564,7 @@ def update_file_in_root(next_version_str: str, verbose: int, root_path: Path, *,
564564 update_file (str (root_path / "package.json" ), r'"version":\s*".*?"' , f'"version": "{ next_version_str } "' , verbose , show_diff = show_diff )
565565 update_file (str (root_path / "pyproject.toml" ), r'version\s*=\s*".*?"' , f'version = "{ next_version_str } "' , verbose , show_diff = show_diff )
566566 update_file (str (root_path / "setup.py" ), r"version=['\"].*?['\"]" , f"version='{ next_version_str } '" , verbose , show_diff = show_diff )
567- update_file (str (root_path / "Cargo.toml" ), r'(?m)^version\s*=\s*".*?"' , f'version = " { next_version_str } "' , verbose , show_diff = show_diff )
567+ update_cargo_toml_version (str (root_path / "Cargo.toml" ), next_version_str , verbose , show_diff = show_diff )
568568 update_file (
569569 str (root_path / "build.gradle.kts" ),
570570 r'version\s*=\s*".*?"' ,
@@ -591,6 +591,37 @@ def update_file(filename: str, search_pattern: str | None, replace_text: str, ve
591591 f .write (new_content )
592592
593593
594+ def update_cargo_toml_version (filename : str , next_version_str : str , verbose : int , * , show_diff : bool = True ) -> None :
595+ """Update version in Cargo.toml, specifically in the [package] section only."""
596+ file_path = Path (filename )
597+ if not file_path .exists ():
598+ return
599+ if verbose > 0 :
600+ console .print (f"Updating { file_path } " )
601+
602+ with file_path .open () as f :
603+ content = f .read ()
604+
605+ # Use regex to match version in [package] section only
606+ # This pattern matches:
607+ # 1. [package] section header
608+ # 2. Any content until version = "..."
609+ # 3. Captures the version line to replace
610+ pattern = r'(\[package\].*?)(version\s*=\s*"[^"]*")'
611+
612+ def replace_version (match : re .Match [str ]) -> str :
613+ package_section = match .group (1 )
614+ return f'{ package_section } version = "{ next_version_str } "'
615+
616+ new_content = re .sub (pattern , replace_version , content , flags = re .DOTALL )
617+
618+ if show_diff :
619+ show_file_diff (content , new_content , str (file_path ))
620+
621+ with file_path .open ("w" ) as f :
622+ f .write (new_content )
623+
624+
594625def show_file_diff (old_content : str , new_content : str , filename : str ) -> None :
595626 old_lines = old_content .splitlines ()
596627 new_lines = new_content .splitlines ()
0 commit comments