1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// SPDX-License-Identifier: MIT
//! # multibase
//!
//! Implementation of [multibase](https://github.com/multiformats/multibase) in Rust.
// The `build_base_enum!` macro generates doc comments containing base alphabet
// strings (e.g. `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`)
// and variant names like `Base256Emoji`, which clippy::doc_markdown flags. These
// are intentional alphabet literals, not identifiers requiring backticks.
extern crate alloc;
use ;
pub use Base;
pub use EncodedString;
pub use ;
/// Decode the base string.
///
/// # Examples
///
/// ```
/// use multi_base::{Base, decode};
///
/// assert_eq!(
/// decode("zCn8eVZg", true).unwrap(),
/// (Base::Base58Btc, b"hello".to_vec())
/// );
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The input string is empty ([`Error::EmptyInput`])
/// - The base code prefix is unknown ([`Error::UnknownBase`])
/// - The encoded data is invalid for the detected base
/// Encode with the given byte slice to base string.
///
/// # Examples
///
/// ```
/// use multi_base::{Base, encode};
///
/// assert_eq!(encode(Base::Base58Btc, b"hello"), "zCn8eVZg");
/// ```
///
/// # Performance
///
/// This function pre-allocates the exact capacity needed and constructs the
/// result string efficiently by prepending the base code without requiring
/// reallocation or memory moves.
/// Encode with the given byte slice to base string, writing into an existing buffer.
///
/// This is a zero-copy variant of [`encode`] that reuses an existing String buffer,
/// avoiding allocations when encoding multiple values in a loop.
///
/// The buffer will be cleared before encoding, then filled with the base code prefix
/// followed by the encoded data.
///
/// # Examples
///
/// ```
/// use multi_base::{Base, encode_into};
///
/// let mut buffer = String::new();
///
/// // Encode multiple values reusing the same buffer
/// encode_into(Base::Base58Btc, b"hello", &mut buffer);
/// assert_eq!(buffer, "zCn8eVZg");
///
/// encode_into(Base::Base64, b"world", &mut buffer);
/// assert_eq!(buffer, "md29ybGQ");
/// ```
///
/// # Performance
///
/// When encoding many values, this function can be significantly faster than
/// [`encode`] as it reuses the allocated buffer instead of allocating a new
/// String for each encoding operation.
/// Decode the base string, writing the result into an existing buffer.
///
/// This is a zero-copy variant of [`decode`] that reuses an existing `Vec<u8>` buffer,
/// avoiding allocations when decoding multiple values in a loop.
///
/// The buffer will be cleared before decoding, then filled with the decoded bytes.
/// Returns the base encoding that was detected from the prefix.
///
/// # Examples
///
/// ```
/// use multi_base::{Base, decode_into};
///
/// let mut buffer = Vec::new();
///
/// // Decode multiple values reusing the same buffer
/// let base = decode_into("zCn8eVZg", true, &mut buffer).unwrap();
/// assert_eq!(base, Base::Base58Btc);
/// assert_eq!(buffer, b"hello");
///
/// let base = decode_into("md29ybGQ", true, &mut buffer).unwrap();
/// assert_eq!(base, Base::Base64);
/// assert_eq!(buffer, b"world");
/// ```
///
/// # Performance
///
/// When decoding many values, this function can be significantly faster than
/// [`decode`] as it reuses the allocated buffer instead of allocating a new
/// `Vec<u8>` for each decoding operation.
///
/// # Errors
///
/// Returns an error if:
/// - The input string is empty
/// - The base code prefix is unknown
/// - The encoded data is invalid for the specified base
/// Encode with the given byte slice and return a validated `EncodedString`.
///
/// This is a convenience function that combines [`encode`] with [`EncodedString::new`],
/// providing type-level guarantees that the returned string is a valid multibase encoding.
///
/// # Examples
///
/// ```
/// use multi_base::{Base, encode_to_validated};
///
/// let encoded = encode_to_validated(Base::Base58Btc, b"hello");
/// assert_eq!(encoded.base(), Base::Base58Btc);
/// assert_eq!(encoded.as_str(), "zCn8eVZg");
///
/// // Decode directly from the validated string
/// let decoded = encoded.decode().unwrap();
/// assert_eq!(decoded, b"hello");
/// ```
///
/// # Performance
///
/// This function has the same performance characteristics as [`encode`].
/// The validation overhead is negligible (just checking the base code).
///
/// # Panics
///
/// Panics if the encoded string fails validation. Since [`encode`] always
/// produces a valid multibase string, this is unreachable in practice.
/// Parse a multibase string into a validated `EncodedString`.
///
/// This is equivalent to [`EncodedString::new`] but provides a standalone
/// function for consistency with other API functions.
///
/// # Examples
///
/// ```
/// use multi_base::{Base, parse_encoded};
///
/// let encoded = parse_encoded("zCn8eVZg").unwrap();
/// assert_eq!(encoded.base(), Base::Base58Btc);
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The input string is empty
/// - The base code prefix is unknown