shape_runtime/stdlib/
byte_utils.rs1use shape_value::ValueWord;
7use std::sync::Arc;
8
9pub fn bytes_from_array(val: &ValueWord) -> Result<Vec<u8>, String> {
13 let arr = val
14 .as_any_array()
15 .ok_or_else(|| "expected an Array<int> of bytes".to_string())?
16 .to_generic();
17 let mut bytes = Vec::with_capacity(arr.len());
18 for item in arr.iter() {
19 let byte_val = item
20 .as_i64()
21 .or_else(|| item.as_f64().map(|n| n as i64))
22 .ok_or_else(|| "array elements must be integers (0-255)".to_string())?;
23 if !(0..=255).contains(&byte_val) {
24 return Err(format!("byte value out of range: {}", byte_val));
25 }
26 bytes.push(byte_val as u8);
27 }
28 Ok(bytes)
29}
30
31pub fn bytes_to_array(bytes: &[u8]) -> ValueWord {
33 let items: Vec<ValueWord> = bytes
34 .iter()
35 .map(|&b| ValueWord::from_i64(b as i64))
36 .collect();
37 ValueWord::from_array(Arc::new(items))
38}