Functional Weave
Code in Python

encoding.base64@1.0.0

impl/rust/base64_url_encode.rs

942 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::encoding_base64_base64_encode::base64_encode;  ← base64Encode, another function of this group · built into the same file, even by a slim install

/// Bytes as base64url (RFC 4648 section 5) without padding, as JWTs use it.
///
/// # Panics
/// Panics if any value is outside 0-255.
pub fn base64_url_encode(bytes: &[i64]) -> String {
    base64_encode(bytes)
        .trim_end_matches('=')
        .chars()
        .map(|c| match c {
            '+' => '-',
            '/' => '_',
            other => other,
        })
        .collect()
}

pub fn fune_vector(args: &[Value]) -> Value {
    let bytes: Vec<i64> = match &args[0] {
        Value::Arr(items) => items
            .iter()
            .map(|item| match item {
                Value::Int(i) => *i,
                _ => panic!("bytes must be a list of integers from 0 to 255"),
            })
            .collect(),
        _ => panic!("bytes must be a list of integers from 0 to 255"),
    };
    Value::str(&base64_url_encode(&bytes))
}