pub fn TomlEdit(File: &Path, Old: &str, Current: &str) -> Result<bool, Error>Expand description
Dynamically modifies specific name fields within a Cargo.toml file.
This function searches for and updates the following fields if they match the old name:
package.name- The package name identifierpackage.default-run- The default binary to runlib.name- The library namebin.name- The binary name (first occurrence)
The function preserves the existing file structure and ensures that only matching names are updated, preventing unintended modifications.
§Parameters
File- Path to the Cargo.toml file to modifyOld- The current name to search for and replaceCurrent- The new name to set
§Returns
Returns a Result<bool> indicating:
Ok(true)- The file was modified and savedOk(false)- No changes were made (either no matches or old == current)Err(BuildError)- An error occurred during modification
§Errors
BuildError::Io- If the file cannot be read or writtenBuildError::Edit- If the TOML document cannot be parsed or modified
§Behavior
- If
OldequalsCurrent, returnsOk(false)without modifying the file - Only updates fields that exactly match the old name
- Updates are atomic: the file is written only if at least one change occurs
- Logs all changes made at INFO level
§Example
use crate::Maintain::Source::Build::TomlEdit;
let path = PathBuf::from("Cargo.toml");
let modified = TomlEdit(&path, "Mountain", "Debug_Mountain")?;
if modified {
println!("Successfully updated Cargo.toml");
}