Expand description
Shared encoding / codec utilities for the pocopine workspace.
The point of this crate is that crates never re-implement encoding helpers or
their serde adapters and never reach for base64 or percent-encoding
directly. Add new codecs here rather than inlining them per crate.
use pocopine_codec::{base64_decode, base64_encode};
assert_eq!(base64_encode(b"hi"), "aGk=");
assert_eq!(base64_decode("aGk=").unwrap(), b"hi");Percent-encoding goes through one component encoder (RFC 3986
“unreserved” set — what URL path segments, query parts, and fragments
want) plus a lossy decoder. Pass a custom AsciiSet to
percent_encode_set when a backend needs a different escape set.
use pocopine_codec::{percent_decode, percent_encode};
assert_eq!(percent_encode("a b/c~d"), "a%20b%2Fc~d");
assert_eq!(percent_decode("a%20b+c", true), "a b c");For a Vec<u8> struct field that should serialize as a base64 string, use the
base64_bytes serde adapter:
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Chunk {
#[serde(with = "pocopine_codec::base64_bytes", default, skip_serializing_if = "Vec::is_empty")]
payload: Vec<u8>,
}This crate is no_std (it only needs alloc).
Re-exports§
pub use base64;pub use percent_encoding;
Modules§
- base64_
bytes - Serde adapter for a
Vec<u8>field encoded as a base64 string.
Structs§
- Ascii
Set - Represents a set of characters or bytes in the ASCII range.
Constants§
- CONTROLS
- The set of 0x00 to 0x1F (C0 controls), and 0x7F (DEL).
- NON_
ALPHANUMERIC - Everything that is not an ASCII letter or digit.
Functions§
- base64_
decode - Decode a standard (padded) base64 string.
- base64_
encode - Encode bytes as a standard (padded) base64 string.
- percent_
decode - Percent-decode
s, lossily (invalid UTF-8 becomes U+FFFD, and a%not followed by two hex digits is passed through verbatim). - percent_
encode - Percent-encode
swith the RFC 3986 component set (see [COMPONENT]). - percent_
encode_ into - Percent-encode
swith the component set, appending intoout. - percent_
encode_ set - Percent-encode
swith a caller-suppliedAsciiSet.