From ac1c1d9202ac8cb53ab7410e4060b7740431cd9d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:19:05 -0500 Subject: [PATCH] encoding: reuse shared Base64 writers across four consumers --- Cargo.lock | 19 +++++++++++++++++++ Cargo.toml | 1 + crates/aep/Cargo.toml | 1 + crates/aep/src/export/icc.rs | 14 +------------- crates/automation/Cargo.toml | 1 + crates/automation/src/base64.rs | 23 +++++++++++++---------- crates/track/Cargo.toml | 1 + crates/track/src/roto/rle.rs | 13 +------------ crates/ui-egui/Cargo.toml | 1 + crates/ui-egui/src/control.rs | 14 +------------- 10 files changed, 40 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7af09affa..4f94415c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1103,6 +1103,11 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-codepage" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" + [[package]] name = "craft-doc" version = "0.1.0" @@ -1220,6 +1225,16 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-mime" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "chrono", + "craft-codepage", + "encoding_rs", +] + [[package]] name = "craft-pcraft-format" version = "0.1.0" @@ -1604,6 +1619,7 @@ name = "effectcraft-aep" version = "0.24.1" dependencies = [ "craft-aep", + "craft-mime", "effectcraft-color", "effectcraft-effects", "effectcraft-keyframe", @@ -1619,6 +1635,7 @@ dependencies = [ name = "effectcraft-automation" version = "0.24.1" dependencies = [ + "craft-mime", "effectcraft-engine", "effectcraft-host", "effectcraft-model", @@ -2116,6 +2133,7 @@ dependencies = [ name = "effectcraft-track" version = "0.24.1" dependencies = [ + "craft-mime", "effectcraft-raster", "effectcraft-segment", "rayon", @@ -2130,6 +2148,7 @@ version = "0.24.1" dependencies = [ "bytemuck", "chrono", + "craft-mime", "craft-quit", "craft-ui", "craft-update", diff --git a/Cargo.toml b/Cargo.toml index ae0fd3d09..292069b97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/effectcraft" [workspace.dependencies] +craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", version = "=0.1.0", default-features = false, features = ["read"] } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } diff --git a/crates/aep/Cargo.toml b/crates/aep/Cargo.toml index 0523edb9b..1189df8dc 100644 --- a/crates/aep/Cargo.toml +++ b/crates/aep/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-mime = { workspace = true } craft-aep = { workspace = true } effectcraft-time = { workspace = true } effectcraft-color = { workspace = true } diff --git a/crates/aep/src/export/icc.rs b/crates/aep/src/export/icc.rs index ae6db7893..04a900d00 100644 --- a/crates/aep/src/export/icc.rs +++ b/crates/aep/src/export/icc.rs @@ -224,19 +224,7 @@ pub(crate) fn profile_json(icc: &[u8], name: &str) -> String { /// Standard base64 with padding. pub(crate) fn base64(data: &[u8]) -> String { - const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - let mut out = String::with_capacity(data.len().div_ceil(3) * 4); - for c in data.chunks(3) { - let n = (u32::from(c[0]) << 16) | (u32::from(c.get(1).copied().unwrap_or(0)) << 8) | u32::from(c.get(2).copied().unwrap_or(0)); - for (i, shift) in [18u32, 12, 6, 0].iter().enumerate() { - if i <= c.len() { - out.push(char::from(T[((n >> shift) & 63) as usize])); - } else { - out.push('='); - } - } - } - out + craft_mime::base64::STANDARD.encode(data) } #[cfg(test)] diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml index e0caf3218..23cff63c8 100644 --- a/crates/automation/Cargo.toml +++ b/crates/automation/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-mime = { workspace = true } effectcraft-engine = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/automation/src/base64.rs b/crates/automation/src/base64.rs index 90002c600..21dd66d7e 100644 --- a/crates/automation/src/base64.rs +++ b/crates/automation/src/base64.rs @@ -1,16 +1,7 @@ //! Standard base64 (RFC 4648 §4, padded): MCP image content and the control channel's inline PNGs. -const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - pub fn encode(data: &[u8]) -> String { - let mut s = String::with_capacity(data.len().div_ceil(3) * 4); - for c in data.chunks(3) { - let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32; - for i in 0..4 { - s.push(if i <= c.len() { A[(n >> (18 - 6 * i) & 63) as usize] as char } else { '=' }); - } - } - s + craft_mime::base64::STANDARD.encode(data) } /// Decode, ignoring ASCII whitespace; `None` on any other invalid input. @@ -48,6 +39,18 @@ pub fn decode(s: &str) -> Option> { #[cfg(test)] mod tests { + #[test] + fn shared_encoder_round_trips_every_byte_and_remainder() { + let all: Vec = (0..=255).cycle().take(513).collect(); + for length in 0..=all.len() { + let input = &all[..length]; + let encoded = super::encode(input); + assert_eq!(encoded.len(), length.div_ceil(3) * 4); + assert_eq!(super::decode(&encoded).as_deref(), Some(input)); + assert_eq!(encoded.bytes().rev().take_while(|b| *b == b'=').count(), (3 - length % 3) % 3); + } + } + #[test] fn rfc4648_vectors() { for (i, o) in [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] { diff --git a/crates/track/Cargo.toml b/crates/track/Cargo.toml index cd9daae36..f374a81f8 100644 --- a/crates/track/Cargo.toml +++ b/crates/track/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-mime = { workspace = true } effectcraft-raster = { workspace = true } effectcraft-segment = { workspace = true } rayon = { workspace = true } diff --git a/crates/track/src/roto/rle.rs b/crates/track/src/roto/rle.rs index 914bcad83..4a4152d82 100644 --- a/crates/track/src/roto/rle.rs +++ b/crates/track/src/roto/rle.rs @@ -1,19 +1,8 @@ //! Compact text encodings of mattes for project storage: run-length coding (LEB128 varints) //! wrapped in standard base64 (RFC 4648 §4). -const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - pub fn base64_encode(data: &[u8]) -> String { - let mut out = String::with_capacity(data.len().div_ceil(3) * 4); - for c in data.chunks(3) { - let b = [c[0], *c.get(1).unwrap_or(&0), *c.get(2).unwrap_or(&0)]; - let n = (b[0] as u32) << 16 | (b[1] as u32) << 8 | b[2] as u32; - out.push(B64[(n >> 18) as usize & 63] as char); - out.push(B64[(n >> 12) as usize & 63] as char); - out.push(if c.len() > 1 { B64[(n >> 6) as usize & 63] as char } else { '=' }); - out.push(if c.len() > 2 { B64[n as usize & 63] as char } else { '=' }); - } - out + craft_mime::base64::STANDARD.encode(data) } pub fn base64_decode(s: &str) -> Option> { diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 3c5862594..81f2023c4 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-mime = { workspace = true } craft-ui = { workspace = true } craft-quit = { workspace = true, features = ["egui"] } effectcraft-time = { workspace = true } diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 9f86c7ace..b1717d55e 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -803,19 +803,7 @@ fn render_frame(app: &EffectcraftApp, p: &Value) -> Outcome { /// Standard base64 (RFC 4648, padded). pub fn base64(data: &[u8]) -> String { - const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - let mut s = String::with_capacity(data.len().div_ceil(3) * 4); - for c in data.chunks(3) { - let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32; - for i in 0..4 { - if i <= c.len() { - s.push(A[(n >> (18 - 6 * i) & 63) as usize] as char); - } else { - s.push('='); - } - } - } - s + craft_mime::base64::STANDARD.encode(data) } /// Builds the `ui.inspect` reply: window size and pixels per point, fps, UI state with the