41 lines
1.4 KiB
Rust
41 lines
1.4 KiB
Rust
use crate::db::pool::Connection;
|
|
|
|
pub fn list(conn: &Connection) -> Result<Vec<String>, String> {
|
|
let mut stmt = conn
|
|
.prepare(crate::dbq!(db_trusted_senders::Q_001))
|
|
.map_err(|e| format!("Failed to prepare trusted sender list: {e}"))?;
|
|
let rows = stmt
|
|
.query_map([], |row| row.get::<_, String>(0))
|
|
.map_err(|e| format!("Failed to query trusted senders: {e}"))?;
|
|
rows.collect::<Result<Vec<_>, _>>()
|
|
.map_err(|e| format!("Failed to read trusted sender: {e}"))
|
|
}
|
|
|
|
pub fn add(conn: &Connection, email: &str) -> Result<(), String> {
|
|
conn.execute(crate::dbq!(db_trusted_senders::Q_002), [email])
|
|
.map_err(|e| format!("Failed to trust sender: {e}"))?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn remove(conn: &Connection, email: &str) -> Result<(), String> {
|
|
conn.execute(crate::dbq!(db_trusted_senders::Q_003), [email])
|
|
.map_err(|e| format!("Failed to remove trusted sender: {e}"))?;
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::db::pool::open_test_db;
|
|
|
|
#[test]
|
|
fn trusted_senders_roundtrip_case_insensitive() {
|
|
let conn = open_test_db();
|
|
add(&conn, "Alice@Example.com").unwrap();
|
|
add(&conn, "alice@example.com").unwrap();
|
|
assert_eq!(list(&conn).unwrap().len(), 1);
|
|
remove(&conn, "ALICE@example.com").unwrap();
|
|
assert!(list(&conn).unwrap().is_empty());
|
|
}
|
|
}
|