Struct dev_utils::files::toml::TomlFile

source ·
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

source

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 the Cargo.toml file.
Returns

A CargoFile instance containing the Cargo.toml file path and structured data.

source

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 the Cargo.toml file 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);
    }
}
source

pub fn get_section_data( &self, section: &str ) -> Option<&HashMap<String, String>>

Gets the data for a specific section from the Cargo.toml file.

Arguments
  • section - The name of the section to retrieve.
Returns

An Option containing a reference to the section’s data as a HashMap.

source

pub fn get_section_attribute( &self, section: &str, attribute: &str ) -> Option<&String>

Gets a specific attribute from a section in the Cargo.toml file.

Arguments
  • section - The name of the section.
  • attribute - The name of the attribute within the section.
Returns

An Option containing a reference to the attribute’s value as a String.

Trait Implementations§

source§

impl Clone for TomlFile

source§

fn clone(&self) -> TomlFile

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TomlFile

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for TomlFile

source§

fn eq(&self, other: &TomlFile) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for TomlFile

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.