refactor: ️ Use anyhow::context to improve err message

This commit is contained in:
Sergio Gasquez 2022-10-10 08:52:01 +02:00
parent ffc2611d41
commit 6a13fec1b9

View File

@ -1,5 +1,5 @@
use crate::emoji; use crate::emoji;
use anyhow::{bail, Result}; use anyhow::{Context, Result};
use guess_host_triple::guess_host_triple; use guess_host_triple::guess_host_triple;
use std::str::FromStr; use std::str::FromStr;
use strum::Display; use strum::Display;
@ -29,16 +29,14 @@ pub enum HostTriple {
/// Parse the host triple if specified, otherwise guess it. /// Parse the host triple if specified, otherwise guess it.
pub fn get_host_triple(host_triple_arg: Option<String>) -> Result<HostTriple> { pub fn get_host_triple(host_triple_arg: Option<String>) -> Result<HostTriple> {
let host_triple = match host_triple_arg { if let Some(host_triple_arg) = host_triple_arg {
Some(host_triple_string) => match FromStr::from_str(&host_triple_string) { HostTriple::from_str(&host_triple_arg).context(format!(
Ok(host_triple) => host_triple, "{} Host triple '{}' is not supported.",
Err(_) => bail!( emoji::ERROR,
"{} Host triple '{}' is not supported.", host_triple_arg,
emoji::ERROR, ))
host_triple_string } else {
), HostTriple::from_str(guess_host_triple().unwrap())
}, .context(format!("{} Unable to guess host triple.", emoji::ERROR,))
None => HostTriple::from_str(guess_host_triple().unwrap()).unwrap(), }
};
Ok(host_triple)
} }