Functional Weave
Code in Rust

encoding.utf8@1.0.0

impl/rust/utf8_decode.rs

1,207 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

/// UTF-8 bytes back to text, strictly (RFC 3629): `String::from_utf8`
/// refuses overlong forms, encoded surrogates, code points past U+10FFFF,
/// truncated sequences and stray continuation bytes, and keeps a leading
/// byte order mark.
///
/// # Panics
/// Panics if a value is outside 0-255 or the bytes are not valid UTF-8.
pub fn utf8_decode(bytes: &[i64]) -> String {
    let mut raw = Vec::with_capacity(bytes.len());
    for &b in bytes {
        if !(0..=255).contains(&b) {
            panic!("bytes must be a list of integers from 0 to 255");
        }
        raw.push(b as u8);
    }
    match String::from_utf8(raw) {
        Ok(text) => text,
        Err(_) => panic!("bytes are not valid UTF-8"),
    }
}

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(&utf8_decode(&bytes))
}