Skip to content

Commit e580966

Browse files
Better introductory docs
1 parent b59e4ea commit e580966

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

src/engine/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use general_purpose::{
2323
/// An `Engine` provides low-level encoding and decoding operations that all other higher-level parts of the API use. Users of the library will generally not need to implement this.
2424
///
2525
/// Different implementations offer different characteristics. The library currently ships with
26-
/// a general-purpose [GeneralPurpose] impl that offers good speed and works on any CPU, with more choices
26+
/// [GeneralPurpose] that offers good speed and works on any CPU, with more choices
2727
/// coming later, like a constant-time one when side channel resistance is called for, and vendor-specific vectorized ones for more speed.
2828
///
2929
/// See [STANDARD] if you just want standard base64. Otherwise, when possible, it's

src/lib.rs

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//!
21
//! # Alphabets
32
//!
43
//! An [alphabet::Alphabet] defines what ASCII symbols are used to encode to or decode from.
@@ -12,46 +11,46 @@
1211
//! API provide a default, but otherwise the user must provide an `Engine` to use.
1312
//!
1413
//! 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.
1616
//!
1717
//! ## Config
1818
//!
1919
//! 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.
2122
//!
2223
//! [encode()] and [decode()] use the standard alphabet and default engine in an RFC 4648 standard
2324
//! setup.
2425
//!
2526
//! # Encoding
2627
//!
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
2829
//! convenience vs performance.
2930
//!
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 |
3636
//!
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.
3838
//!
3939
//! # Decoding
4040
//!
41-
//! Just as for encoding, there are different decoding functions available.
41+
//! Just as for encoding, there are different decoding methods available.
4242
//!
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 |
4948
//!
5049
//! Unlike encoding, where all possible input is valid, decoding can fail (see [DecodeError]).
5150
//!
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.
5554
//!
5655
//! # `Read` and `Write`
5756
//!
@@ -68,6 +67,43 @@
6867
//!
6968
//! See [display] for how to transparently base64 data via a `Display` implementation.
7069
//!
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+
//!
71107
//! # Panics
72108
//!
73109
//! If length calculations result in overflowing `usize`, a panic will result.

0 commit comments

Comments
 (0)