lindera_dictionary/loader/
unknown_dictionary.rs1use std::path::Path;
2
3use crate::LinderaResult;
4#[cfg(feature = "compress")]
5use crate::decompress::{CompressedData, decompress};
6use crate::dictionary::unknown_dictionary::UnknownDictionary;
7#[cfg(feature = "compress")]
8use crate::error::LinderaErrorKind;
9use crate::util::read_file;
10
11pub struct UnknownDictionaryLoader {}
12
13impl UnknownDictionaryLoader {
14 #[allow(unused_mut)]
15 pub fn load(input_dir: &Path) -> LinderaResult<UnknownDictionary> {
16 let raw_data = read_file(input_dir.join("unk.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 unk.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 unknown dictionary data")
36 })?;
37
38 let mut aligned_decompressed = rkyv::util::AlignedVec::<16>::new();
39 aligned_decompressed.extend_from_slice(&decompressed_data);
40
41 return UnknownDictionary::load(&aligned_decompressed);
42 }
43
44 #[cfg(not(feature = "compress"))]
45 {
46 UnknownDictionary::load(&aligned_data)
47 }
48 }
49}