mirror of
https://mzte.de/git/LordMZTE/brevo
synced 2024-05-16 14:03:45 +02:00
43 lines
1 KiB
Rust
43 lines
1 KiB
Rust
use std::sync::Arc;
|
|
|
|
use crate::{handlers::BrevoReject, Brevo};
|
|
use rand::seq::IteratorRandom;
|
|
use warp::{reject, Rejection};
|
|
|
|
#[macro_export]
|
|
macro_rules! sqlargs {
|
|
($($item:expr),+) => {{
|
|
use sqlx::Arguments;
|
|
let mut args = sqlx::mysql::MySqlArguments::default();
|
|
$(
|
|
args.add($item);
|
|
)*
|
|
args
|
|
}};
|
|
}
|
|
|
|
pub fn sql_reject(e: sqlx::Error) -> Rejection {
|
|
reject::custom(BrevoReject::SqlError(e))
|
|
}
|
|
|
|
pub fn render_reject(err: tera::Error) -> Rejection {
|
|
reject::custom(BrevoReject::RenderFail(err))
|
|
}
|
|
|
|
pub fn make_link(brevo: Arc<Brevo>, id: &str) -> Result<String, Rejection> {
|
|
brevo
|
|
.config
|
|
.base_url
|
|
.join(id)
|
|
.map(String::from)
|
|
.map_err(|e| reject::custom(BrevoReject::UrlParseError(e)))
|
|
}
|
|
|
|
pub async fn gen_id(brevo: Arc<Brevo>) -> String {
|
|
"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
.chars()
|
|
.choose_multiple(&mut *brevo.rng.lock().await, brevo.config.link_len as usize)
|
|
.into_iter()
|
|
.collect()
|
|
}
|