pub struct TomlFile {
pub path: PathBuf,
pub data: HashMap<String, HashMap<String, String>>,
}Expand description
This struct represents a *.toml file and provides methods to read and extract information from it.
Used to read and extract information from a Cargo.toml file.
Fields§
§path: PathBuf§data: HashMap<String, HashMap<String, String>>Implementations§
source§impl TomlFile
impl TomlFile
sourcepub fn new(path: &Path) -> Self
pub fn new(path: &Path) -> Self
Creates a new CargoFile instance by reading the specified Cargo.toml file.
This function initializes a CargoFile instance by reading the Cargo.toml file located at the given path.
It also extracts and structures the data within the Cargo.toml file.
Arguments
path- The path to theCargo.tomlfile.
Returns
A CargoFile instance containing the Cargo.toml file path and structured data.
sourcepub fn get_toml_data(
cargo_toml_path: &Path
) -> HashMap<String, HashMap<String, String>>
pub fn get_toml_data( cargo_toml_path: &Path ) -> HashMap<String, HashMap<String, String>>
Reads a Cargo.toml file, extracts information separated by sections, and returns it as a structured data.
This function parses a Cargo.toml file, extracting key-value pairs organized by sections (e.g., “package”, “dependencies”).
It also ignores comments (lines starting with #) and removes quotes from values.
Arguments
cargo_toml_path- The path to theCargo.tomlfile to be processed.
Returns
A HashMap where keys represent sections (e.g., “package”, “dependencies”) and values are
sub-maps containing key-value pairs for each section. The top-level keys in the HashMap correspond
to section names, and the associated sub-maps contain the key-value pairs within each section.
Example
use dev_utils::files::toml::CargoFile;
use std::collections::HashMap;
use std::path::Path;
let cargo_toml_path = Path::new("Cargo.toml");
let toml_data = CargoFile::get_cargo_toml_data(&cargo_toml_path);
if let Some(package_info) = toml_data.get("package") {
if let Some(name) = package_info.get("name") {
println!("Package name: {}", name);
}
}