TomlEdit

Function TomlEdit 

Source
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 identifier
  • package.default-run - The default binary to run
  • lib.name - The library name
  • bin.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 modify
  • Old - The current name to search for and replace
  • Current - The new name to set

§Returns

Returns a Result<bool> indicating:

  • Ok(true) - The file was modified and saved
  • Ok(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 written
  • BuildError::Edit - If the TOML document cannot be parsed or modified

§Behavior

  • If Old equals Current, returns Ok(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");
}