Skip to main content

inc_complete/db/
debug_with_db.rs

1use crate::Db;
2
3/// Format `Self` into a debug string similar to the `Debug` trait but
4/// with a `Db` to provide additional context. This is helpful to format
5/// id-based types into a more human readable format by grabbing additional
6/// data stored elsewhere.
7pub trait DebugWithDb<Storage>: Sized {
8    fn debug_with_db<'a>(&'a self, db: &'a Db<Storage>) -> DebugWithDbWrapper<'a, Self, Storage> {
9        DebugWithDbWrapper { obj: self, db }
10    }
11
12    fn fmt_with_db(&self, _db: &Db<Storage>, f: &mut std::fmt::Formatter) -> std::fmt::Result;
13}
14
15pub fn debug_with_db<'a, T, Storage>(
16    obj: &'a T,
17    db: &'a Db<Storage>,
18) -> DebugWithDbWrapper<'a, T, Storage> {
19    DebugWithDbWrapper { obj, db }
20}
21
22/// Wrapper type returned by [DebugWithDb::debug_with_db] used for formatting
23pub struct DebugWithDbWrapper<'a, T, S> {
24    obj: &'a T,
25    db: &'a Db<S>,
26}
27
28impl<T, S> std::fmt::Debug for DebugWithDbWrapper<'_, T, S>
29where
30    T: DebugWithDb<S>,
31{
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        self.obj.fmt_with_db(self.db, f)
34    }
35}
36
37impl<T: std::fmt::Debug, S> DebugWithDb<S> for T {
38    fn fmt_with_db(&self, _db: &Db<S>, f: &mut std::fmt::Formatter) -> std::fmt::Result {
39        write!(f, "{self:?}")
40    }
41}