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            // First check if this is compressed data by attempting to decode as CompressedData
13            match bincode::serde::decode_from_slice::<CompressedData, _>(
14                &$bytes[..],
15                bincode::config::legacy(),
16            ) {
17                Ok((compressed_data, _)) => {
18                    // Successfully decoded as CompressedData, now decompress it
19                    match decompress(compressed_data) {
20                        Ok(decompressed) => decompressed,
21                        Err(_) => {
22                            // Decompression failed, fall back to raw data
23                            $bytes.to_vec()
24                        }
25                    }
26                }
27                Err(_) => {
28                    // Not compressed data format, use as raw binary
29                    $bytes.to_vec()
30                }
31            }
32        });
33        #[cfg(not(feature = "compress"))]
34        const $name: &'static [u8] = $bytes;
35    };
36}