Crate aliri_base64[−][src]
aliri_base64
Wrappers for values that should be serialized or represented as base64
Underlying data is stored as an actual byte slice. Costs of conversions
between base64 and raw bytes only occur for calls to from_encoded()
or
conversions to strings via debug or display formatting.
This can make debugging byte arrays significantly less annoying,
as Debug
and Display
implementations are provided as better views of the underlying byte data.
Example
Using ToString::to_string()
:
use aliri_base64::Base64; let data = Base64::from_raw("👋 hello, world! 👋".as_bytes()); let enc = data.to_string(); assert_eq!(enc, "8J+RiyBoZWxsbywgd29ybGQhIPCfkYs=");
use aliri_base64::Base64; let data = Base64::from_raw("👋 hello, world! 👋".as_bytes()); let enc = format!("MyData: {}", data); assert_eq!(enc, "MyData: 8J+RiyBoZWxsbywgd29ybGQhIPCfkYs=");
Note that the output data is fenced in backticks when formatted for debugging.
use aliri_base64::Base64; let data = Base64::from_raw("👋 hello, world! 👋".as_bytes()); let enc = format!("MyData: {:?}", data); assert_eq!(enc, "MyData: `8J+RiyBoZWxsbywgd29ybGQhIPCfkYs=`");
Reinterpreting raw data, moving from URL encoding with no padding to standard encoding with padding:
use aliri_base64::{Base64, Base64Url}; let data = Base64Url::from_encoded("8J-RiyBoZWxsbywgd29ybGQhIPCfkYs").unwrap(); assert_eq!(data.as_slice(), "👋 hello, world! 👋".as_bytes()); let transcode = Base64::from_raw(data.into_inner()); let enc = transcode.to_string(); assert_eq!(enc, "8J+RiyBoZWxsbywgd29ybGQhIPCfkYs=");
Serde
With the serde
feature enabled, serializers and deserializers will be
created that will encode the underlying byte array as a base64 string
using the relevant encoding.
Structs
Base64 | Owned data to be encoded as standard base64 |
Base64Ref | Borrowed data to be encoded as standard base64 |
Base64Url | Owned data to be encoded as URL-safe base64 (no padding) |
Base64UrlRef | Borrowed data to be encoded as URL-safe base64 (no padding) |
InvalidBase64Data | An error while decoding a value which is not properly formatted base64 data |