Skip to main content

lindera_core/dictionary_builder/
chardef.rs

1use std::borrow::Cow;
2use std::fs::File;
3use std::io::{self, Write};
4use std::path::Path;
5
6use derive_builder::Builder;
7use log::debug;
8
9use crate::decompress::Algorithm;
10use crate::dictionary::character_definition::{CharacterDefinitions, CharacterDefinitionsBuilder};
11use crate::dictionary_builder::utils::{compress_write, read_file_with_encoding};
12use crate::error::LinderaErrorKind;
13use crate::LinderaResult;
14
15#[derive(Builder, Debug)]
16#[builder(name = "CharDefBuilderOptions")]
17#[builder(build_fn(name = "builder"))]
18pub struct CharDefBuilder {
19    #[builder(default = "\"UTF-8\".into()", setter(into))]
20    encoding: Cow<'static, str>,
21    #[builder(default = "Algorithm::Deflate")]
22    compress_algorithm: Algorithm,
23}
24
25impl CharDefBuilder {
26    pub fn build(
27        &self,
28        input_dir: &Path,
29        output_dir: &Path,
30    ) -> LinderaResult<CharacterDefinitions> {
31        let char_def_path = input_dir.join("char.def");
32        debug!("reading {:?}", char_def_path);
33        let char_def = read_file_with_encoding(&char_def_path, &self.encoding)?;
34
35        let mut char_definitions_builder = CharacterDefinitionsBuilder::default();
36        char_definitions_builder.parse(&char_def)?;
37        let char_definitions = char_definitions_builder.build();
38
39        let mut chardef_buffer = Vec::new();
40        bincode::serialize_into(&mut chardef_buffer, &char_definitions)
41            .map_err(|err| LinderaErrorKind::Serialize.with_error(anyhow::anyhow!(err)))?;
42
43        let wtr_chardef_path = output_dir.join(Path::new("char_def.bin"));
44        let mut wtr_chardef = io::BufWriter::new(
45            File::create(wtr_chardef_path)
46                .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?,
47        );
48
49        compress_write(&chardef_buffer, self.compress_algorithm, &mut wtr_chardef)?;
50
51        wtr_chardef
52            .flush()
53            .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
54
55        Ok(char_definitions)
56    }
57}