Skip to main content

lindera_wasm/
metadata.rs

1use wasm_bindgen::prelude::*;
2
3use lindera::dictionary::{CompressionAlgorithm, Metadata};
4
5use crate::schema::JsSchema;
6
7/// Compression algorithm for dictionary data.
8#[wasm_bindgen(js_name = "CompressionAlgorithm")]
9#[derive(Clone, Copy)]
10pub enum JsCompressionAlgorithm {
11    Deflate,
12    Zlib,
13    Gzip,
14    Raw,
15}
16
17impl From<CompressionAlgorithm> for JsCompressionAlgorithm {
18    fn from(alg: CompressionAlgorithm) -> Self {
19        match alg {
20            CompressionAlgorithm::Deflate => JsCompressionAlgorithm::Deflate,
21            CompressionAlgorithm::Zlib => JsCompressionAlgorithm::Zlib,
22            CompressionAlgorithm::Gzip => JsCompressionAlgorithm::Gzip,
23            CompressionAlgorithm::Raw => JsCompressionAlgorithm::Raw,
24        }
25    }
26}
27
28impl From<JsCompressionAlgorithm> for CompressionAlgorithm {
29    fn from(alg: JsCompressionAlgorithm) -> Self {
30        match alg {
31            JsCompressionAlgorithm::Deflate => CompressionAlgorithm::Deflate,
32            JsCompressionAlgorithm::Zlib => CompressionAlgorithm::Zlib,
33            JsCompressionAlgorithm::Gzip => CompressionAlgorithm::Gzip,
34            JsCompressionAlgorithm::Raw => CompressionAlgorithm::Raw,
35        }
36    }
37}
38
39/// Dictionary metadata configuration.
40#[wasm_bindgen(js_name = "Metadata")]
41#[derive(Clone)]
42pub struct JsMetadata {
43    pub(crate) inner: Metadata,
44}
45
46#[wasm_bindgen]
47impl JsMetadata {
48    #[wasm_bindgen(constructor)]
49    pub fn new(
50        name: Option<String>,
51        encoding: Option<String>,
52        compress_algorithm: Option<JsCompressionAlgorithm>,
53    ) -> Self {
54        let mut inner = Metadata::default();
55        if let Some(n) = name {
56            inner.name = n;
57        }
58        if let Some(e) = encoding {
59            inner.encoding = e;
60        }
61        if let Some(a) = compress_algorithm {
62            inner.compress_algorithm = a.into();
63        }
64        Self { inner }
65    }
66
67    #[wasm_bindgen(js_name = "createDefault")]
68    pub fn create_default() -> Self {
69        Self {
70            inner: Metadata::default(),
71        }
72    }
73
74    #[wasm_bindgen(getter)]
75    pub fn name(&self) -> String {
76        self.inner.name.clone()
77    }
78
79    #[wasm_bindgen(setter)]
80    pub fn set_name(&mut self, name: String) {
81        self.inner.name = name;
82    }
83
84    #[wasm_bindgen(getter)]
85    pub fn encoding(&self) -> String {
86        self.inner.encoding.clone()
87    }
88
89    #[wasm_bindgen(setter)]
90    pub fn set_encoding(&mut self, encoding: String) {
91        self.inner.encoding = encoding;
92    }
93
94    #[wasm_bindgen(getter)]
95    pub fn compress_algorithm(&self) -> JsCompressionAlgorithm {
96        self.inner.compress_algorithm.into()
97    }
98
99    #[wasm_bindgen(setter)]
100    pub fn set_compress_algorithm(&mut self, algorithm: JsCompressionAlgorithm) {
101        self.inner.compress_algorithm = algorithm.into();
102    }
103
104    #[wasm_bindgen(getter)]
105    pub fn dictionary_schema(&self) -> JsSchema {
106        JsSchema {
107            inner: self.inner.dictionary_schema.clone(),
108        }
109    }
110
111    #[wasm_bindgen(setter)]
112    pub fn set_dictionary_schema(&mut self, schema: JsSchema) {
113        self.inner.dictionary_schema = schema.inner;
114    }
115
116    #[wasm_bindgen(getter)]
117    pub fn user_dictionary_schema(&self) -> JsSchema {
118        JsSchema {
119            inner: self.inner.user_dictionary_schema.clone(),
120        }
121    }
122
123    #[wasm_bindgen(setter)]
124    pub fn set_user_dictionary_schema(&mut self, schema: JsSchema) {
125        self.inner.user_dictionary_schema = schema.inner;
126    }
127}
128
129impl From<Metadata> for JsMetadata {
130    fn from(metadata: Metadata) -> Self {
131        JsMetadata { inner: metadata }
132    }
133}
134
135impl From<JsMetadata> for Metadata {
136    fn from(metadata: JsMetadata) -> Self {
137        metadata.inner
138    }
139}