use crate::utils::*;
use crate::{any::Any, array::Uint8Array};
use alloc::string::String;
#[derive(Clone, Debug, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct TextEncoder {
inner: emlite::Val,
}
bind!(TextEncoder);
impl_dyn_cast!(TextEncoder);
impl Default for TextEncoder {
fn default() -> Self {
Self::new()
}
}
impl TextEncoder {
pub fn new() -> Self {
emlite::Val::global("TextEncoder").new(&[]).as_::<Self>()
}
pub fn encode(&self, s: &str) -> Uint8Array {
self.inner.call("encode", &[s.into()]).as_::<Uint8Array>()
}
pub fn encode_into(&self, src: &str, dst: &mut Uint8Array) -> (usize, usize) {
let res = self
.inner
.call("encodeInto", &[src.into(), dst.clone().into()]);
let read = res.get("read").as_::<u32>() as usize;
let written = res.get("written").as_::<u32>() as usize;
(read, written)
}
}
#[derive(Clone, Debug, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct TextDecoder {
inner: emlite::Val,
}
bind!(TextDecoder);
impl_dyn_cast!(TextDecoder);
impl TextDecoder {
pub fn new(label: Option<String>, opts: Option<&Any>) -> Self {
let ctor = emlite::Val::global("TextDecoder");
match (label, opts) {
(Some(l), Some(o)) => ctor.new(&[l.into(), o.clone()]).as_::<Self>(),
(Some(l), None) => ctor.new(&[l.into()]).as_::<Self>(),
(None, Some(o)) => ctor.new(&[o.clone()]).as_::<Self>(),
(None, None) => ctor.new(&[]).as_::<Self>(),
}
}
pub fn decode(&self, bytes: &Uint8Array) -> Option<String> {
self.inner.call("decode", &[bytes.clone().into()]).as_()
}
}