eva/src/utils.rs

48 lines
1.1 KiB
Rust

//! # Uncategorized useful functionality
use eyre::Context as _;
use std::{fmt, path::PathBuf};
use crate::data;
#[data(crate = crate, not(Debug), display("<secret>"))]
pub enum SecretString {
Plaintext(String),
File(PathBuf),
}
impl SecretString {
pub fn read(&self) -> eyre::Result<String> {
match self {
Self::Plaintext(t) => Ok(t.clone()),
Self::File(f) => std::fs::read_to_string(f).wrap_err("failed to read secret string"),
}
}
}
impl fmt::Debug for SecretString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}
/// Hint that this branch is cold.
#[inline(always)]
pub const fn cold<T>(v: T) -> T {
v
}
/// Indicate that this value is unlikely to be true. Hint to
/// the compiler.
#[inline(always)]
pub const fn unlikely(v: bool) -> bool {
if v { cold(true) } else { false }
}
/// Indicate that this value is likely to be true. Hint to
/// the compiler.
#[inline(always)]
pub const fn likely(v: bool) -> bool {
if v { true } else { cold(false) }
}