lindera_dictionary/
util.rs1use std::fs::File;
2use std::io::{Read, Write};
3use std::ops::Deref;
4use std::path::Path;
5
6#[cfg(feature = "mmap")]
7use memmap2::Mmap;
8
9use anyhow::anyhow;
10use encoding_rs::Encoding;
11use serde::{Deserialize, Serialize};
12
13use crate::LinderaResult;
14#[cfg(feature = "compress")]
15use crate::compress::compress;
16use crate::decompress::Algorithm;
17use crate::error::LinderaErrorKind;
18
19#[cfg(feature = "compress")]
20pub fn compress_write<W: Write>(
21 buffer: &[u8],
22 algorithm: Algorithm,
23 writer: &mut W,
24) -> LinderaResult<()> {
25 let compressed = compress(buffer, algorithm)
26 .map_err(|err| LinderaErrorKind::Compress.with_error(anyhow::anyhow!(err)))?;
27 bincode::serde::encode_into_std_write(&compressed, writer, bincode::config::legacy())
28 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
29
30 Ok(())
31}
32
33#[cfg(not(feature = "compress"))]
34pub fn compress_write<W: Write>(
35 buffer: &[u8],
36 _algorithm: Algorithm,
37 writer: &mut W,
38) -> LinderaResult<()> {
39 writer
40 .write_all(buffer)
41 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
42
43 Ok(())
44}
45
46pub fn read_file(filename: &Path) -> LinderaResult<Vec<u8>> {
47 let mut input_read = File::open(filename)
48 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
49 let mut buffer = Vec::new();
50 input_read
51 .read_to_end(&mut buffer)
52 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
53 Ok(buffer)
54}
55
56#[cfg(feature = "mmap")]
57pub fn mmap_file(filename: &Path) -> LinderaResult<Mmap> {
58 let file = File::open(filename)
59 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
60 let mmap = unsafe { Mmap::map(&file) }
61 .map_err(|err| LinderaErrorKind::Io.with_error(anyhow::anyhow!(err)))?;
62 Ok(mmap)
63}
64
65pub fn read_file_with_encoding(filepath: &Path, encoding_name: &str) -> LinderaResult<String> {
66 let encoding = Encoding::for_label_no_replacement(encoding_name.as_bytes());
67 let encoding = encoding.ok_or_else(|| {
68 LinderaErrorKind::Decode.with_error(anyhow!("Invalid encoding: {}", encoding_name))
69 })?;
70
71 let buffer = read_file(filepath)?;
72 Ok(encoding.decode(&buffer).0.into_owned())
73}
74
75pub enum Data {
76 Static(&'static [u8]),
77 Vec(Vec<u8>),
78 #[cfg(feature = "mmap")]
79 Map(Mmap),
80}
81
82impl Deref for Data {
83 type Target = [u8];
84 fn deref(&self) -> &Self::Target {
85 match self {
86 Data::Static(s) => s,
87 Data::Vec(v) => v,
88 #[cfg(feature = "mmap")]
89 Data::Map(m) => m,
90 }
91 }
92}
93
94impl From<&'static [u8]> for Data {
95 fn from(s: &'static [u8]) -> Self {
96 Self::Static(s)
97 }
98}
99
100impl<T: Deref<Target = [u8]>> From<&'static T> for Data {
101 fn from(t: &'static T) -> Self {
102 Self::Static(t)
103 }
104}
105
106impl From<Vec<u8>> for Data {
107 fn from(v: Vec<u8>) -> Self {
108 Self::Vec(v)
109 }
110}
111
112#[cfg(feature = "mmap")]
113impl From<Mmap> for Data {
114 fn from(m: Mmap) -> Self {
115 Self::Map(m)
116 }
117}
118
119impl Clone for Data {
120 fn clone(&self) -> Self {
121 match self {
122 Data::Static(s) => Data::Static(s),
123 Data::Vec(v) => Data::Vec(v.clone()),
124 #[cfg(feature = "mmap")]
125 Data::Map(m) => Data::Vec(m.to_vec()),
126 }
127 }
128}
129
130impl Serialize for Data {
131 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
132 where
133 S: serde::Serializer,
134 {
135 match self {
136 Self::Static(s) => serializer.serialize_bytes(s),
137 Self::Vec(v) => serializer.serialize_bytes(v),
138 #[cfg(feature = "mmap")]
139 Self::Map(m) => serializer.serialize_bytes(m),
140 }
141 }
142}
143
144impl<'de> Deserialize<'de> for Data {
145 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
146 where
147 D: serde::Deserializer<'de>,
148 {
149 Vec::<u8>::deserialize(deserializer).map(Self::Vec)
150 }
151}