Skip to main content

lindera_core/dictionary_builder/
utils.rs

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