use crate::result::Result;
use anyhow::anyhow;
use std::{fs, path::PathBuf};
pub fn home() -> PathBuf {
let home = dirs::home_dir().unwrap_or_else(|| ".".into()).join(".gear");
if !home.exists() {
fs::create_dir_all(&home).expect("Failed to create ~/.gear");
}
home
}
pub trait Hex {
fn to_vec(&self) -> Result<Vec<u8>>;
fn to_hash(&self) -> Result<[u8; 32]>;
}
impl<T> Hex for T
where
T: AsRef<str>,
{
fn to_vec(&self) -> Result<Vec<u8>> {
hex::decode(self.as_ref().trim_start_matches("0x")).map_err(Into::into)
}
fn to_hash(&self) -> Result<[u8; 32]> {
let hex = self.to_vec()?;
if hex.len() != 32 {
return Err(anyhow!("Incorrect id length").into());
}
let mut arr = [0; 32];
arr.copy_from_slice(&hex);
Ok(arr)
}
}