use wasm_bindgen::prelude::*;
use crate::wasm_iter::ByteIterator;
use super::BytesMut;
#[allow(missing_docs)]
#[wasm_bindgen]
impl BytesMut {
#[wasm_bindgen(constructor)]
pub fn new_wasm() -> Self {
Self::new()
}
#[wasm_bindgen(js_name = "withCapacity")]
pub fn with_capacity_wasm(capacity: usize) -> Self {
Self::with_capacity(capacity)
}
#[wasm_bindgen(js_name = "fromBytes")]
pub fn from_bytes_wasm(data: &[u8]) -> BytesMut {
BytesMut::from(data)
}
#[wasm_bindgen(js_name = "fromString")]
pub fn from_string_wasm(s: &str) -> BytesMut {
BytesMut::from(s.as_bytes())
}
#[wasm_bindgen(js_name = "toBytes")]
pub fn to_bytes_wasm(&self) -> Vec<u8> {
self.as_slice().to_vec()
}
#[wasm_bindgen(js_name = "toString")]
pub fn to_string_wasm(&self) -> Result<String, JsError> {
core::str::from_utf8(self.as_slice())
.map(|s| s.to_string())
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "len")]
pub fn len_wasm(&self) -> usize {
self.len()
}
#[wasm_bindgen(js_name = "isEmpty")]
pub fn is_empty_wasm(&self) -> bool {
self.is_empty()
}
#[wasm_bindgen(js_name = "capacity")]
pub fn capacity_wasm(&self) -> usize {
self.capacity()
}
#[wasm_bindgen(js_name = "isInline")]
pub fn is_inline_wasm(&self) -> bool {
self.is_inline()
}
#[wasm_bindgen(js_name = "isHeap")]
pub fn is_heap_wasm(&self) -> bool {
self.is_heap()
}
#[wasm_bindgen(js_name = "remaining")]
pub fn remaining_wasm(&self) -> usize {
bytes::Buf::remaining(self)
}
#[wasm_bindgen(js_name = "advance")]
pub fn advance_wasm(&mut self, cnt: usize) -> Result<(), JsError> {
self
.try_advance(cnt)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "clear")]
pub fn clear_wasm(&mut self) {
self.clear();
}
#[wasm_bindgen(js_name = "truncate")]
pub fn truncate_wasm(&mut self, new_len: usize) {
self.truncate(new_len);
}
#[wasm_bindgen(js_name = "reserve")]
pub fn reserve_wasm(&mut self, additional: usize) {
self.reserve(additional);
}
#[wasm_bindgen(js_name = "resize")]
pub fn resize_wasm(&mut self, new_len: usize, value: u8) {
self.resize(new_len, value);
}
#[wasm_bindgen(js_name = "splitTo")]
pub fn split_to_wasm(&mut self, at: usize) -> Result<BytesMut, JsError> {
self
.try_split_to(at)
.map(|r| r.unwrap_or_else(BytesMut::from))
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "splitOff")]
pub fn split_off_wasm(&mut self, at: usize) -> Result<BytesMut, JsError> {
self
.try_split_off(at)
.map(|r| r.unwrap_or_else(BytesMut::from))
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "split")]
pub fn split_wasm(&mut self) -> BytesMut {
self.split().unwrap_or_else(BytesMut::from)
}
#[wasm_bindgen(js_name = "unsplit")]
pub fn unsplit_wasm(&mut self, other: BytesMut) -> Option<BytesMut> {
self.unsplit(other)
}
#[wasm_bindgen(js_name = "getU8")]
pub fn get_u8_wasm(&mut self) -> Result<u8, JsError> {
if bytes::Buf::remaining(self) < 1 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u8(self))
}
#[wasm_bindgen(js_name = "getI8")]
pub fn get_i8_wasm(&mut self) -> Result<i8, JsError> {
if bytes::Buf::remaining(self) < 1 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i8(self))
}
#[wasm_bindgen(js_name = "getU16")]
pub fn get_u16_wasm(&mut self) -> Result<u16, JsError> {
if bytes::Buf::remaining(self) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u16(self))
}
#[wasm_bindgen(js_name = "getU16Le")]
pub fn get_u16_le_wasm(&mut self) -> Result<u16, JsError> {
if bytes::Buf::remaining(self) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u16_le(self))
}
#[wasm_bindgen(js_name = "getI16")]
pub fn get_i16_wasm(&mut self) -> Result<i16, JsError> {
if bytes::Buf::remaining(self) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i16(self))
}
#[wasm_bindgen(js_name = "getI16Le")]
pub fn get_i16_le_wasm(&mut self) -> Result<i16, JsError> {
if bytes::Buf::remaining(self) < 2 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i16_le(self))
}
#[wasm_bindgen(js_name = "getU32")]
pub fn get_u32_wasm(&mut self) -> Result<u32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u32(self))
}
#[wasm_bindgen(js_name = "getU32Le")]
pub fn get_u32_le_wasm(&mut self) -> Result<u32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u32_le(self))
}
#[wasm_bindgen(js_name = "getI32")]
pub fn get_i32_wasm(&mut self) -> Result<i32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i32(self))
}
#[wasm_bindgen(js_name = "getI32Le")]
pub fn get_i32_le_wasm(&mut self) -> Result<i32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i32_le(self))
}
#[wasm_bindgen(js_name = "getU64")]
pub fn get_u64_wasm(&mut self) -> Result<u64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u64(self))
}
#[wasm_bindgen(js_name = "getU64Le")]
pub fn get_u64_le_wasm(&mut self) -> Result<u64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_u64_le(self))
}
#[wasm_bindgen(js_name = "getI64")]
pub fn get_i64_wasm(&mut self) -> Result<i64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i64(self))
}
#[wasm_bindgen(js_name = "getI64Le")]
pub fn get_i64_le_wasm(&mut self) -> Result<i64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_i64_le(self))
}
#[wasm_bindgen(js_name = "getF32")]
pub fn get_f32_wasm(&mut self) -> Result<f32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f32(self))
}
#[wasm_bindgen(js_name = "getF32Le")]
pub fn get_f32_le_wasm(&mut self) -> Result<f32, JsError> {
if bytes::Buf::remaining(self) < 4 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f32_le(self))
}
#[wasm_bindgen(js_name = "getF64")]
pub fn get_f64_wasm(&mut self) -> Result<f64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f64(self))
}
#[wasm_bindgen(js_name = "getF64Le")]
pub fn get_f64_le_wasm(&mut self) -> Result<f64, JsError> {
if bytes::Buf::remaining(self) < 8 {
return Err(JsError::new("not enough data"));
}
Ok(bytes::Buf::get_f64_le(self))
}
#[wasm_bindgen(js_name = "putSlice")]
pub fn put_slice_wasm(&mut self, data: &[u8]) {
self.extend_from_slice(data);
}
#[wasm_bindgen(js_name = "putU8")]
pub fn put_u8_wasm(&mut self, val: u8) {
bytes::BufMut::put_u8(self, val);
}
#[wasm_bindgen(js_name = "putI8")]
pub fn put_i8_wasm(&mut self, val: i8) {
bytes::BufMut::put_i8(self, val);
}
#[wasm_bindgen(js_name = "putU16")]
pub fn put_u16_wasm(&mut self, val: u16) {
bytes::BufMut::put_u16(self, val);
}
#[wasm_bindgen(js_name = "putU16Le")]
pub fn put_u16_le_wasm(&mut self, val: u16) {
bytes::BufMut::put_u16_le(self, val);
}
#[wasm_bindgen(js_name = "putI16")]
pub fn put_i16_wasm(&mut self, val: i16) {
bytes::BufMut::put_i16(self, val);
}
#[wasm_bindgen(js_name = "putI16Le")]
pub fn put_i16_le_wasm(&mut self, val: i16) {
bytes::BufMut::put_i16_le(self, val);
}
#[wasm_bindgen(js_name = "putU32")]
pub fn put_u32_wasm(&mut self, val: u32) {
bytes::BufMut::put_u32(self, val);
}
#[wasm_bindgen(js_name = "putU32Le")]
pub fn put_u32_le_wasm(&mut self, val: u32) {
bytes::BufMut::put_u32_le(self, val);
}
#[wasm_bindgen(js_name = "putI32")]
pub fn put_i32_wasm(&mut self, val: i32) {
bytes::BufMut::put_i32(self, val);
}
#[wasm_bindgen(js_name = "putI32Le")]
pub fn put_i32_le_wasm(&mut self, val: i32) {
bytes::BufMut::put_i32_le(self, val);
}
#[wasm_bindgen(js_name = "putU64")]
pub fn put_u64_wasm(&mut self, val: u64) {
bytes::BufMut::put_u64(self, val);
}
#[wasm_bindgen(js_name = "putU64Le")]
pub fn put_u64_le_wasm(&mut self, val: u64) {
bytes::BufMut::put_u64_le(self, val);
}
#[wasm_bindgen(js_name = "putI64")]
pub fn put_i64_wasm(&mut self, val: i64) {
bytes::BufMut::put_i64(self, val);
}
#[wasm_bindgen(js_name = "putI64Le")]
pub fn put_i64_le_wasm(&mut self, val: i64) {
bytes::BufMut::put_i64_le(self, val);
}
#[wasm_bindgen(js_name = "putF32")]
pub fn put_f32_wasm(&mut self, val: f32) {
bytes::BufMut::put_f32(self, val);
}
#[wasm_bindgen(js_name = "putF32Le")]
pub fn put_f32_le_wasm(&mut self, val: f32) {
bytes::BufMut::put_f32_le(self, val);
}
#[wasm_bindgen(js_name = "putF64")]
pub fn put_f64_wasm(&mut self, val: f64) {
bytes::BufMut::put_f64(self, val);
}
#[wasm_bindgen(js_name = "putF64Le")]
pub fn put_f64_le_wasm(&mut self, val: f64) {
bytes::BufMut::put_f64_le(self, val);
}
#[wasm_bindgen(js_name = "iter")]
pub fn iter_wasm(&self) -> ByteIterator {
ByteIterator::new(self.as_slice().to_vec())
}
}