imessage_database/error/
table.rs

1/*!
2 Errors that can happen when extracting data from a `SQLite` table.
3*/
4
5use std::{
6    fmt::{Display, Formatter, Result},
7    path::PathBuf,
8};
9
10/// Errors that can happen when extracting data from a `SQLite` table
11#[derive(Debug)]
12pub enum TableError {
13    /// Error when querying the table
14    QueryError(rusqlite::Error),
15    /// Error when connecting to the database
16    CannotConnect(TableConnectError),
17    /// Error when reading from the database file
18    CannotRead(std::io::Error),
19}
20
21/// Reasons why the database could not be connected to or read from
22#[derive(Debug)]
23pub enum TableConnectError {
24    /// The database file could not be opened due to lack of full disk access
25    Permissions(rusqlite::Error),
26    /// The database file is not a valid `SQLite` database
27    NotAFile(PathBuf),
28    /// The database file does not exist
29    DoesNotExist(PathBuf),
30    /// Not a backup root directory
31    NotBackupRoot,
32}
33
34impl Display for TableError {
35    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
36        match self {
37            TableError::CannotConnect(why) => match why {
38                TableConnectError::Permissions(why) => write!(
39                    fmt,
40                    "Unable to read from chat database: {why}\nEnsure full disk access is enabled for your terminal emulator in System Settings > Privacy & Security > Full Disk Access"
41                ),
42                TableConnectError::NotAFile(path) => {
43                    write!(
44                        fmt,
45                        "Specified path `{}` is not a valid SQLite database file!",
46                        path.to_string_lossy()
47                    )
48                }
49                TableConnectError::DoesNotExist(path) => {
50                    write!(
51                        fmt,
52                        "Database file `{}` does not exist at the specified path!",
53                        path.to_string_lossy()
54                    )
55                }
56                TableConnectError::NotBackupRoot => write!(
57                    fmt,
58                    "The path provided points to a database inside of an iOS backup, not the root of the backup."
59                ),
60            },
61            TableError::CannotRead(why) => write!(fmt, "Cannot read from filesystem: {why}"),
62            TableError::QueryError(error) => write!(fmt, "Failed to query table: {error}"),
63        }
64    }
65}
66
67impl From<std::io::Error> for TableError {
68    fn from(err: std::io::Error) -> Self {
69        TableError::CannotRead(err)
70    }
71}
72
73impl From<rusqlite::Error> for TableError {
74    fn from(err: rusqlite::Error) -> Self {
75        TableError::QueryError(err)
76    }
77}