lindera_dictionary_builder/
utils.rs

1use std::io::Write;
2use std::path::Path;
3
4use anyhow::anyhow;
5use encoding_rs::Encoding;
6#[cfg(feature = "compress")]
7use lindera_compress::compress;
8use lindera_core::error::LinderaErrorKind;
9use lindera_core::file_util::read_file;
10use lindera_core::LinderaResult;
11use lindera_decompress::Algorithm;
12
13#[cfg(feature = "compress")]
14pub fn compress_write<W: Write>(
15    buffer: &[u8],
16    algorithm: Algorithm,
17    writer: &mut W,
18) -> LinderaResult<()> {
19    let compressed = compress(buffer, algorithm)
20        .map_err(|err| LinderaErrorKind::Compress.with_error(anyhow::anyhow!(err)))?;
21    bincode::serialize_into(writer, &compressed)
22        .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
23
24    Ok(())
25}
26
27#[cfg(not(feature = "compress"))]
28pub fn compress_write<W: Write>(
29    buffer: &[u8],
30    _algorithm: Algorithm,
31    writer: &mut W,
32) -> LinderaResult<()> {
33    writer
34        .write_all(buffer)
35        .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
36
37    Ok(())
38}
39
40pub fn read_file_with_encoding(filepath: &Path, encoding_name: &str) -> LinderaResult<String> {
41    let encoding = Encoding::for_label_no_replacement(encoding_name.as_bytes());
42    let encoding = encoding.ok_or_else(|| {
43        LinderaErrorKind::Decode.with_error(anyhow!("Invalid encoding: {}", encoding_name))
44    })?;
45
46    let buffer = read_file(&filepath)?;
47    Ok(encoding.decode(&buffer).0.into_owned())
48}