1
1
use crate :: fmt:: CaretNotation ;
2
2
use core:: fmt;
3
+ use std:: marker:: PhantomData ;
3
4
use std:: time:: Duration ;
4
5
use std:: { error, io} ;
5
6
@@ -16,13 +17,19 @@ pub enum Error {
16
17
/// or the terminal has a lot of latency (e.g. when connected via SSH).
17
18
Timeout ( Duration ) ,
18
19
/// The terminal does not support querying for the foreground or background color.
19
- UnsupportedTerminal ,
20
+ UnsupportedTerminal ( UnsupportedTerminalError ) ,
20
21
}
21
22
23
+ // Note: the private field is here for fowards-compatibility
24
+ // in case we want to introduce detailed reasons.
25
+ #[ derive( Debug ) ]
26
+ pub struct UnsupportedTerminalError ( PhantomData < ( ) > ) ;
27
+
22
28
impl error:: Error for Error {
23
29
fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
24
30
match self {
25
31
Error :: Io ( source) => Some ( source) ,
32
+ Error :: UnsupportedTerminal ( source) => Some ( source) ,
26
33
_ => None ,
27
34
}
28
35
}
@@ -42,13 +49,25 @@ impl fmt::Display for Error {
42
49
Error :: Timeout ( timeout) => {
43
50
write ! ( f, "operation did not complete within {timeout:?}" )
44
51
}
45
- Error :: UnsupportedTerminal { } => {
46
- write ! ( f, "the terminal does not support querying for its colors" )
47
- }
52
+ Error :: UnsupportedTerminal ( e) => e. fmt ( f) ,
48
53
}
49
54
}
50
55
}
51
56
57
+ impl Error {
58
+ pub ( crate ) fn unsupported ( ) -> Self {
59
+ Error :: UnsupportedTerminal ( UnsupportedTerminalError ( PhantomData ) )
60
+ }
61
+ }
62
+
63
+ impl error:: Error for UnsupportedTerminalError { }
64
+
65
+ impl fmt:: Display for UnsupportedTerminalError {
66
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
67
+ f. write_str ( "the terminal does not support querying for its colors" )
68
+ }
69
+ }
70
+
52
71
impl From < io:: Error > for Error {
53
72
fn from ( source : io:: Error ) -> Self {
54
73
Error :: Io ( source)
0 commit comments