#[inline]
pub fn into_arr4(slice: &[u8], pos: usize) -> [u8; 4] {
let mut arr = [0; 4];
arr.copy_from_slice(&slice[pos..pos + 4]);
arr
}
#[inline]
pub fn u32_from_le_bytes(slice: &[u8], pos: usize) -> u32 {
u32::from_le_bytes(into_arr4(slice, pos))
}
#[inline]
pub fn f32_from_le_bytes(slice: &[u8], pos: usize) -> f32 {
f32::from_le_bytes(into_arr4(slice, pos))
}
#[inline]
pub fn into_arr2(slice: &[u8], pos: usize) -> [u8; 2] {
let mut arr = [0; 2];
arr.copy_from_slice(unsafe { slice.get_unchecked(pos..pos + 2) });
arr
}
#[inline]
pub fn u16_from_le_bytes(slice: &[u8], pos: usize) -> u16 {
u16::from_le_bytes(into_arr2(slice, pos))
}
#[inline]
pub fn u8_from_le_bytes(slice: &[u8], pos: usize) -> u8 {
u8::from_le_bytes({
let mut arr = [0];
arr.copy_from_slice(unsafe { slice.get_unchecked(pos..pos + 1) });
arr
})
}
#[inline]
pub fn date_string(x: u32) -> String {
let [y, m, d] = [x / 10000, x % 10000 / 100, x % 10000 % 100];
let fill = |x: u32| if x > 9 { "" } else { "0" };
format!("{}-{}{}-{}{}", y, fill(m), m, fill(d), d)
}
pub fn ser_date_string<S>(date: &u32, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&crate::bytes_helper::date_string(*date))
}
pub fn ser_code_string<S>(code: &u32, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!("{code:06}"))
}