coven_database/
write_failure.rs1use crate::store_failure::StoreFailure;
5use crate::DbError;
6
7pub trait WriteFailure:
14 From<DbError> + From<sqlive::rusqlite::Error> + coven_foundation::task::CarriesBug
15{
16 fn with_rollback(self, rollback: sqlive::rusqlite::Error) -> Self;
18 fn with_cleanup(write: Self, failures: Vec<DbError>) -> Self
20 where
21 Self: Sized;
22}
23
24pub(crate) fn after_cleanup<R, E: WriteFailure>(
28 result: Result<R, E>,
29 failures: Vec<DbError>,
30) -> Result<R, E> {
31 if failures.is_empty() {
32 return result;
33 }
34 match result {
35 Ok(result) => {
36 tracing::error!(
37 ?failures,
38 "removing released file bytes failed; their records stay for the next write or open"
39 );
40 Ok(result)
41 }
42 Err(error) => Err(E::with_cleanup(error, failures)),
43 }
44}
45
46impl WriteFailure for DbError {
47 fn with_rollback(self, rollback: sqlive::rusqlite::Error) -> Self {
48 Self::Rollback { operation: Box::new(self), rollback }
49 }
50 fn with_cleanup(write: Self, failures: Vec<DbError>) -> Self {
51 Self::FileCleanup { write: Err(Box::new(write)), failures }
52 }
53}
54
55impl sqlive::RollbackJoin<StoreFailure, DbError> for DbError {
56 fn with_rollback(self, rollback: sqlive::rusqlite::Error) -> Self {
57 WriteFailure::with_rollback(self, rollback)
58 }
59}
60
61pub(crate) struct Hosted<E>(pub(crate) E);
65
66impl<E> Hosted<E> {
67 pub(crate) fn into_inner(self) -> E {
69 self.0
70 }
71}
72
73pub(crate) fn hosted<T, E>(run: impl FnOnce() -> Result<T, E>) -> Result<T, Hosted<E>> {
75 run().map_err(Hosted)
76}
77
78impl<E: From<DbError>> From<sqlive::Error<StoreFailure, DbError>> for Hosted<E> {
79 fn from(error: sqlive::Error<StoreFailure, DbError>) -> Self {
80 Self(E::from(DbError::from(error)))
81 }
82}
83
84impl<E: WriteFailure> sqlive::RollbackJoin<StoreFailure, DbError> for Hosted<E> {
85 fn with_rollback(self, rollback: sqlive::rusqlite::Error) -> Self {
86 Self(self.0.with_rollback(rollback))
87 }
88}