summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/vtcol.rs70
1 files changed, 36 insertions, 34 deletions
diff --git a/src/vtcol.rs b/src/vtcol.rs
index 361b8fe..d886f2b 100644
--- a/src/vtcol.rs
+++ b/src/vtcol.rs
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Result};
-use std::{fmt,
+use std::{convert::TryFrom,
+ fmt,
io::Error,
path::{Path, PathBuf},
sync::atomic::{AtomicBool, Ordering}};
@@ -41,33 +42,38 @@ enum Color
White(bool),
}
-impl Color
+impl TryFrom<u8> for Color
{
- fn of_value(val: u8) -> Color
+ type Error = anyhow::Error;
+
+ fn try_from(val: u8) -> Result<Self, Self::Error>
{
match val {
- 0x00_u8 => Color::Black(false),
- 0x01_u8 => Color::Red(false),
- 0x02_u8 => Color::Green(false),
- 0x03_u8 => Color::Yellow(false),
- 0x04_u8 => Color::Blue(false),
- 0x05_u8 => Color::Magenta(false),
- 0x06_u8 => Color::Cyan(false),
- 0x07_u8 => Color::White(false),
-
- 0x08_u8 => Color::Black(true),
- 0x09_u8 => Color::Red(true),
- 0x0a_u8 => Color::Green(true),
- 0x0b_u8 => Color::Yellow(true),
- 0x0c_u8 => Color::Blue(true),
- 0x0d_u8 => Color::Magenta(true),
- 0x0e_u8 => Color::Cyan(true),
- 0x0f_u8 => Color::White(true),
-
- _ => panic!("invalid color value: {}", val),
+ 0x00 => Ok(Color::Black(false)),
+ 0x01 => Ok(Color::Red(false)),
+ 0x02 => Ok(Color::Green(false)),
+ 0x03 => Ok(Color::Yellow(false)),
+ 0x04 => Ok(Color::Blue(false)),
+ 0x05 => Ok(Color::Magenta(false)),
+ 0x06 => Ok(Color::Cyan(false)),
+ 0x07 => Ok(Color::White(false)),
+
+ 0x08 => Ok(Color::Black(true)),
+ 0x09 => Ok(Color::Red(true)),
+ 0x0a => Ok(Color::Green(true)),
+ 0x0b => Ok(Color::Yellow(true)),
+ 0x0c => Ok(Color::Blue(true)),
+ 0x0d => Ok(Color::Magenta(true)),
+ 0x0e => Ok(Color::Cyan(true)),
+ 0x0f => Ok(Color::White(true)),
+
+ _ => Err(anyhow!("invalid color value: {}", val)),
}
}
+} /* [impl TryFrom<u8> for Color] */
+impl Color
+{
fn format_brightness(b: bool, s: &str) -> String
{
if b {
@@ -437,12 +443,12 @@ impl Palette
let idx: usize = i % 3;
buf[idx] = *col;
if idx == 2 {
+ let col = Color::try_from((i / 3) as u8)
+ .map(|c| format!("{}", c))
+ .unwrap_or_else(|_| "??".into());
println!(
"{:>15} => 0x{:02.X}{:02.X}{:02.X}",
- Color::of_value((i / 3) as u8).to_string(),
- buf[0],
- buf[1],
- buf[2]
+ col, buf[0], buf[1], buf[2]
);
}
}
@@ -509,14 +515,10 @@ impl fmt::Debug for Palette
let r = self.0[i as usize];
let g = self.0[i as usize + 1];
let b = self.0[i as usize + 2];
- let _ = writeln!(
- f,
- "{} => 0x{:02.X}{:02.X}{:02.X}",
- Color::of_value(i),
- r,
- g,
- b
- );
+ let col = Color::try_from((i / 3) as u8)
+ .map(|c| format!("{}", c))
+ .unwrap_or_else(|_| "??".into());
+ let _ = writeln!(f, "{} => 0x{:02.X}{:02.X}{:02.X}", col, r, g, b);
i += 3_u8;
}
std::result::Result::Ok(())