diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 31 |
1 files changed, 31 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 |