compris/annotate/depict/
depiction.rs

1use super::{super::traits::*, mode::*};
2
3use {
4    depiction::*,
5    std::{error::*, io},
6};
7
8//
9// AnnotatedDepiction
10//
11
12/// A [Depict] wrapper for an [Annotated] [Depict].
13///
14/// The inner [Depict] is called first and the
15/// [Annotations](super::super::annotations::Annotations) next.
16pub struct AnnotatedDepiction<'own, InnerT> {
17    /// Inner.
18    pub inner: &'own InnerT,
19
20    /// Mode.
21    pub mode: AnnotatedDepictionMode,
22}
23
24impl<'own, InnerT> AnnotatedDepiction<'own, InnerT> {
25    /// Constructor.
26    pub fn new(inner: &'own InnerT, mode: AnnotatedDepictionMode) -> Self {
27        Self { inner, mode }
28    }
29}
30
31impl<'own, InnerT> Depict for AnnotatedDepiction<'own, InnerT>
32where
33    InnerT: Annotated + Depict,
34{
35    fn depict<WriteT>(&self, writer: &mut WriteT, context: &DepictionContext) -> io::Result<()>
36    where
37        WriteT: io::Write,
38    {
39        if let Some(annotations) = self.inner.annotations() {
40            match self.mode {
41                AnnotatedDepictionMode::Inline => {
42                    self.inner.depict(writer, context)?;
43                    if annotations.has_depiction(DepictionFormat::Compact) {
44                        annotations.depict(writer, &context.clone().with_format(DepictionFormat::Compact))?;
45                    }
46                }
47
48                AnnotatedDepictionMode::Multiline => {
49                    if annotations.has_depiction(DepictionFormat::Optimized) {
50                        annotations.depict(writer, &context.clone().with_format(DepictionFormat::Optimized))?;
51                        context.indent(writer)?;
52                    } else {
53                        context.separate(writer)?;
54                    }
55                    self.inner.depict(writer, context)?;
56                }
57            }
58        } else {
59            context.separate(writer)?;
60            self.inner.depict(writer, context)?;
61        }
62
63        Ok(())
64    }
65}
66
67//
68// ToAnnotatedDepiction
69//
70
71/// To [AnnotatedDepiction].
72pub trait ToAnnotatedDepiction<'own>
73where
74    Self: Sized,
75{
76    /// To [AnnotatedDepiction].
77    fn annotated_depiction(&'own self) -> AnnotatedDepiction<'own, Self>;
78}
79
80impl<'own, ErrorT> ToAnnotatedDepiction<'own> for ErrorT
81where
82    ErrorT: Error,
83{
84    fn annotated_depiction(&'own self) -> AnnotatedDepiction<'own, Self> {
85        AnnotatedDepiction::new(self, AnnotatedDepictionMode::Multiline)
86    }
87}