1use std::{array::TryFromSliceError, ops::Range, str};
18
19use arrow_schema::ArrowError;
20
21use std::fmt::Debug;
22use std::slice::SliceIndex;
23
24#[inline]
25pub(crate) fn slice_from_slice<I: SliceIndex<[u8]> + Clone + Debug>(
26 bytes: &[u8],
27 index: I,
28) -> Result<&I::Output, ArrowError> {
29 bytes.get(index.clone()).ok_or_else(|| {
30 ArrowError::InvalidArgumentError(format!(
31 "Tried to extract byte(s) {index:?} from {}-byte buffer",
32 bytes.len(),
33 ))
34 })
35}
36pub(crate) fn array_from_slice<const N: usize>(
37 bytes: &[u8],
38 offset: usize,
39) -> Result<[u8; N], ArrowError> {
40 let bytes = slice_from_slice(bytes, offset..offset + N)?;
41 bytes.try_into().map_err(map_try_from_slice_error)
42}
43
44pub(crate) fn map_try_from_slice_error(e: TryFromSliceError) -> ArrowError {
46 ArrowError::InvalidArgumentError(e.to_string())
47}
48
49pub(crate) fn first_byte_from_slice(slice: &[u8]) -> Result<&u8, ArrowError> {
50 slice
51 .first()
52 .ok_or_else(|| ArrowError::InvalidArgumentError("Received empty bytes".to_string()))
53}
54
55pub(crate) fn string_from_slice(slice: &[u8], range: Range<usize>) -> Result<&str, ArrowError> {
57 str::from_utf8(slice_from_slice(slice, range)?)
58 .map_err(|_| ArrowError::InvalidArgumentError("invalid UTF-8 string".to_string()))
59}