lindera_dictionary/loader/
character_definition.rs

1use std::path::Path;
2
3use crate::LinderaResult;
4#[cfg(feature = "compress")]
5use crate::decompress::{CompressedData, decompress};
6use crate::dictionary::character_definition::CharacterDefinition;
7#[cfg(feature = "compress")]
8use crate::error::LinderaErrorKind;
9use crate::util::read_file;
10
11pub struct CharacterDefinitionLoader {}
12
13impl CharacterDefinitionLoader {
14    #[allow(unused_mut)]
15    pub fn load(input_dir: &Path) -> LinderaResult<CharacterDefinition> {
16        let raw_data = read_file(input_dir.join("char_def.bin").as_path())?;
17
18        let mut aligned_data = rkyv::util::AlignedVec::<16>::new();
19        aligned_data.extend_from_slice(&raw_data);
20
21        #[cfg(feature = "compress")]
22        {
23            let compressed_data: CompressedData =
24                rkyv::from_bytes::<CompressedData, rkyv::rancor::Error>(&aligned_data).map_err(
25                    |err| {
26                        LinderaErrorKind::Deserialize
27                            .with_error(anyhow::anyhow!(err.to_string()))
28                            .add_context("Failed to deserialize char_def.bin data")
29                    },
30                )?;
31
32            let decompressed_data = decompress(compressed_data).map_err(|err| {
33                LinderaErrorKind::Compression
34                    .with_error(err)
35                    .add_context("Failed to decompress character definition data")
36            })?;
37
38            let mut aligned_decompressed = rkyv::util::AlignedVec::<16>::new();
39            aligned_decompressed.extend_from_slice(&decompressed_data);
40
41            return CharacterDefinition::load(&aligned_decompressed);
42        }
43
44        #[cfg(not(feature = "compress"))]
45        {
46            CharacterDefinition::load(&aligned_data)
47        }
48    }
49}