Repository navigation
refactor(rust): replace the hand-rolled base64 and hex codecs with crates - #1132
Merged
Merged
Conversation
`functions.rs` hand-rolled a standard-alphabet base64 encoder and a strict
decoder (~58 lines, including a `clippy::cast_possible_truncation` opt-out
for its shifts) and `cli/serialize.rs` hand-rolled hex. Both are replaced by
the crates that cover them: `base64`'s `general_purpose::STANDARD` engine,
which is byte-for-byte the configuration the `{"$bytes": "<base64>"}` wire
format documents, and `hex::encode`.
`base64` is an unconditional dependency because the function-worker path is
core, not CLI; `hex` sits behind the `cli` feature. Both are zero-dependency
and `base64` was already in the lockfile transitively.
The colocated tests for the deleted functions go with them. The protocol
tests one level up are untouched and still pass, including the malformed
inputs the old decoder rejected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1130.
Deletes the hand-rolled binary codecs in the core and replaces them with the crates that cover them.
packages/rust/src/functions.rs:BASE64_ALPHABET,base64_encode,base64_decode(~58 lines) →base64::engine::general_purpose::STANDARD. The#[expect(clippy::cast_possible_truncation)]the decoder's shifts needed goes with them.packages/rust/src/cli/serialize.rs:hex_encode(7 lines) →hex::encode. Taken rather than left alone: seven lines offormat!("{b:02x}")in a loop is the same hand-rolled-infrastructure shape as the base64, andhexis zero-dependency and CLI-gated, so a library consumer never sees it.Net −90 lines in
packages/rust/src.Dependencies
base64 = "0.22"is unconditional —functions.rsis core, reachable by a library consumer through the function-worker path, not the CLI. It is zero-dependency,no_std-capable, and was already inCargo.locktransitively, so the lockfile gains exactly one new entry (hex).hex = "0.4"isoptional = truebehind theclifeature.Byte-identical output
STANDARDis the standard alphabet with required padding and no line wrapping, which is whatdocs/reference/config.mddocuments for{"$bytes": "<base64>"}and what the embeddings plugin's stdlibbase64decodes.hex::encodeis lowercase, two chars per byte, no separator.Decoder strictness is preserved on every path the deleted tests covered. Verified with a throwaway probe against
parse_response(removed before committing) — each of"abc"(length not a multiple of 4),"a!=="(byte outside the alphabet),"ab=c","ab==cd=="and"a==="(padding outside the close of the final chunk) still fails the query with"$bytes" is not valid base64, and"AQI="still decodes to[1, 2].One input is now rejected that the old decoder accepted: a final character carrying non-canonical trailing bits, e.g.
"AQJ=". No conforming encoder emits it, no test covered it, and the old behavior (silently dropping the stray bits) was not a documented promise. Called out here rather than configured away, becausedecode_allow_trailing_bitsis the laxer setting and matching a hand-rolled quirk is not worth carrying.Tests
The colocated tests for the deleted functions are deleted with them (
base64_round_trips_every_padding_length,base64_known_vectors,base64_decode_rejects_malformed_input,hex_encode_matches_lowercase_spec) — a test for a function that no longer exists is not a test. The protocol-level tests (request_line_encodes_every_scalar_shape,meta_does_not_change_the_decoded_value,response_defects_are_named,blob_cell_becomes_hex_string) are unchanged and pass.blob_cell_becomes_hex_stringpicks up thehex_encode_matches_lowercase_specvector (00 0f ff→000fff) at the public seam, so the zero-padding assertion survives the deletion rather than being dropped.No new tests restating crate behavior.
unit mutationon the changed lines: 2 mutants, 0 survivors.Coverage
No floor re-pinned.
cargo +nightly llvm-cov --lib --features cli --branch, same command on both sides:main(12a73dd)The deletion removes 18 fully-covered branches and no uncovered ones, so
branchdips a little while missed-branch count is identical (128 both sides). Everything stays well clear of[rust.coverage], so the floors are untouched.Manual exercise
Built the CLI (
cargo build --features cli --bin dirsql) and round-tripped real BLOBs through a[[dirsql.function]]worker that prints the request line it receives.AN6tvu/wcarries a/, confirming the standard (not URL-safe) alphabet;Zm9vYg==is verbatim the vector the deletedbase64_known_vectorsasserted. The worker is a Python script decoding with stdlibbase64.b64decode(..., validate=True), i.e. the same decoder the embeddings plugin uses, and it accepted the payload.Hex, through the HTTP API (the path
cli/serialize.rsserves):000fffdeadis verbatim the vector the deletedhex_encode_matches_lowercase_specasserted.Parity
No public API change — both functions were private.
PARITY.mdis untouched.E2E Verification
cargo test --features cli --tests, all suites green)packages/python/e2e-attestations/<branch>.jsonreceipt written ifpackages/pythonchanged (just e2e-attest-python)packages/ts/e2e-attestations/<branch>.jsonreceipt written ifpackages/tschanged (just e2e-attest-ts)just e2e-attest-python;just e2e-attest-ts;cargo test --manifest-path packages/rust/Cargo.toml --features cli --tests;cargo test --lib --features cli;just preflightjust preflight0 failing pairs / 3 skipped.Changelog / Migrations
<root>/<pkg>/changelog.d/for each changed package (packages/rust/changelog.d/2026-09-18-codec-crates.md, Changed)<root>/<pkg>/migrations.d/(or: not required -- additive/bugfix only) — not required, behavior-preserving refactor