compris/annotate/depict/
depiction.rs

1use super::{super::annotated::*, mode::*};
2
3use {
4    kutil::cli::depict::*,
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            self.inner.depict(writer, context)?;
60        }
61
62        Ok(())
63    }
64}
65
66//
67// ToAnnotatedDepiction
68//
69
70/// To [AnnotatedDepiction].
71pub trait ToAnnotatedDepiction<'own>
72where
73    Self: Sized,
74{
75    /// To [AnnotatedDepiction].
76    fn annotated_depiction(&'own self) -> AnnotatedDepiction<'own, Self>;
77}
78
79impl<'own, ErrorT> ToAnnotatedDepiction<'own> for ErrorT
80where
81    ErrorT: Error,
82{
83    fn annotated_depiction(&'own self) -> AnnotatedDepiction<'own, Self> {
84        AnnotatedDepiction::new(self, AnnotatedDepictionMode::Multiline)
85    }
86}