use wasm_bindgen::prelude::*;
use crate::wasm_iter::CharIterator;
use super::Utf8BytesMut;
#[allow(missing_docs)]
#[wasm_bindgen]
impl Utf8BytesMut {
#[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 = "fromString")]
pub fn from_string_wasm(s: js_sys::JsString) -> Result<Utf8BytesMut, JsError> {
let s = crate::wasm::js_string_to_string(&s)?;
Ok(Utf8BytesMut::from(s))
}
#[wasm_bindgen(js_name = "fromBytesLossy")]
pub fn from_bytes_lossy_wasm(data: &[u8]) -> Utf8BytesMut {
Utf8BytesMut::from_utf8_lossy(crate::BytesMut::from(data))
}
#[wasm_bindgen(js_name = "toString")]
pub fn to_string_wasm(&self) -> String {
self.as_str().to_string()
}
#[wasm_bindgen(js_name = "toBytes")]
pub fn to_bytes_wasm(&self) -> Vec<u8> {
self.as_str().as_bytes().to_vec()
}
#[wasm_bindgen(js_name = "byteLength")]
pub fn len_wasm(&self) -> usize {
self.len()
}
#[wasm_bindgen(js_name = "charLength")]
pub fn char_length_wasm(&self) -> usize {
self.as_str().chars().count()
}
#[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 = "tryMakeInline")]
pub fn try_make_inline_wasm(&mut self) -> bool {
self.try_make_inline()
}
#[wasm_bindgen(js_name = "remainingBytes")]
pub fn remaining_bytes_wasm(&self) -> usize {
bytes::Buf::remaining(&self.inner)
}
#[wasm_bindgen(js_name = "advanceBytes")]
pub fn advance_bytes_wasm(&mut self, count: usize) -> Result<(), JsError> {
crate::utf8_buf::validate_char_boundary(self.as_str(), count)
.map_err(|e| JsError::new(&e.to_string()))?;
self
.inner
.try_advance(count)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "push")]
pub fn push_wasm(&mut self, ch: js_sys::JsString) -> Result<(), JsError> {
let c = crate::wasm::js_string_to_char(&ch)?;
self.push(c);
Ok(())
}
#[wasm_bindgen(js_name = "pushStr")]
pub fn push_str_wasm(&mut self, s: js_sys::JsString) -> Result<(), JsError> {
let s = crate::wasm::js_string_to_string(&s)?;
self.push_str(&s);
Ok(())
}
#[wasm_bindgen(js_name = "insertBytes")]
pub fn insert_wasm(&mut self, index: usize, ch: js_sys::JsString) -> Result<(), JsError> {
let ch = crate::wasm::js_string_to_char(&ch)?;
self
.try_insert(index, ch)
.map_err(|error| JsError::new(&error.to_string()))
}
#[wasm_bindgen(js_name = "insertStrBytes")]
pub fn insert_str_wasm(&mut self, index: usize, s: js_sys::JsString) -> Result<(), JsError> {
let s = crate::wasm::js_string_to_string(&s)?;
self
.try_insert_str(index, &s)
.map_err(|error| JsError::new(&error.to_string()))
}
#[wasm_bindgen(js_name = "replaceRangeBytes")]
pub fn replace_range_wasm(
&mut self,
start: usize,
end: usize,
replacement: js_sys::JsString,
) -> Result<(), JsError> {
let replacement = crate::wasm::js_string_to_string(&replacement)?;
self
.try_replace_range(start..end, &replacement)
.map_err(|error| JsError::new(&error.to_string()))
}
#[wasm_bindgen(js_name = "pop")]
pub fn pop_wasm(&mut self) -> Option<String> {
self.pop().map(|ch| ch.to_string())
}
#[wasm_bindgen(js_name = "clear")]
pub fn clear_wasm(&mut self) {
self.clear();
}
#[wasm_bindgen(js_name = "truncateBytes")]
pub fn truncate_wasm(&mut self, new_len: usize) -> Result<(), JsError> {
self
.try_truncate(new_len)
.map_err(|error| JsError::new(&error.to_string()))
}
#[wasm_bindgen(js_name = "reserve")]
pub fn reserve_wasm(&mut self, additional: usize) {
self.reserve(additional);
}
#[wasm_bindgen(js_name = "splitToBytes")]
pub fn split_to_wasm(&mut self, at: usize) -> Result<Utf8BytesMut, JsError> {
self
.try_split_to(at)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "splitOffBytes")]
pub fn split_off_wasm(&mut self, at: usize) -> Result<Utf8BytesMut, JsError> {
self
.try_split_off(at)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "split")]
pub fn split_wasm(&mut self) -> Utf8BytesMut {
self.split()
}
#[wasm_bindgen(js_name = "sliceBytes")]
pub fn slice_wasm(&self, start: usize, end: usize) -> Result<Utf8BytesMut, JsError> {
self
.try_slice(start..end)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "clone")]
pub fn clone_wasm(&self) -> Utf8BytesMut {
self.clone()
}
#[wasm_bindgen(js_name = "freezeShared")]
pub fn freeze_shared_wasm(&mut self) -> crate::utf8_bytes::wasm::WasmUtf8Bytes {
core::mem::take(self).freeze_shared().into()
}
#[wasm_bindgen(js_name = "freezeCompact")]
pub fn freeze_compact_wasm(&mut self) -> crate::utf8_bytes::wasm::WasmCompactUtf8Bytes {
core::mem::take(self).freeze_compact().into()
}
#[wasm_bindgen(js_name = "iter")]
pub fn iter_wasm(&self) -> CharIterator {
CharIterator::new(self.as_str())
}
}