sejong/lib.rs
1
2//! Sejong [`Buffer`] takes English letters([`Byte`]) that appears on standard
3//! keyboard and convert them to corresponding Hangul Jamos as
4//! in standard Korean 2-set keyboard. It can output complete Hangul
5//! Syllables as a UTF-32 string. It also allows deletion by Hangul
6//! Jamo.
7//!
8//! # Example
9//! ```
10//! use sejong::{Buffer, Byte};
11//! let mut buf = Buffer::default();
12//! buf.put(Byte::NG as u8);
13//! buf.put(Byte::A as u8);
14//! buf.put(Byte::N as u8);
15//! buf.put(Byte::N as u8);
16//! buf.put(Byte::YEO as u8);
17//! buf.put(Byte::NG as u8);
18//!
19//! assert_eq!(buf.to_string(), "안녕");
20//!
21//! buf.put(Byte::N as u8);
22//! assert_eq!(buf.to_string(), "안녕ㄴ");
23//!
24//! buf.pop();
25//! assert_eq!(buf.out(), "안녕");
26//! assert_eq!(buf.out(), "");
27//! ```
28
29#[cfg(feature = "wasm")]
30use wasm_bindgen::prelude::wasm_bindgen;
31
32mod buffer;
33mod byte;
34mod syllable;
35pub use buffer::Buffer;
36pub use byte::Byte;
37
38#[cfg(feature = "wasm")]
39#[macro_use]
40extern crate lazy_static;
41
42#[cfg(feature = "wasm")]
43lazy_static! {
44 static ref BUFFER: std::sync::Mutex<Buffer> = std::sync::Mutex::new(Buffer::default());
45}
46#[cfg(feature = "wasm")]
47#[global_allocator]
48static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
49
50/// This is a simple wrapper for [`Buffer::put`].
51/// When used as a WASM module, this lib instantiate a global
52/// [`Buffer`] and this method is using the global instance.
53#[cfg(any(feature = "wasm", doc))]
54#[cfg_attr(feature = "wasm", wasm_bindgen)]
55pub fn put(c: char) -> Option<String> {
56 let mut b = BUFFER.lock().unwrap();
57 match b.put(c) {
58 None => Some(b.to_string()),
59 _ => None,
60 }
61}
62
63/// This is a simple wrapper for [`Buffer::pop`].
64/// When used as a WASM module, this lib instantiate a global
65/// [`Buffer`] and this method is using the global instance.
66#[cfg(any(feature = "wasm", doc))]
67#[cfg_attr(feature = "wasm", wasm_bindgen)]
68pub fn pop() -> Option<String> {
69 let mut b = BUFFER.lock().unwrap();
70 b.pop().map(|_| b.to_string())
71}
72
73/// This is a simple wrapper for [`Buffer::out`].
74/// When used as a WASM module, this lib instantiate a global
75/// [`Buffer`] and this method is using the global instance.
76#[cfg(any(feature = "wasm", doc))]
77#[cfg_attr(feature = "wasm", wasm_bindgen)]
78pub fn out() -> String {
79 let mut b = BUFFER.lock().unwrap();
80 b.out()
81}