mirror of
https://github.com/lune-org/lune.git
synced 2025-04-11 22:10:53 +01:00
feat: reduce boilerplate using macro (thanks, dekkonot\!)
This commit is contained in:
parent
12d23e7e1b
commit
7fd5e60a8b
1 changed files with 42 additions and 35 deletions
|
@ -8,18 +8,51 @@ use std::sync::Mutex;
|
||||||
|
|
||||||
// TODO: Proper error handling, remove unwraps
|
// TODO: Proper error handling, remove unwraps
|
||||||
|
|
||||||
#[derive(Clone)]
|
macro_rules! impl_hash_algo {
|
||||||
pub struct Crypto {
|
($($algo:ident => $Type:ty),*) => {
|
||||||
algo: Arc<Mutex<CryptoAlgo>>,
|
#[derive(Clone)]
|
||||||
|
pub enum CryptoAlgo {
|
||||||
|
$(
|
||||||
|
$algo(Box<$Type>),
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CryptoAlgo {
|
||||||
|
pub fn update(&mut self, data: impl AsRef<[u8]>) {
|
||||||
|
match self {
|
||||||
|
$(
|
||||||
|
Self::$algo(hasher) => hasher.update(data),
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn digest(&mut self, encoding: EncodingKind) -> Result<String> {
|
||||||
|
let computed = match self {
|
||||||
|
$(
|
||||||
|
Self::$algo(hasher) => hasher.clone().finalize_reset().to_vec(),
|
||||||
|
)*
|
||||||
|
};
|
||||||
|
|
||||||
|
match encoding {
|
||||||
|
EncodingKind::Utf8 => String::from_utf8(computed).map_err(anyhow::Error::from),
|
||||||
|
EncodingKind::Base64 => Ok(Base64::STANDARD.encode(computed)),
|
||||||
|
EncodingKind::Hex => Ok(hex::encode(&computed)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// enum CryptoAlgo
|
||||||
|
impl_hash_algo! {
|
||||||
|
Sha1 => sha1::Sha1,
|
||||||
|
Sha256 => sha2::Sha256,
|
||||||
|
Sha512 => sha2::Sha512
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum CryptoAlgo {
|
pub struct Crypto {
|
||||||
Sha1(Box<sha1::Sha1>),
|
algo: Arc<Mutex<CryptoAlgo>>,
|
||||||
Sha256(Box<sha2::Sha256>),
|
|
||||||
Sha512(Box<sha2::Sha512>),
|
|
||||||
// Blake2(Box<T>),
|
|
||||||
// Md5(Box<T>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialOrd, PartialEq, Ord, Eq)]
|
#[derive(PartialOrd, PartialEq, Ord, Eq)]
|
||||||
|
@ -67,32 +100,6 @@ impl FromLua<'_> for EncodingKind {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CryptoAlgo {
|
|
||||||
// TODO: Replace boilerplate using a macro
|
|
||||||
|
|
||||||
pub fn update(&mut self, content: impl AsRef<[u8]>) {
|
|
||||||
match self {
|
|
||||||
CryptoAlgo::Sha1(hasher) => hasher.update(content),
|
|
||||||
CryptoAlgo::Sha256(hasher) => hasher.update(content),
|
|
||||||
CryptoAlgo::Sha512(hasher) => hasher.update(content),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn digest(&mut self, encoding: EncodingKind) -> Result<String> {
|
|
||||||
let computed: Vec<u8> = match self {
|
|
||||||
CryptoAlgo::Sha1(hasher) => hasher.clone().finalize().to_vec(),
|
|
||||||
CryptoAlgo::Sha256(hasher) => hasher.clone().finalize().to_vec(),
|
|
||||||
CryptoAlgo::Sha512(hasher) => hasher.clone().finalize().to_vec(),
|
|
||||||
};
|
|
||||||
|
|
||||||
match encoding {
|
|
||||||
EncodingKind::Utf8 => String::from_utf8(computed).map_err(anyhow::Error::from),
|
|
||||||
EncodingKind::Base64 => Ok(Base64::STANDARD.encode(computed)),
|
|
||||||
EncodingKind::Hex => Ok(hex::encode(&computed)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Crypto {
|
impl Crypto {
|
||||||
pub fn sha1<T: ToString>(content: Option<T>) -> Crypto {
|
pub fn sha1<T: ToString>(content: Option<T>) -> Crypto {
|
||||||
let constructed = Self {
|
let constructed = Self {
|
||||||
|
|
Loading…
Add table
Reference in a new issue