diff options
Diffstat (limited to 'examples/base64.rs')
-rw-r--r-- | examples/base64.rs | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/examples/base64.rs b/examples/base64.rs index cba745b..0a214d2 100644 --- a/examples/base64.rs +++ b/examples/base64.rs @@ -4,37 +4,28 @@ use std::path::PathBuf; use std::process; use std::str::FromStr; -use base64::{read, write}; +use base64::{alphabet, engine, read, write}; use structopt::StructOpt; #[derive(Debug, StructOpt)] -enum CharacterSet { +enum Alphabet { Standard, UrlSafe, } -impl Default for CharacterSet { +impl Default for Alphabet { fn default() -> Self { - CharacterSet::Standard + Self::Standard } } -impl Into<base64::Config> for CharacterSet { - fn into(self) -> base64::Config { - match self { - CharacterSet::Standard => base64::STANDARD, - CharacterSet::UrlSafe => base64::URL_SAFE, - } - } -} - -impl FromStr for CharacterSet { +impl FromStr for Alphabet { type Err = String; - fn from_str(s: &str) -> Result<CharacterSet, String> { + fn from_str(s: &str) -> Result<Self, String> { match s { - "standard" => Ok(CharacterSet::Standard), - "urlsafe" => Ok(CharacterSet::UrlSafe), - _ => Err(format!("charset '{}' unrecognized", s)), + "standard" => Ok(Self::Standard), + "urlsafe" => Ok(Self::UrlSafe), + _ => Err(format!("alphabet '{}' unrecognized", s)), } } } @@ -45,10 +36,10 @@ struct Opt { /// decode data #[structopt(short = "d", long = "decode")] decode: bool, - /// The character set to choose. Defaults to the standard base64 character set. - /// Supported character sets include "standard" and "urlsafe". - #[structopt(long = "charset")] - charset: Option<CharacterSet>, + /// The alphabet to choose. Defaults to the standard base64 alphabet. + /// Supported alphabets include "standard" and "urlsafe". + #[structopt(long = "alphabet")] + alphabet: Option<Alphabet>, /// The file to encode/decode. #[structopt(parse(from_os_str))] file: Option<PathBuf>, @@ -68,14 +59,23 @@ fn main() { } Some(f) => Box::new(File::open(f).unwrap()), }; - let config = opt.charset.unwrap_or_default().into(); + + let alphabet = opt.alphabet.unwrap_or_default(); + let engine = engine::GeneralPurpose::new( + &match alphabet { + Alphabet::Standard => alphabet::STANDARD, + Alphabet::UrlSafe => alphabet::URL_SAFE, + }, + engine::general_purpose::PAD, + ); + let stdout = io::stdout(); let mut stdout = stdout.lock(); let r = if opt.decode { - let mut decoder = read::DecoderReader::new(&mut input, config); + let mut decoder = read::DecoderReader::new(&mut input, &engine); io::copy(&mut decoder, &mut stdout) } else { - let mut encoder = write::EncoderWriter::new(&mut stdout, config); + let mut encoder = write::EncoderWriter::new(&mut stdout, &engine); io::copy(&mut input, &mut encoder) }; if let Err(e) = r { |