Skip to content

Commit

Permalink
Bug fix for loop regex in loop at storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Khoa Truong authored and Khoa Truong committed Jan 24, 2025
1 parent 0bdfabd commit 250a028
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/bin/hardware_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ use structopt::StructOpt;

#[derive(Debug)]
enum FileFormat {
TOML,
JSON,
Toml,
Json,
}

impl std::str::FromStr for FileFormat {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_uppercase().as_str() {
"TOML" => Ok(FileFormat::TOML),
"JSON" => Ok(FileFormat::JSON),
"TOML" => Ok(FileFormat::Toml),
"JSON" => Ok(FileFormat::Json),
_ => Err("File format must be either 'toml' or 'json'".to_string()),
}
}
Expand All @@ -42,8 +42,8 @@ impl std::str::FromStr for FileFormat {
impl std::fmt::Display for FileFormat {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
FileFormat::TOML => write!(f, "TOML"),
FileFormat::JSON => write!(f, "JSON"),
FileFormat::Toml => write!(f, "TOML"),
FileFormat::Json => write!(f, "JSON"),
}
}
}
Expand Down Expand Up @@ -250,15 +250,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
"{}_hardware_report.{}",
safe_filename,
match opt.file_format {
FileFormat::TOML => "toml",
FileFormat::JSON => "json",
FileFormat::Toml => "toml",
FileFormat::Json => "json",
}
);

// Convert to specified format and write to file
let output_string = match opt.file_format {
FileFormat::TOML => toml::to_string_pretty(&server_info)?,
FileFormat::JSON => serde_json::to_string_pretty(&server_info)?,
FileFormat::Toml => toml::to_string_pretty(&server_info)?,
FileFormat::Json => serde_json::to_string_pretty(&server_info)?,
};

std::fs::write(&output_filename, output_string)?;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,10 @@ impl ServerInfo {
/// Calculates total storage capacity
fn calculate_total_storage(storage: &StorageInfo) -> Result<String, Box<dyn Error>> {
let mut total_bytes: u64 = 0;
let re = Regex::new(r"(\d+(?:\.\d+)?)(B|K|M|G|T)")?;

for device in &storage.devices {
let size_str = device.size.replace(" ", "");
let re = Regex::new(r"(\d+(?:\.\d+)?)(B|K|M|G|T)")?;

if let Some(caps) = re.captures(&size_str) {
let value: f64 = caps[1].parse()?;
Expand Down

0 comments on commit 250a028

Please sign in to comment.