Skip to main content

sqlive/
no_policy.rs

1//! What a host with no policy of its own opens a database with: nothing
2//! beyond sqlive's own rules, and the engine's failure as the handle's.
3
4use crate::authorization::{Policy, PolicyAction, Refresh};
5use crate::connection::HostSql;
6use crate::EngineFailure;
7use std::convert::Infallible;
8use std::sync::Arc;
9
10/// The policy of a host that adds nothing to sqlive's own rules: it
11/// refuses no statement.
12#[derive(Clone, Copy, Debug, Default)]
13pub struct NoPolicy;
14
15impl Policy for NoPolicy {
16    type Refusal = Infallible;
17    type Failure = HandleFailure;
18    type Scope = ();
19
20    fn judge(&self, _action: &PolicyAction<'_>) -> Result<(), Infallible> {
21        Ok(())
22    }
23
24    fn check_sql(&self, _tokens: &[crate::sql::Token]) -> Result<(), Infallible> {
25        Ok(())
26    }
27
28    fn begin_app_call(&self) {}
29
30    fn host_object(&self, _name: &str) -> bool {
31        false
32    }
33
34    fn statement_key(&self) -> u64 {
35        0
36    }
37
38    fn enter(&self, _scope: &()) {}
39
40    fn leave(&self, _scope: &()) {}
41
42    fn refresh(&self, _sql: &HostSql<Self>, _extent: Refresh) -> rusqlite::Result<()> {
43        Ok(())
44    }
45}
46
47/// What a handle fails with when its host has no failure of its own: the
48/// engine's failure that ended it, shared by every later call.
49#[derive(Clone, Debug)]
50pub struct HandleFailure(Arc<EngineFailure<HandleFailure, Infallible>>);
51
52impl HandleFailure {
53    /// The engine's failure that ended the handle.
54    pub fn engine(&self) -> &EngineFailure<HandleFailure, Infallible> {
55        &self.0
56    }
57}
58
59impl From<EngineFailure<HandleFailure, Infallible>> for HandleFailure {
60    fn from(failure: EngineFailure<HandleFailure, Infallible>) -> Self {
61        Self(Arc::new(failure))
62    }
63}
64
65impl std::fmt::Display for HandleFailure {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        match &*self.0 {
68            EngineFailure::Rollback => write!(f, "rolling back a failed transaction failed"),
69            EngineFailure::Checkpoint(error) => write!(f, "a WAL checkpoint failed: {error}"),
70            EngineFailure::Statistics(error) => {
71                write!(f, "keeping the planner's statistics failed: {error}")
72            }
73            EngineFailure::Hook(error) => {
74                write!(f, "removing a transaction's hook failed: {error}")
75            }
76        }
77    }
78}
79
80impl std::error::Error for HandleFailure {
81    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
82        match &*self.0 {
83            EngineFailure::Rollback => None,
84            EngineFailure::Checkpoint(error)
85            | EngineFailure::Statistics(error)
86            | EngineFailure::Hook(error) => Some(error),
87        }
88    }
89}