From d6e8a37767644b50001e14ebbab594e799a94db7 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 25 Nov 2021 00:11:08 +0100 Subject: move base64 handling out of Palette Try and not encumber the lower level types with more dependencies than needed. --- src/lib.rs | 33 +++++++++++++++++++-------------- src/vtcol.rs | 18 +++++++----------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d0014a5..c652565 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -351,6 +351,25 @@ impl Scheme { Self::Custom(Some(path.as_ref().into())) } + + pub fn base64(&self) -> io::Result + { + let pal = Palette::try_from(self)?; + Ok(base64::encode(&pal.0)) + } + + pub fn from_base64(b64: &str) -> io::Result + { + base64::decode(b64.as_bytes()) + .map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("failed to decode input as base64: {}", e), + ) + }) + .and_then(|b| Palette::from_bytes(&b)) + .map(Self::from) + } } /* [impl Scheme] */ /** Try to select one of the predefined schemes; if that fails, @@ -591,20 +610,6 @@ impl Palette } /* [Palette::from_stdin] */ - - pub fn base64(&self) -> String { base64::encode(&self.0) } - - pub fn from_base64(b64: &str) -> io::Result - { - base64::decode(b64.as_bytes()) - .map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("failed to decode input as base64: {}", e), - ) - }) - .and_then(|b| Self::from_bytes(&b)) - } } /* [impl Palette] */ impl fmt::Display for Palette diff --git a/src/vtcol.rs b/src/vtcol.rs index 256d137..9a78d2f 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -187,8 +187,7 @@ impl<'a> Job match matches.subcommand() { ("dump", Some(subm)) => { if let Some(b64) = subm.value_of("base64") { - let scheme = - Palette::from_base64(&b64).map(Scheme::from)?; + let scheme = Scheme::from_base64(b64)?; return Ok(Self::Dump(scheme)); } if let Some(name) = subm.value_of("scheme") { @@ -200,8 +199,7 @@ impl<'a> Job ("list", _) => Ok(Self::List), ("set", Some(subm)) => { if let Some(b64) = subm.value_of("base64") { - let scheme = - Palette::from_base64(&b64).map(Scheme::from)?; + let scheme = Scheme::from_base64(&b64)?; return Ok(Self::Set(scheme)); } let scheme = match subm.value_of("scheme") { @@ -353,16 +351,14 @@ impl<'a> Job let fd = Console::current()?; vrb!("console fd: {}", fd); + let scm = fd.current_scheme()?; if b64 { - let pal = fd.current_palette()?; - println!("{}", pal.base64()); + print!("{}", scm.base64()?); + } else { + vrb!("active scheme:"); + println!("{}", scm); } - let scm = fd.current_scheme()?; - - vrb!("active scheme:"); - println!("{}", scm); - Ok(()) } -- cgit v1.2.3