Skip to main content

demesne_postgres/
error.rs

1use std::fmt;
2
3/// Why a live database could not be reached or read.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct Error {
6    kind: Kind,
7    message: String,
8}
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11enum Kind {
12    ConnectionString,
13    Connect,
14    Query,
15    Sql,
16    Grantee,
17}
18
19impl Error {
20    pub(crate) fn connection_string(message: impl Into<String>) -> Self {
21        Error { kind: Kind::ConnectionString, message: message.into() }
22    }
23
24    pub(crate) fn connect(message: impl Into<String>) -> Self {
25        Error { kind: Kind::Connect, message: message.into() }
26    }
27
28    pub(crate) fn query(message: impl Into<String>) -> Self {
29        Error { kind: Kind::Query, message: message.into() }
30    }
31
32    pub(crate) fn sql(message: impl Into<String>) -> Self {
33        Error { kind: Kind::Sql, message: message.into() }
34    }
35
36    pub(crate) fn grantee(message: impl Into<String>) -> Self {
37        Error { kind: Kind::Grantee, message: message.into() }
38    }
39
40    /// Returns whether the connection string does not parse.
41    #[must_use]
42    pub fn is_connection_string(&self) -> bool {
43        self.kind == Kind::ConnectionString
44    }
45
46    /// Returns whether no host in the connection string accepted a session: DNS, the network, TLS or authentication failed.
47    #[must_use]
48    pub fn is_connect(&self) -> bool {
49        self.kind == Kind::Connect
50    }
51
52    /// Returns whether a query against the connected database failed.
53    #[must_use]
54    pub fn is_query(&self) -> bool {
55        self.kind == Kind::Query
56    }
57
58    /// Returns whether the SQL to compare is not in the form demesne writes.
59    #[must_use]
60    pub fn is_sql(&self) -> bool {
61        self.kind == Kind::Sql
62    }
63
64    /// Returns whether a role named as a grantee cannot be one.
65    #[must_use]
66    pub fn is_grantee(&self) -> bool {
67        self.kind == Kind::Grantee
68    }
69}
70
71impl fmt::Display for Error {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        f.write_str(&self.message)
74    }
75}
76
77impl std::error::Error for Error {}