|
1 |
| -//! |
2 | 1 | //! # Alphabets
|
3 | 2 | //!
|
4 | 3 | //! An [alphabet::Alphabet] defines what ASCII symbols are used to encode to or decode from.
|
|
12 | 11 | //! API provide a default, but otherwise the user must provide an `Engine` to use.
|
13 | 12 | //!
|
14 | 13 | //! See [engine::Engine] for more on what engine to choose, or use [engine::STANDARD] if you
|
15 |
| -//! just want plain old standard base64 and don't have other requirements. |
| 14 | +//! just want plain old standard base64 and don't have other requirements. [engine::URL_SAFE] and |
| 15 | +//! [engine::URL_SAFE_NO_PAD] are also available. |
16 | 16 | //!
|
17 | 17 | //! ## Config
|
18 | 18 | //!
|
19 | 19 | //! In addition to an `Alphabet`, constructing an `Engine` also requires an [engine::Config]. Each
|
20 |
| -//! `Engine` has a corresponding `Config` implementation. |
| 20 | +//! `Engine` has a corresponding `Config` implementation since different `Engine`s may offer different |
| 21 | +//! levels of configurability. |
21 | 22 | //!
|
22 | 23 | //! [encode()] and [decode()] use the standard alphabet and default engine in an RFC 4648 standard
|
23 | 24 | //! setup.
|
24 | 25 | //!
|
25 | 26 | //! # Encoding
|
26 | 27 | //!
|
27 |
| -//! Several different encoding functions are available to you depending on your desire for |
| 28 | +//! Several different encoding methods on [Engine] are available to you depending on your desire for |
28 | 29 | //! convenience vs performance.
|
29 | 30 | //!
|
30 |
| -//! | Function | Output | Allocates | |
31 |
| -//! | ----------------------- | ---------------------------- | ------------------------------ | |
32 |
| -//! | `encode` | Returns a new `String` | Always | |
33 |
| -//! | `encode_engine` | Returns a new `String` | Always | |
34 |
| -//! | `encode_engine_string` | Appends to provided `String` | Only if `String` needs to grow | |
35 |
| -//! | `encode_engine_slice` | Writes to provided `&[u8]` | Never - fastest | |
| 31 | +//! | Method | Output | Allocates | |
| 32 | +//! | ---------------- | ---------------------------- | ------------------------------ | |
| 33 | +//! | `encode` | Returns a new `String` | Always | |
| 34 | +//! | `encode_string` | Appends to provided `String` | Only if `String` needs to grow | |
| 35 | +//! | `encode_slice` | Writes to provided `&[u8]` | Never - fastest | |
36 | 36 | //!
|
37 |
| -//! All of the encoding functions that take an `Engine` will pad as per the engine's config. |
| 37 | +//! All of the encoding methods will pad as per the engine's config. |
38 | 38 | //!
|
39 | 39 | //! # Decoding
|
40 | 40 | //!
|
41 |
| -//! Just as for encoding, there are different decoding functions available. |
| 41 | +//! Just as for encoding, there are different decoding methods available. |
42 | 42 | //!
|
43 |
| -//! | Function | Output | Allocates | |
44 |
| -//! | ----------------------- | ----------------------------- | ------------------------------ | |
45 |
| -//! | `decode` | Returns a new `Vec<u8>` | Always | |
46 |
| -//! | `decode_engine` | Returns a new `Vec<u8>` | Always | |
47 |
| -//! | `decode_engine_vec` | Appends to provided `Vec<u8>` | Only if `Vec` needs to grow | |
48 |
| -//! | `decode_engine_slice` | Writes to provided `&[u8]` | Never - fastest | |
| 43 | +//! | Method | Output | Allocates | |
| 44 | +//! | ---------------- | ----------------------------- | ------------------------------ | |
| 45 | +//! | `decode` | Returns a new `Vec<u8>` | Always | |
| 46 | +//! | `decode_vec` | Appends to provided `Vec<u8>` | Only if `Vec` needs to grow | |
| 47 | +//! | `decode_slice` | Writes to provided `&[u8]` | Never - fastest | |
49 | 48 | //!
|
50 | 49 | //! Unlike encoding, where all possible input is valid, decoding can fail (see [DecodeError]).
|
51 | 50 | //!
|
52 |
| -//! Input can be invalid because it has invalid characters or invalid padding. (No padding at all is |
53 |
| -//! valid, but excess padding is not.) Whitespace in the input is invalid, just like any other |
54 |
| -//! non-base64 byte. |
| 51 | +//! Input can be invalid because it has invalid characters or invalid padding. The nature of how |
| 52 | +//! padding is checked depends on the engine's config. |
| 53 | +//! Whitespace in the input is invalid, just like any other non-base64 byte. |
55 | 54 | //!
|
56 | 55 | //! # `Read` and `Write`
|
57 | 56 | //!
|
|
68 | 67 | //!
|
69 | 68 | //! See [display] for how to transparently base64 data via a `Display` implementation.
|
70 | 69 | //!
|
| 70 | +//! # Examples |
| 71 | +//! |
| 72 | +//! ## Using predefined engines |
| 73 | +//! |
| 74 | +//! ``` |
| 75 | +//! use base64::{engine, Engine as _}; |
| 76 | +//! |
| 77 | +//! let orig = b"some data"; |
| 78 | +//! let encoded: String = engine::STANDARD.encode(orig); |
| 79 | +//! assert_eq!(orig.as_slice(), &engine::STANDARD.decode(encoded).unwrap()); |
| 80 | +//! |
| 81 | +//! // or, URL-safe |
| 82 | +//! let encoded_url = engine::URL_SAFE_NO_PAD.encode(orig); |
| 83 | +//! ``` |
| 84 | +//! |
| 85 | +//! ## Custom alphabet, config, and engine |
| 86 | +//! |
| 87 | +//! ``` |
| 88 | +//! use base64::{engine, alphabet, Engine as _}; |
| 89 | +//! |
| 90 | +//! // bizarro-world base64: +/ as the first symbols instead of the last |
| 91 | +//! let alphabet = |
| 92 | +//! alphabet::Alphabet::new("+/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") |
| 93 | +//! .unwrap(); |
| 94 | +//! |
| 95 | +//! // a very weird config that encodes with padding but requires no padding when decoding...? |
| 96 | +//! let config = engine::GeneralPurposeConfig::new() |
| 97 | +//! .with_decode_allow_trailing_bits(true) |
| 98 | +//! .with_encode_padding(true) |
| 99 | +//! .with_decode_padding_mode(engine::DecodePaddingMode::RequireNone); |
| 100 | +//! |
| 101 | +//! let engine = engine::GeneralPurpose::new(&alphabet, config); |
| 102 | +//! |
| 103 | +//! let encoded = engine.encode(b"abc 123"); |
| 104 | +//! |
| 105 | +//! ``` |
| 106 | +//! |
71 | 107 | //! # Panics
|
72 | 108 | //!
|
73 | 109 | //! If length calculations result in overflowing `usize`, a panic will result.
|
|
0 commit comments