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