use wasm_bindgen::prelude::*;
use crate::Utf8Buf as _;
use crate::bytes::strategy::compact::Compact;
use crate::bytes::strategy::shared::Shared;
use crate::wasm_iter::CharIterator;
type SharedUtf8Bytes = super::Utf8Bytes<Shared>;
type CompactUtf8BytesInner = super::Utf8Bytes<Compact>;
#[wasm_bindgen(js_name = "Utf8Bytes")]
pub struct WasmUtf8Bytes {
inner: SharedUtf8Bytes,
}
impl From<SharedUtf8Bytes> for WasmUtf8Bytes {
fn from(inner: SharedUtf8Bytes) -> Self {
Self { inner }
}
}
#[allow(missing_docs)]
#[wasm_bindgen(js_class = "Utf8Bytes")]
impl WasmUtf8Bytes {
#[wasm_bindgen(constructor)]
pub fn new_wasm() -> Self {
Self {
inner: SharedUtf8Bytes::new(),
}
}
#[wasm_bindgen(js_name = "fromString")]
pub fn from_string_wasm(s: &str) -> WasmUtf8Bytes {
WasmUtf8Bytes {
inner: SharedUtf8Bytes::from(s),
}
}
#[wasm_bindgen(js_name = "fromStatic")]
pub fn from_static_wasm(s: &str) -> WasmUtf8Bytes {
WasmUtf8Bytes {
inner: SharedUtf8Bytes::from(s),
}
}
#[wasm_bindgen(js_name = "toString")]
pub fn to_string_wasm(&self) -> String {
self.inner.as_str().to_string()
}
#[wasm_bindgen(js_name = "toBytes")]
pub fn to_bytes_wasm(&self) -> Vec<u8> {
self.inner.as_str().as_bytes().to_vec()
}
#[wasm_bindgen(js_name = "len")]
pub fn len_wasm(&self) -> usize {
self.inner.len()
}
#[wasm_bindgen(js_name = "isEmpty")]
pub fn is_empty_wasm(&self) -> bool {
self.inner.is_empty()
}
#[wasm_bindgen(js_name = "isInline")]
pub fn is_inline_wasm(&self) -> bool {
self.inner.is_inline()
}
#[wasm_bindgen(js_name = "isHeap")]
pub fn is_heap_wasm(&self) -> bool {
self.inner.is_heap()
}
#[wasm_bindgen(js_name = "remaining")]
pub fn remaining_wasm(&self) -> usize {
bytes::Buf::remaining(self.inner.as_inner())
}
#[wasm_bindgen(js_name = "advance")]
pub fn advance_wasm(&mut self, cnt: usize) -> Result<(), JsError> {
self
.inner
.validate_char_boundary(cnt)
.map_err(|e| JsError::new(&e.to_string()))?;
self
.inner
.inner
.try_advance(cnt)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "splitTo")]
pub fn split_to_wasm(&mut self, at: usize) -> Result<WasmUtf8Bytes, JsError> {
self
.inner
.try_split_to(at)
.map(Into::into)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "splitOff")]
pub fn split_off_wasm(&mut self, at: usize) -> Result<WasmUtf8Bytes, JsError> {
self
.inner
.try_split_off(at)
.map(Into::into)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "slice")]
pub fn slice_wasm(&self, start: usize, end: usize) -> Result<WasmUtf8Bytes, JsError> {
self
.inner
.try_slice(start..end)
.map(Into::into)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "iter")]
pub fn iter_wasm(&self) -> CharIterator {
CharIterator::new(self.inner.as_str())
}
}
#[wasm_bindgen(js_name = "CompactUtf8Bytes")]
pub struct WasmCompactUtf8Bytes {
inner: CompactUtf8BytesInner,
}
impl From<CompactUtf8BytesInner> for WasmCompactUtf8Bytes {
fn from(inner: CompactUtf8BytesInner) -> Self {
Self { inner }
}
}
#[allow(missing_docs)]
#[wasm_bindgen(js_class = "CompactUtf8Bytes")]
impl WasmCompactUtf8Bytes {
#[wasm_bindgen(constructor)]
pub fn new_wasm() -> Self {
Self {
inner: CompactUtf8BytesInner::new(),
}
}
#[wasm_bindgen(js_name = "fromString")]
pub fn from_string_wasm(s: &str) -> WasmCompactUtf8Bytes {
WasmCompactUtf8Bytes {
inner: CompactUtf8BytesInner::from(s),
}
}
#[wasm_bindgen(js_name = "fromStatic")]
pub fn from_static_wasm(s: &str) -> WasmCompactUtf8Bytes {
WasmCompactUtf8Bytes {
inner: CompactUtf8BytesInner::from(s),
}
}
#[wasm_bindgen(js_name = "toString")]
pub fn to_string_wasm(&self) -> String {
self.inner.as_str().to_string()
}
#[wasm_bindgen(js_name = "toBytes")]
pub fn to_bytes_wasm(&self) -> Vec<u8> {
self.inner.as_str().as_bytes().to_vec()
}
#[wasm_bindgen(js_name = "len")]
pub fn len_wasm(&self) -> usize {
self.inner.len()
}
#[wasm_bindgen(js_name = "isEmpty")]
pub fn is_empty_wasm(&self) -> bool {
self.inner.is_empty()
}
#[wasm_bindgen(js_name = "isInline")]
pub fn is_inline_wasm(&self) -> bool {
self.inner.is_inline()
}
#[wasm_bindgen(js_name = "isHeap")]
pub fn is_heap_wasm(&self) -> bool {
self.inner.is_heap()
}
#[wasm_bindgen(js_name = "remaining")]
pub fn remaining_wasm(&self) -> usize {
bytes::Buf::remaining(self.inner.as_inner())
}
#[wasm_bindgen(js_name = "advance")]
pub fn advance_wasm(&mut self, cnt: usize) -> Result<(), JsError> {
self
.inner
.validate_char_boundary(cnt)
.map_err(|e| JsError::new(&e.to_string()))?;
self
.inner
.inner
.try_advance(cnt)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "splitTo")]
pub fn split_to_wasm(&mut self, at: usize) -> Result<WasmCompactUtf8Bytes, JsError> {
self
.inner
.try_split_to(at)
.map(Into::into)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "splitOff")]
pub fn split_off_wasm(&mut self, at: usize) -> Result<WasmCompactUtf8Bytes, JsError> {
self
.inner
.try_split_off(at)
.map(Into::into)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "slice")]
pub fn slice_wasm(&self, start: usize, end: usize) -> Result<WasmCompactUtf8Bytes, JsError> {
self
.inner
.try_slice(start..end)
.map(Into::into)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "iter")]
pub fn iter_wasm(&self) -> CharIterator {
CharIterator::new(self.inner.as_str())
}
}