From df5a1edc18f85c54b8e497839a309ad95515b70f Mon Sep 17 00:00:00 2001 From: rap2h Date: Fri, 19 May 2017 14:24:02 +0200 Subject: [PATCH] encoding/decoding base64 --- src/encoding.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/intro.md | 4 ++++ 2 files changed, 49 insertions(+) diff --git a/src/encoding.md b/src/encoding.md index b7c574f..f573f7a 100644 --- a/src/encoding.md +++ b/src/encoding.md @@ -6,6 +6,7 @@ | [Deserialize a TOML configuration file][ex-toml-config] | [![toml-badge]][toml] | [![cat-encoding-badge]][cat-encoding] | | [Percent-encode a string][ex-percent-encode] | [![url-badge]][url] | [![cat-encoding-badge]][cat-encoding] | | [Encode and decode hex][ex-hex-encode-decode] | [![data-encoding-badge]][data-encoding] | [![cat-encoding-badge]][cat-encoding] | +| [Encode and decode base64][ex-base64] | [![base64-badge]][base64] | [![cat-encoding-badge]][cat-encoding] | [ex-json-value]: #ex-json-value @@ -278,6 +279,48 @@ fn run() -> Result<()> { quick_main!(run); ``` +[ex-base64]: #ex-base64 + +## Encode and decode base64 + +[![base64-badge]][base64] [![cat-encoding-badge]][cat-encoding] + +Byte slice is encoded into `base64` String with help of [`encode`] +and subsequently decoded with [`decode`]. + +[`decode`]: https://docs.rs/base64/*/base64/fn.decode.html +[`encode`]: https://docs.rs/base64/*/base64/fn.encode.html + +```rust +#[macro_use] +extern crate error_chain; +extern crate base64; + +use std::str; +use base64::{encode, decode}; + +error_chain! { + foreign_links { + Base64(base64::DecodeError); + Utf8Error(str::Utf8Error); + } +} + +fn run() -> Result<()> { + let hello = b"hello rustaceans"; + let encoded = encode(hello); + let decoded = decode(&encoded)?; + + println!("origin: {}", str::from_utf8(hello)?); + println!("base64 encoded: {}", encoded); + println!("back to origin: {}", str::from_utf8(&decoded)?); + + Ok(()) +} + +quick_main!(run); +``` + [cat-encoding-badge]: https://img.shields.io/badge/-encoding-red.svg @@ -293,6 +336,8 @@ quick_main!(run); [url]: https://docs.rs/url/ [data-encoding-badge]: https://img.shields.io/crates/v/data-encoding.svg?label=url [data-encoding]: https://github.com/ia0/data-encoding +[base64-badge]: https://img.shields.io/crates/v/base64.svg?label=base64 +[base64]: https://docs.rs/base64/ diff --git a/src/intro.md b/src/intro.md index 5f0b4d6..af23c40 100644 --- a/src/intro.md +++ b/src/intro.md @@ -36,6 +36,7 @@ community. It needs and welcomes help. For details see | [Serialize and deserialize unstructured JSON][ex-json-value] | [![serde-json-badge]][serde-json] | [![cat-encoding-badge]][cat-encoding] | | [Deserialize a TOML configuration file][ex-toml-config] | [![toml-badge]][toml] | [![cat-encoding-badge]][cat-encoding] | | [Percent-encode a string][ex-percent-encode] | [![url-badge]][url] | [![cat-encoding-badge]][cat-encoding] | +| [Encode and decode base64][ex-base64] | [![base64-badge]][base64] | [![cat-encoding-badge]][cat-encoding] | ## [Concurrency](concurrency.html) @@ -148,6 +149,8 @@ Keep lines sorted. [url]: https://docs.rs/url/ [regex]: https://docs.rs/regex/ [regex-badge]: https://img.shields.io/crates/v/regex.svg?label=regex +[base64-badge]: https://img.shields.io/crates/v/base64.svg?label=base64 +[base64]: https://docs.rs/base64/ @@ -181,3 +184,4 @@ Keep lines sorted. [ex-url-parse]: net.html#ex-url-parse [ex-url-rm-frag]: net.html#ex-url-rm-frag [ex-parse-subprocess-output]: basics.html#ex-parse-subprocess-output +[ex-base64]: encoding.html#ex-base64 \ No newline at end of file