diff options
author | Philipp Gesang <phg@phi-gamma.net> | 2021-11-24 21:08:17 +0100 |
---|---|---|
committer | Philipp Gesang <phg@phi-gamma.net> | 2021-11-24 21:11:06 +0100 |
commit | c5305231d900cca8189a9995284258a841042f50 (patch) | |
tree | 87e1631148ab2ce38067e81481abfbb8a1d5375f | |
parent | fab3a5aea7107688b957ffc2502ed1539c7fad70 (diff) | |
download | vtcol-c5305231d900cca8189a9995284258a841042f50.tar.gz |
add base64 input for ‘set’
-rw-r--r-- | src/lib.rs | 31 | ||||
-rw-r--r-- | src/vtcol.rs | 14 |
2 files changed, 45 insertions, 0 deletions
@@ -571,9 +571,40 @@ impl Palette Self::from_buffered_reader(&mut reader) } + fn from_bytes(b: &[u8]) -> io::Result<Self> + { + if b.len() != PALETTE_SIZE * 3 { + return Err(io::Error::new( + io::ErrorKind::Other, + format!( + "expected {} B of data, got {}", + PALETTE_SIZE * 3, + b.len() + ), + )); + } + + let mut res = Self::new(); + res.0.copy_from_slice(&b); + + Ok(res) + } + /* [Palette::from_stdin] */ pub fn base64(&self) -> String { base64::encode(&self.0) } + + pub fn from_base64(b64: &str) -> io::Result<Self> + { + 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 0a198e6..8161241 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -76,6 +76,15 @@ impl<'a> Job .value_name("PATH") .help("apply scheme from file") .takes_value(true), + ) + .arg( + Arg::with_name("base64") + .short("b") + .long("base64") + .help("base64 encoded binary input") + .value_name("DATA") + .required(false) + .takes_value(true), ), ) .subcommand( @@ -174,6 +183,11 @@ 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)?; + return Ok(Self::Set(scheme)); + } let scheme = match subm.value_of("scheme") { Some("-") => Self::read_scheme_from_stdin(), Some(name) => { |