-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmod.rs
More file actions
144 lines (119 loc) · 3.38 KB
/
mod.rs
File metadata and controls
144 lines (119 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! This module contains helper structs and functions to render the CLI output
//! based on templates. These templates allow dynamic composition of the output.
//! The output offers sections for pre, post and command hints, as well as
//! success and error output. The [`ErrorReport`] serves as an alternative to
//! snafu's [`Report`](snafu::Report).
use std::{
fmt::Write,
ops::{Deref, DerefMut},
};
use snafu::{ResultExt, Snafu};
use tera::Tera;
mod error;
mod result;
pub use error::ErrorContext;
pub use result::ResultContext;
use crate::utils::use_colored_output;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("failed to create output renderer"))]
CreateRenderer { source: tera::Error },
}
#[derive(Debug)]
pub enum OutputKind {
Result,
Error,
}
pub trait ContextExt {
fn set_no_color(&mut self, no_color: bool);
fn into_context(self) -> tera::Context;
fn output_kind(&self) -> OutputKind;
}
pub trait ErrorReport
where
Self: std::error::Error,
{
fn into_error_report(self) -> std::result::Result<String, std::fmt::Error>;
}
impl<T> ErrorReport for T
where
T: std::error::Error,
{
fn into_error_report(self) -> std::result::Result<String, std::fmt::Error> {
let mut report = String::new();
// Print top most error
write!(report, "An unrecoverable error occurred: {self}\n\n")?;
writeln!(
report,
"Caused by these errors (recent errors listed first):"
)?;
let mut error: &dyn std::error::Error = &self;
let mut index = 1;
while let Some(source) = error.source() {
writeln!(report, " {index}: {source}")?;
error = source;
index += 1;
}
Ok(report)
}
}
pub struct Output<C>
where
C: ContextExt,
{
renderer: Tera,
context: C,
}
impl<C> Output<C>
where
C: ContextExt,
{
pub fn new(mut context: C, no_color: bool) -> Result<Self> {
let renderer = Self::create_renderer()?;
let no_color = use_colored_output(!no_color);
context.set_no_color(no_color);
Ok(Self { renderer, context })
}
pub fn render(self) -> String {
// We ignore the error. If we cannot render the output, there is
// no point to explicitly handle the error.
match self.context.output_kind() {
OutputKind::Result => self
.renderer
.render("result", &self.context.into_context())
.expect("Failed to render result"),
OutputKind::Error => self
.renderer
.render("error", &self.context.into_context())
.expect("Failed to render error"),
}
}
fn create_renderer() -> Result<Tera> {
let mut renderer = Tera::default();
renderer
.add_raw_templates(vec![
("result", include_str!("templates/result.tpl")),
("error", include_str!("templates/error.tpl")),
])
.context(CreateRendererSnafu)?;
Ok(renderer)
}
}
impl<C> Deref for Output<C>
where
C: ContextExt,
{
type Target = C;
fn deref(&self) -> &Self::Target {
&self.context
}
}
impl<C> DerefMut for Output<C>
where
C: ContextExt,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.context
}
}