combs_mesh/engine/exporter.rs
1//! [`EmojiExporter`] — the single entry point for moving an [`Emoji`]
2//! between the in-memory model and its two serializations: `.cmse` binary
3//! (storage/FFI) and the Unicode envelope string (text channels).
4
5use crate::binary;
6use crate::codepoint;
7use crate::crypto::KeyRing;
8use crate::engine::Emoji;
9use crate::error::Result;
10
11/// Stateless serializer/deserializer for [`Emoji`].
12pub struct EmojiExporter;
13
14impl EmojiExporter {
15 /// Serializes to the `.cmse` binary container (plaintext).
16 pub fn to_binary(emoji: &Emoji) -> Result<Vec<u8>> {
17 binary::write_emoji(emoji, None)
18 }
19
20 /// Serializes to `.cmse`, encrypting every block type named by the
21 /// emoji's `Enc` block with `keyring`.
22 pub fn to_binary_encrypted(emoji: &Emoji, keyring: &KeyRing) -> Result<Vec<u8>> {
23 binary::write_emoji(emoji, Some(keyring))
24 }
25
26 /// Parses a `.cmse` container. Fails if any block is encrypted.
27 pub fn from_binary(bytes: &[u8]) -> Result<Emoji> {
28 Ok(Emoji::from_blocks(binary::read_blocks(bytes, None)?))
29 }
30
31 /// Parses a `.cmse` container, decrypting encrypted blocks with
32 /// `keyring` (plaintext containers are accepted too).
33 pub fn from_binary_decrypted(bytes: &[u8], keyring: &KeyRing) -> Result<Emoji> {
34 Ok(Emoji::from_blocks(binary::read_blocks(
35 bytes,
36 Some(keyring),
37 )?))
38 }
39
40 /// Encodes to the Unicode envelope string (plane 15/16 PUA + tag
41 /// chars). Always plaintext — encrypt bytes first if a confidential
42 /// text-channel transport is needed.
43 pub fn to_unicode(emoji: &Emoji) -> Result<String> {
44 codepoint::encode_blocks(&emoji.blocks)
45 }
46
47 /// Decodes every block envelope found in `s` (other text is skipped).
48 pub fn from_unicode(s: &str) -> Result<Emoji> {
49 Ok(Emoji::from_blocks(codepoint::decode_blocks(s)?))
50 }
51}