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