lindera_dictionary/loader/
character_definition.rs

1use std::path::Path;
2
3use crate::LinderaResult;
4#[cfg(feature = "compress")]
5use crate::decompress::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 mut data = read_file(input_dir.join("char_def.bin").as_path())?;
17
18        #[cfg(feature = "compress")]
19        {
20            let (compressed_data, _) =
21                bincode::serde::decode_from_slice(data.as_slice(), bincode::config::legacy())
22                    .map_err(|err| {
23                        LinderaErrorKind::Deserialize
24                            .with_error(anyhow::anyhow!(err))
25                            .add_context("Failed to deserialize char_def.bin data")
26                    })?;
27            data = decompress(compressed_data).map_err(|err| {
28                LinderaErrorKind::Compression
29                    .with_error(err)
30                    .add_context("Failed to decompress character definition data")
31            })?;
32        }
33
34        CharacterDefinition::load(data.as_slice())
35    }
36}