lindera_dictionary/
macros.rs

1//! Common macros for dictionary data loading and decompression
2
3/// Macro for decompressing dictionary data
4/// This macro handles both compressed and uncompressed data formats
5#[macro_export]
6macro_rules! decompress_data {
7    ($name: ident, $bytes: expr, $filename: literal) => {
8        #[cfg(feature = "compress")]
9        static $name: once_cell::sync::Lazy<Vec<u8>> = once_cell::sync::Lazy::new(|| {
10            use $crate::decompress::{CompressedData, decompress};
11
12            let mut aligned = rkyv::util::AlignedVec::<16>::new();
13            aligned.extend_from_slice(&$bytes[..]);
14            match rkyv::from_bytes::<CompressedData, rkyv::rancor::Error>(&aligned) {
15                Ok(compressed_data) => {
16                    // Successfully decoded as CompressedData, now decompress it
17                    match decompress(compressed_data) {
18                        Ok(decompressed) => decompressed,
19                        Err(_) => {
20                            // Decompression failed, fall back to raw data
21                            $bytes.to_vec()
22                        }
23                    }
24                }
25                Err(_) => {
26                    // Not compressed data format, use as raw binary
27                    $bytes.to_vec()
28                }
29            }
30        });
31        #[cfg(not(feature = "compress"))]
32        const $name: &'static [u8] = $bytes;
33    };
34}