Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chore] Refactor .unwrap() with Match in /ol/util folder #1257

Open
wants to merge 2 commits into
base: v6
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 88 additions & 55 deletions ol/util/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ use std::io::{Read, Write};
use std::path::PathBuf;

fn main() {
let node_home = dirs::home_dir().unwrap().join(NODE_HOME);
let config_file = node_home.join(CONFIG_FILE);

migrate_0l_toml(config_file, node_home);
match dirs::home_dir() {
Some(home_dir) => {
let node_home = home_dir.join(NODE_HOME);
let config_file = node_home.join(CONFIG_FILE);
migrate_0l_toml(config_file, node_home);
}
None => {
eprintln!("Failed to get the home directory");
}
}
}

fn migrate_0l_toml(config_file: PathBuf, node_home: PathBuf) {
Expand All @@ -39,28 +45,29 @@ fn migrate_0l_toml(config_file: PathBuf, node_home: PathBuf) {

// step 2: update all attributes

// ---------------------- udate [workspace] config start ----------------------
// ---------------------- update [workspace] config start ----------------------
let default_db_path = node_home.join("db").as_path().display().to_string();
let default_source_path = dirs::home_dir()
.unwrap()
.join("libra")
.as_path()
.display()
.to_string();
let default_source_path = match dirs::home_dir() {
Some(home) => home.join("libra").as_path().display().to_string(),
None => {
eprintln!("Failed to get the home directory");
return;
}
};
add_or_update_s(&config_file, "workspace", "db_path", default_db_path);
add_or_update_s(
&config_file,
"workspace",
"source_path",
default_source_path,
);
// ---------------------- udate [workspace] config finished ----------------------
// ---------------------- update [workspace] config finished ----------------------

// ---------------------- udate [chain_info] config start ----------------------
// ---------------------- update [chain_info] config start ----------------------
add_or_update_n(&config_file, "chain_info", "base_epoch", 0);
// ---------------------- udate [workspace] config finished ----------------------
// ---------------------- update [workspace] config finished ----------------------

// ---------------------- udate the tx costs config start ----------------------
// ---------------------- update the tx costs config start ----------------------
rename_section(
&config_file,
"tx_configs.miner_txs",
Expand Down Expand Up @@ -143,7 +150,7 @@ fn migrate_0l_toml(config_file: PathBuf, node_home: PathBuf) {
1000,
);

// ---------------------- udate the tx costs config finished ----------------------
// ---------------------- update the tx costs config finished ----------------------
}

/// add a new section in case it does not exist already
Expand All @@ -158,7 +165,13 @@ fn migrate_0l_toml(config_file: PathBuf, node_home: PathBuf) {
pub fn add_section(filename: &PathBuf, section: &str) {
let mut section_exists = false;

let my_section_re = Regex::new(&format!(r"^\[{}\]$", section).as_str()).unwrap();
let my_section_re = match Regex::new(&format!(r"^\[{}\]$", section).as_str()) {
Ok(regex) => regex,
Err(e) => {
eprintln!("Error creating regex: {}", e);
return;
}
};

// round 1: check if section already exists
let file_content = read_file(&filename);
Expand All @@ -179,14 +192,12 @@ pub fn add_section(filename: &PathBuf, section: &str) {
};

for line in file_content.lines() {
match file.write_fmt(format_args!("{}\n", &line)) {
Err(why) => println!("writing to file failed {:?}", why),
_ => (),
if let Err(why) = file.write_fmt(format_args!("{}\n", &line)) {
println!("writing to file failed {:?}", why)
}
}
match file.write_fmt(format_args!("\n[{}]\n", &section)) {
Err(why) => println!("writing to file failed {:?}", why),
_ => (),
if let Err(why) = file.write_fmt(format_args!("\n[{}]\n", &section)) {
println!("writing to file failed {:?}", why)
}
println!("{:?}: added section [{}]", &filename, &section);
}
Expand All @@ -203,8 +214,20 @@ pub fn add_section(filename: &PathBuf, section: &str) {
pub fn rename_section(filename: &PathBuf, old_section_name: &str, new_section_name: &str) {
let mut section_exists = false;

let old_section_re = Regex::new(&format!(r"^\[{}\]$", old_section_name).as_str()).unwrap();
let new_section_re = Regex::new(&format!(r"^\[{}\]$", new_section_name).as_str()).unwrap();
let old_section_re = match Regex::new(&format!(r"^\[{}\]$", old_section_name).as_str()) {
Ok(regex) => regex,
Err(e) => {
eprintln!("Error creating regex for old section name: {}", e);
return;
}
};
let new_section_re = match Regex::new(&format!(r"^\[{}\]$", new_section_name).as_str()) {
Ok(regex) => regex,
Err(e) => {
eprintln!("Error creating regex for new section name: {}", e);
return;
}
};

// round 1: check if section already exists
let file_content = read_file(&filename);
Expand All @@ -226,15 +249,11 @@ pub fn rename_section(filename: &PathBuf, old_section_name: &str, new_section_na

for line in file_content.lines() {
if old_section_re.is_match(&line) {
match file.write_fmt(format_args!("[{}]\n", &new_section_name)) {
Err(why) => println!("writing to file failed {:?}", why),
_ => (),
}
} else {
match file.write_fmt(format_args!("{}\n", &line)) {
Err(why) => println!("writing to file failed {:?}", why),
_ => (),
if let Err(why) = file.write_fmt(format_args!("[{}]\n", &new_section_name)) {
println!("writing to file failed {:?}", why)
}
} else if let Err(why) = file.write_fmt(format_args!("{}\n", &line)) {
println!("writing to file failed {:?}", why)
}
}

Expand Down Expand Up @@ -275,9 +294,27 @@ pub fn add_or_update(filename: &PathBuf, section: &str, attribute: &str, value:
let mut in_my_section = false;
let mut attribute_exists = false;

let any_section_start_re = Regex::new(r"^\[.*\]$").unwrap();
let my_section_re = Regex::new(&format!(r"^\[{}\]$", section).as_str()).unwrap();
let my_attribute_re = Regex::new(&format!(r"^{}[ \t]*=.*$", attribute).as_str()).unwrap();
let any_section_start_re = match Regex::new(r"^\[.*\]$") {
Ok(regex) => regex,
Err(e) => {
eprintln!("Error creating regex for any section start: {}", e);
return;
}
};
let my_section_re = match Regex::new(&format!(r"^\[{}\]$", section).as_str()) {
Ok(regex) => regex,
Err(e) => {
eprintln!("Error creating regex for section: {}", e);
return;
}
};
let my_attribute_re = match Regex::new(&format!(r"^{}[ \t]*=.*$", attribute).as_str()) {
Ok(regex) => regex,
Err(e) => {
eprintln!("Error creating regex for attribute: {}", e);
return;
}
};

// round 1: check if attribute already exists
let file_content = read_file(&filename);
Expand All @@ -299,9 +336,8 @@ pub fn add_or_update(filename: &PathBuf, section: &str, attribute: &str, value:
for line in file_content.lines() {
if any_section_start_re.is_match(&line) {
in_my_section = my_section_re.is_match(&line);
match file.write_fmt(format_args!("{}\n", &line)) {
Err(why) => println!("writing to file failed {:?}", why),
_ => (),
if let Err(why) = file.write_fmt(format_args!("{}\n", &line)) {
println!("writing to file failed {:?}", why)
}
if in_my_section && !attribute_exists {
// add the new attribute and value to start of section
Expand All @@ -313,22 +349,19 @@ pub fn add_or_update(filename: &PathBuf, section: &str, attribute: &str, value:
),
}
}
} else if in_my_section && my_attribute_re.is_match(&line) {
// update the value
match file.write_fmt(format_args!("{} = {}\n", attribute, value)) {
Err(why) => println!("writing to file failed {:?}", why),
_ => println!(
"{:?}: updated property [{}]/{} to {}",
&filename, &section, &attribute, &value
),
}
} else {
if in_my_section && my_attribute_re.is_match(&line) {
// update the value
match file.write_fmt(format_args!("{} = {}\n", attribute, value)) {
Err(why) => println!("writing to file failed {:?}", why),
_ => println!(
"{:?}: updated property [{}]/{} to {}",
&filename, &section, &attribute, &value
),
}
} else {
// otherwise just write the original attribute and value
match file.write_fmt(format_args!("{}\n", &line)) {
Err(why) => println!("writing to file failed {:?}", why),
_ => (),
}
// otherwise just write the original attribute and value
if let Err(why) = file.write_fmt(format_args!("{}\n", &line)) {
println!("writing to file failed {:?}", why)
}
}
}
Expand Down Expand Up @@ -372,6 +405,6 @@ fn read_file(filename: &PathBuf) -> String {
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("unable to read {:?} - {}", filename, why),
Ok(_) => return s,
Ok(_) => s,
}
}