Skip to content

Commit 2997a2c

Browse files
committed
💥 Future-proof UnsupportedTerminal error
1 parent a74c802 commit 2997a2c

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

crates/benchmark/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn main() -> Result<()> {
4242

4343
let supported = match color_palette() {
4444
Ok(_) => true,
45-
Err(Error::UnsupportedTerminal) => false,
45+
Err(Error::UnsupportedTerminal(_)) => false,
4646
Err(e) => return Err(e),
4747
};
4848

@@ -54,7 +54,7 @@ fn main() -> Result<()> {
5454
fn bench() -> Result<Duration> {
5555
let start = Instant::now();
5656
match black_box(color_palette()) {
57-
Ok(_) | Err(Error::UnsupportedTerminal) => Ok(start.elapsed()),
57+
Ok(_) | Err(Error::UnsupportedTerminal(_)) => Ok(start.elapsed()),
5858
Err(err) => Err(err),
5959
}
6060
}

crates/terminal-colorsaurus/src/error.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::fmt::CaretNotation;
22
use core::fmt;
3+
use std::marker::PhantomData;
34
use std::time::Duration;
45
use std::{error, io};
56

@@ -16,13 +17,19 @@ pub enum Error {
1617
/// or the terminal has a lot of latency (e.g. when connected via SSH).
1718
Timeout(Duration),
1819
/// The terminal does not support querying for the foreground or background color.
19-
UnsupportedTerminal,
20+
UnsupportedTerminal(UnsupportedTerminalError),
2021
}
2122

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+
2228
impl error::Error for Error {
2329
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
2430
match self {
2531
Error::Io(source) => Some(source),
32+
Error::UnsupportedTerminal(source) => Some(source),
2633
_ => None,
2734
}
2835
}
@@ -42,13 +49,25 @@ impl fmt::Display for Error {
4249
Error::Timeout(timeout) => {
4350
write!(f, "operation did not complete within {timeout:?}")
4451
}
45-
Error::UnsupportedTerminal {} => {
46-
write!(f, "the terminal does not support querying for its colors")
47-
}
52+
Error::UnsupportedTerminal(e) => e.fmt(f),
4853
}
4954
}
5055
}
5156

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+
5271
impl From<io::Error> for Error {
5372
fn from(source: io::Error) -> Self {
5473
Error::Io(source)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::{Color, ColorPalette, Error, QueryOptions, Result};
22

33
pub(crate) fn color_palette(_options: QueryOptions) -> Result<ColorPalette> {
4-
Err(Error::UnsupportedTerminal)
4+
Err(Error::unsupported())
55
}
66

77
pub(crate) fn foreground_color(_options: QueryOptions) -> Result<Color> {
8-
Err(Error::UnsupportedTerminal)
8+
Err(Error::unsupported())
99
}
1010

1111
pub(crate) fn background_color(_options: QueryOptions) -> Result<Color> {
12-
Err(Error::UnsupportedTerminal)
12+
Err(Error::unsupported())
1313
}

crates/terminal-colorsaurus/src/xterm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn query<T>(
9393
read_response: impl FnOnce(&mut Reader<'_>) -> Result<T>,
9494
) -> Result<T> {
9595
if quirks.is_known_unsupported() {
96-
return Err(Error::UnsupportedTerminal);
96+
return Err(Error::unsupported());
9797
}
9898

9999
let mut tty = terminal()?;
@@ -123,7 +123,7 @@ fn read_color_response(r: &mut Reader<'_>) -> Result<Vec<u8>> {
123123
// the terminal does not recocgnize the color query.
124124
if !r.buffer().starts_with(b"]") {
125125
_ = consume_da1_response(r, false);
126-
return Err(Error::UnsupportedTerminal);
126+
return Err(Error::unsupported());
127127
}
128128

129129
// Some terminals always respond with BEL (see terminal survey).

0 commit comments

Comments
 (0)