binarystream/types/signed/
int24.rs1use napi_derive::napi;
2use napi::{bindgen_prelude::FromNapiValue, Result};
3use crate::binary::{ BinaryStream, Endianness };
4
5#[napi]
6#[derive(Clone)]
7pub struct Int24 {}
13
14#[napi]
15impl Int24 {
16 #[napi]
17 pub fn read(stream: &mut BinaryStream, endian: Option<Endianness>) -> Result<i32> {
23 let endian = match endian {
24 Some(endian) => endian,
25 None => Endianness::Big,
26 };
27
28 let bytes = match stream.read(3) {
29 Ok(bytes) => bytes,
30 Err(err) => return Err(err)
31 };
32
33 match endian {
34 Endianness::Big => Ok(i32::from_be_bytes([0, bytes[0], bytes[1], bytes[2]])),
35 Endianness::Little => Ok(i32::from_le_bytes([bytes[0], bytes[1], bytes[2], 0])),
36 }
37 }
38
39 #[napi]
40 pub fn write(stream: &mut BinaryStream, value: i32, endian: Option<Endianness>) {
46 let endian = match endian {
47 Some(endian) => endian,
48 None => Endianness::Big,
49 };
50
51 match endian {
52 Endianness::Big => stream.write(value.to_be_bytes()[1..].to_vec()),
53 Endianness::Little => stream.write(value.to_le_bytes()[..3].to_vec()),
54 }
55 }
56}
57
58impl FromNapiValue for Int24 {
59 unsafe fn from_napi_value(_: napi::sys::napi_env, _: napi::sys::napi_value) -> Result<Self> {
60 Ok(Int24 {})
61 }
62}