1use crate::AnalyzerDb;
2use std::fmt;
3
4pub trait Displayable: DisplayWithDb {
5 fn display<'a, 'b>(&'a self, db: &'b dyn AnalyzerDb) -> DisplayableWrapper<'b, &'a Self> {
6 DisplayableWrapper::new(db, self)
7 }
8}
9impl<T: DisplayWithDb> Displayable for T {}
10
11pub trait DisplayWithDb {
12 fn format(&self, db: &dyn AnalyzerDb, f: &mut fmt::Formatter<'_>) -> fmt::Result;
13}
14
15impl<T: ?Sized> DisplayWithDb for &T
16where
17 T: DisplayWithDb,
18{
19 fn format(&self, db: &dyn AnalyzerDb, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 (*self).format(db, f)
21 }
22}
23
24pub struct DisplayableWrapper<'a, T> {
25 db: &'a dyn AnalyzerDb,
26 inner: T,
27}
28impl<'a, T> DisplayableWrapper<'a, T> {
29 pub fn new(db: &'a dyn AnalyzerDb, inner: T) -> Self {
30 Self { db, inner }
31 }
32 pub fn child(&self, inner: T) -> Self {
33 Self { db: self.db, inner }
34 }
35}
36
37impl<T: DisplayWithDb> fmt::Display for DisplayableWrapper<'_, T> {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 self.inner.format(self.db, f)
40 }
41}