rqjs_ext/modules/encoding/
text_encoder.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3use rquickjs::{Ctx, Result, TypedArray, Value};
4
5#[derive(rquickjs::class::Trace)]
6#[rquickjs::class]
7pub struct TextEncoder {}
8
9impl Default for TextEncoder {
10    fn default() -> Self {
11        Self::new()
12    }
13}
14
15#[rquickjs::methods]
16impl TextEncoder {
17    #[qjs(constructor)]
18    pub fn new() -> Self {
19        Self {}
20    }
21
22    #[qjs(get)]
23    fn encoding(&self) -> String {
24        "utf-8".to_string()
25    }
26
27    pub fn encode<'js>(&self, ctx: Ctx<'js>, string: String) -> Result<Value<'js>> {
28        TypedArray::new(ctx, string.as_bytes()).map(|m| m.into_value())
29    }
30}