use std::{collections::HashMap, path::Path};
use rusqlite::{Connection, Error, OpenFlags, Result, Row, Statement};
use crate::error::table::TableError;
pub trait Table {
fn from_row(row: &Row) -> Result<Self>
where
Self: Sized;
fn get(db: &Connection) -> Result<Statement, TableError>;
fn extract(item: Result<Result<Self, Error>, Error>) -> Result<Self, TableError>
where
Self: Sized;
}
pub trait Cacheable {
type K;
type V;
fn cache(db: &Connection) -> Result<HashMap<Self::K, Self::V>, TableError>;
}
pub trait Deduplicate {
type T;
fn dedupe(duplicated_data: &HashMap<i32, Self::T>) -> HashMap<i32, i32>;
}
pub trait Diagnostic {
fn run_diagnostic(db: &Connection);
}
pub fn get_connection(path: &Path) -> Result<Connection, TableError> {
if path.exists() {
return match Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY) {
Ok(res) => Ok(res),
Err(why) => Err(
TableError::CannotConnect(
format!("Unable to read from chat database: {}\nEnsure full disk access is enabled for your terminal emulator in System Settings > Security and Privacy > Full Disk Access", why)
)
),
};
}
Err(TableError::CannotConnect(format!(
"Database not found at {}",
&path.to_str().unwrap_or("Unknown")
)))
}
pub const HANDLE: &str = "handle";
pub const MESSAGE: &str = "message";
pub const CHAT: &str = "chat";
pub const ATTACHMENT: &str = "attachment";
pub const CHAT_MESSAGE_JOIN: &str = "chat_message_join";
pub const MESSAGE_ATTACHMENT_JOIN: &str = "message_attachment_join";
pub const CHAT_HANDLE_JOIN: &str = "chat_handle_join";
pub const MESSAGE_PAYLOAD: &str = "payload_data";
pub const MESSAGE_SUMMARY_INFO: &str = "message_summary_info";
pub const ATTRIBUTED_BODY: &str = "attributedBody";
pub const ME: &str = "Me";
pub const YOU: &str = "You";
pub const UNKNOWN: &str = "Unknown";
pub const DEFAULT_PATH: &str = "Library/Messages/chat.db";
pub const ORPHANED: &str = "orphaned";
pub const MAX_LENGTH: usize = 240;
pub const FITNESS_RECEIVER: &str = "$(kIMTranscriptPluginBreadcrumbTextReceiverIdentifier)";
pub const ATTACHMENTS_DIR: &str = "attachments";