mod build;
mod fs;
pub mod http;
pub mod temp;
pub use build::*;
pub use fs::*;
use std::{
error::Error,
fmt::Write,
io,
path::{Path, PathBuf},
sync::LazyLock,
};
use anyhow::Result;
pub fn not_found_err(e: &(dyn Error + 'static)) -> bool {
matches!(e.downcast_ref::<io::Error>(), Some(e) if e.kind() == io::ErrorKind::NotFound)
}
pub trait Emojify {
fn emojify(&self) -> String;
}
impl Emojify for str {
fn emojify(&self) -> String {
let mut input = self;
let mut output = String::new();
while let Some((i, m, n, j)) = input
.find(':')
.map(|i| (i, i + 1))
.and_then(|(i, m)| input[m..].find(':').map(|x| (i, m, m + x, m + x + 1)))
{
match emojis::get_by_shortcode(&input[m..n]) {
Some(emoji) => {
write!(output, "{}", &input[..i]).unwrap();
write!(output, "{}", emoji.as_str()).unwrap();
input = &input[j..];
}
None => {
write!(output, "{}", &input[..n]).unwrap();
input = &input[n..];
}
}
}
write!(output, "{}", input).unwrap();
output
}
}
static HOME: LazyLock<PathBuf> = LazyLock::new(|| {
home::home_dir().expect("failed to determine the current user's home directory")
});
pub trait Shorten {
fn shorten(&self) -> Result<String>;
}
impl<T> Shorten for T
where
T: AsRef<Path>,
{
fn shorten(&self) -> Result<String> {
let s = match self.as_ref().strip_prefix(&*HOME) {
Ok(p) => Path::new("$HOME").join(p).to_str().map(|s| s.to_owned()),
Err(_) => self.as_ref().to_str().map(|s| s.to_owned()),
};
s.ok_or_else(|| anyhow::anyhow!("failed to convert path to string: {:?}", self.as_ref()))
}
}