Skip to main content

proka_exec/utils/
mod.rs

1//! Utilities to parse proka exec headers.
2pub(crate) mod builder;
3pub(crate) mod parser;
4use crate::Error;
5
6/// Convert a `&str` to a specified-length array.
7#[inline]
8pub fn str_to_array<const N: usize>(s: &str) -> [u8; N] {
9    let mut arr: [u8; N] = [0u8; N];
10    let bytes = s.as_bytes();
11    let copy_len = bytes.len().min(N);
12
13    // Copy to slice and return
14    arr[..copy_len].copy_from_slice(&bytes[..copy_len]);
15    arr
16}
17
18/// Convert the slice to a UTF-8 string.
19#[inline]
20pub fn slice_to_str(arr: &[u8]) -> Result<&str, Error> {
21    Ok(str::from_utf8(arr).unwrap())
22}
23
24// Tests...
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    const TEXT: &str = "Hello world";
30    const RAW: &[u8] = &*b"Hello world";
31
32    #[test]
33    fn test_str_to_array() {
34        let result: [u8; 11] = str_to_array(TEXT);
35        assert_eq!(&result, RAW);
36    }
37
38    #[test]
39    fn test_slice_to_str() {
40        let result = slice_to_str(RAW).unwrap();
41        assert_eq!(result, TEXT);
42    }
43}