Skip to main content

parse_data_url

Function parse_data_url 

Source
pub fn parse_data_url(url: &str) -> Option<(&str, &str)>
Expand description

Parses a data URL, returning (full_mime, base64_payload).

Expects the format data:{type}/{subtype};base64,{payload} and validates that the entire input is a single data URL — the payload must be exclusively standard base64 characters ([A-Za-z0-9+/=]) and the mime carries no whitespace. That rules out strings where a data-URL prefix is followed by unrelated text (e.g. a tool output that happens to start with data:image/png;base64,XYZ\nfollowed by prose…); those round- trip as None so callers reliably pass them through as text.

Returns None for:

  • Strings missing the data: prefix (including any leading whitespace or content before it).
  • Strings missing the ;base64, marker.
  • Strings whose payload contains anything outside the standard base64 alphabet (newlines, spaces, trailing prose, etc.).
  • Strings whose mime portion contains ASCII whitespace.

#[inline] because this is on the hot path of every MCP content-block conversion (From<ContentBlock>, RichContentPart::from_text_or_data_url, and the per-leaf file_content extraction during log writes) and the body is a handful of cheap string ops — the call overhead would be a measurable fraction of the work.