diff options
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 114 |
1 files changed, 57 insertions, 57 deletions
diff --git a/src/utils.rs b/src/utils.rs index 81bd63f..71181ab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,90 +1,90 @@ use colored::Colorize; +use std::fmt::Debug; use std::io::Write; use std::process::exit; pub fn log_warning(msg: &str) { - let msg = format!("⚠️ {}", msg).yellow(); - println!("{}", msg); + let msg = format!("⚠️ {}", msg).yellow(); + println!("{}", msg); } pub fn log_error(msg: &str) { - let msg = format!("❌ {}", msg).red(); - println!("{}", msg); + let msg = format!("❌ {}", msg).red(); + println!("{}", msg); } pub fn log_info(msg: &str) { - let msg = format!("ℹ️ {}", msg).blue(); - println!("{}", msg); + let msg = format!("ℹ️ {}", msg).blue(); + println!("{}", msg); } pub fn log_check(msg: &str) { - let msg = format!("✅ {}", msg).green(); - println!("{}", msg); + let msg = format!("✅ {}", msg).green(); + println!("{}", msg); } -pub fn assert_res(res : &Result<String, String>, error_msg : &str) { - if res.is_err() { - log_error(error_msg); - log_error(&format!("{:?}", res)); - exit(1); - } +pub fn assert_result<E>(res: &Result<String, E>, error_msg: &str) where E: Debug { + if res.is_err() { + log_error(error_msg); + log_error(&format!("{:?}", res)); + exit(1); + } } pub fn get_repo_name(repo: &str) -> String { - let mut chars = repo.chars().rev(); - let length = repo.chars().count(); - let mut last_i = 0; - let mut first_i = 0; + let mut chars = repo.chars().rev(); + let length = repo.chars().count(); + let mut last_i = 0; + let mut first_i = 0; - let mut i = length; + let mut i = length; - while i > 0 { - match chars.next() { - Some('.') => { - last_i = i-1; - } - Some('/') => { - first_i = i; - break; - } - _ => {} - } - i -= 1; - } + while i > 0 { + match chars.next() { + Some('.') => { + last_i = i - 1; + } + Some('/') => { + first_i = i; + break; + } + _ => {} + } + i -= 1; + } - if last_i == 0 { - last_i = length; - } + if last_i == 0 { + last_i = length; + } - let name = &repo[first_i..last_i]; - // TODO: Return a Result (may be error) - return name.to_string(); + let name = &repo[first_i..last_i]; + // TODO: Return a Result (may be error) + return name.to_string(); } -pub fn run_shell_command(command : &str, folder : &str, verbose : bool) -> Result<String, String> { - let output = std::process::Command:: - new("sh") - .current_dir(folder) - .stdin(std::process::Stdio::inherit()) +pub fn run_shell_command(command: &str, folder: &str, verbose: bool) -> Result<String, String> { + let output = std::process::Command::new("sh") + .current_dir(folder) + .stdin(std::process::Stdio::inherit()) .arg("-c") .arg(command) .output() .expect("Error running command."); - if verbose { - std::io::stdout().write_all(&output.stdout).unwrap(); - std::io::stderr().write_all(&output.stderr).unwrap(); - } + if verbose { + std::io::stdout().write_all(&output.stdout).unwrap(); + std::io::stderr().write_all(&output.stderr).unwrap(); + } - match output.status.success() { - true => { - let stdout_str = String::from_utf8(output.stdout).unwrap(); - return Ok(stdout_str); - } + match output.status.success() { + true => { + let stdout_str = String::from_utf8(output.stdout).unwrap(); + return Ok(stdout_str); + } - false => { - let stderr_str = String::from_utf8(output.stderr).unwrap(); - return Err(stderr_str); - } - } + false => { + let stderr_str = String::from_utf8(output.stderr).unwrap(); + return Err(stderr_str); + } + } } |