1pub(crate) mod builder;
3pub(crate) mod parser;
4use crate::Error;
5
6#[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 arr[..copy_len].copy_from_slice(&bytes[..copy_len]);
15 arr
16}
17
18#[inline]
20pub fn slice_to_str(arr: &[u8]) -> Result<&str, Error> {
21 Ok(str::from_utf8(arr).unwrap())
22}
23
24#[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}