lindera_dependency/
embedded.rs1use crate::error::{Error, Result};
34use crate::model::Model;
35
36pub const SCHEME: &str = "embedded://";
39
40const KNOWN: [(&str, &str); 4] = [
43 ("ipadic", "embed-model-ipadic"),
44 ("unidic", "embed-model-unidic"),
45 ("unidic-cwj", "embed-model-unidic-cwj"),
46 ("unidic-csj", "embed-model-unidic-csj"),
47];
48
49#[cfg(feature = "embed-model-ipadic")]
51static IPADIC: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dependency-ipadic.model"));
52
53#[cfg(feature = "embed-model-unidic")]
55static UNIDIC: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dependency-unidic.model"));
56
57#[cfg(feature = "embed-model-unidic-cwj")]
59static UNIDIC_CWJ: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dependency-unidic-cwj.model"));
60
61#[cfg(feature = "embed-model-unidic-csj")]
63static UNIDIC_CSJ: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dependency-unidic-csj.model"));
64
65pub fn dictionary_of(model: &str) -> Option<&str> {
76 model.strip_prefix(SCHEME)
77}
78
79pub fn bytes(dictionary: &str) -> Result<&'static [u8]> {
92 let Some((_, feature)) = KNOWN.iter().find(|(name, _)| *name == dictionary) else {
93 let known: Vec<&str> = KNOWN.iter().map(|(name, _)| *name).collect();
94 return Err(Error::Embedded(format!(
95 "no model can be embedded for the dictionary {dictionary}: there are models for {}",
96 known.join(", ")
97 )));
98 };
99 let embedded: Option<&'static [u8]> = match dictionary {
100 #[cfg(feature = "embed-model-ipadic")]
101 "ipadic" => Some(IPADIC),
102 #[cfg(feature = "embed-model-unidic")]
103 "unidic" => Some(UNIDIC),
104 #[cfg(feature = "embed-model-unidic-cwj")]
105 "unidic-cwj" => Some(UNIDIC_CWJ),
106 #[cfg(feature = "embed-model-unidic-csj")]
107 "unidic-csj" => Some(UNIDIC_CSJ),
108 _ => None,
109 };
110 answer(dictionary, feature, embedded)
111}
112
113fn answer(
126 dictionary: &str,
127 feature: &str,
128 embedded: Option<&'static [u8]>,
129) -> Result<&'static [u8]> {
130 match embedded {
131 None => Err(Error::Embedded(format!(
132 "the model for {dictionary} is not embedded in this build: it needs a build with the \
133 feature `{feature}` of lindera-dependency, which lindera, its command and its \
134 bindings forward as `embed-dependency-{dictionary}`"
135 ))),
136 Some([]) => Err(Error::Embedded(format!(
137 "the model for {dictionary} is empty in this build: a documentation build embeds no \
138 model"
139 ))),
140 Some(bytes) => Ok(bytes),
141 }
142}
143
144pub fn model(dictionary: &str) -> Result<Model<&'static [u8]>> {
156 Model::new(bytes(dictionary)?)
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162
163 #[test]
164 fn a_model_is_named_as_an_embedded_dictionary_is() {
165 assert_eq!(dictionary_of("embedded://ipadic"), Some("ipadic"));
166 assert_eq!(dictionary_of("embedded://"), Some(""));
167 assert_eq!(dictionary_of("ipadic"), None);
168 assert_eq!(dictionary_of("/models/embedded://ipadic"), None);
169 assert_eq!(dictionary_of("file:///models/ipadic.model"), None);
170 }
171
172 #[test]
173 fn a_dictionary_without_a_model_names_the_ones_there_are() {
174 let message = bytes("ko-dic").unwrap_err().to_string();
175 assert!(message.contains("ko-dic"), "{message}");
176 assert!(
177 message.contains("models for ipadic, unidic, unidic-cwj, unidic-csj"),
178 "{message}"
179 );
180 assert!(matches!(model("").unwrap_err(), Error::Embedded(_)));
181 }
182
183 #[cfg(not(feature = "embed-model-ipadic"))]
184 #[test]
185 fn a_build_without_the_feature_names_it() {
186 let message = bytes("ipadic").unwrap_err().to_string();
187 assert!(message.contains("not embedded in this build"), "{message}");
188 assert!(message.contains("`embed-model-ipadic`"), "{message}");
189 assert!(message.contains("`embed-dependency-ipadic`"), "{message}");
190 assert!(matches!(model("ipadic").unwrap_err(), Error::Embedded(_)));
191 }
192
193 #[cfg(feature = "embed-model-ipadic")]
194 #[test]
195 fn the_embedded_model_is_for_its_dictionary() {
196 let model = model("ipadic").expect("embedded model");
197 assert_eq!(model.dictionary(), "ipadic");
198 assert!(model.chunker().is_some());
199 assert!(model.parser().is_some());
200 }
201
202 fn has_feature(dictionary: &str) -> bool {
204 match dictionary {
205 "ipadic" => cfg!(feature = "embed-model-ipadic"),
206 "unidic" => cfg!(feature = "embed-model-unidic"),
207 "unidic-cwj" => cfg!(feature = "embed-model-unidic-cwj"),
208 "unidic-csj" => cfg!(feature = "embed-model-unidic-csj"),
209 other => panic!("no feature for {other}"),
210 }
211 }
212
213 #[test]
214 fn every_model_is_embedded_by_its_feature_and_by_no_other() {
215 for (dictionary, feature) in KNOWN {
216 assert_eq!(feature, format!("embed-model-{dictionary}"));
217 match model(dictionary) {
218 Ok(model) => {
219 assert!(has_feature(dictionary), "{dictionary}");
220 assert_eq!(model.dictionary(), dictionary);
222 assert!(model.chunker().is_some() && model.parser().is_some());
223 }
224 Err(error) => {
225 assert!(!has_feature(dictionary), "{dictionary}: {error}");
226 let message = error.to_string();
227 assert!(message.contains(&format!("`{feature}`")), "{message}");
228 assert!(
229 message.contains(&format!("`embed-dependency-{dictionary}`")),
230 "{message}"
231 );
232 }
233 }
234 }
235 }
236
237 #[test]
238 fn the_build_script_has_a_model_for_every_dictionary() {
239 let script = include_str!("../build.rs");
240 for (dictionary, feature) in KNOWN {
241 let variable = dictionary.to_uppercase().replace('-', "_");
242 for text in [
243 format!("cfg!(feature = \"{feature}\")"),
244 format!("\"lindera-dependency-{dictionary}\""),
245 format!("\"prebuilt-{dictionary}.json\""),
246 format!("\"LINDERA_BUILD_DEPENDENCY_MODEL_{variable}_FILE\""),
247 format!("\"dependency-{dictionary}.model\""),
248 ] {
249 assert!(script.contains(&text), "build.rs has no {text}");
250 }
251 }
252 let count = script.matches("cfg!(feature = \"embed-model-").count();
253 assert_eq!(count, KNOWN.len());
254 }
255
256 #[test]
257 fn the_empty_file_of_a_documentation_build_is_an_error() {
258 let message = answer("ipadic", "embed-model-ipadic", Some(&[]))
259 .unwrap_err()
260 .to_string();
261 assert!(message.contains("is empty in this build"), "{message}");
262 assert_eq!(
263 answer("ipadic", "embed-model-ipadic", Some(b"bytes")).unwrap(),
264 b"bytes"
265 );
266 let message = answer("ipadic", "embed-model-ipadic", None)
267 .unwrap_err()
268 .to_string();
269 assert!(message.contains("`embed-model-ipadic`"), "{message}");
270 }
271
272 #[test]
273 fn the_build_script_pins_the_format_version_this_crate_reads() {
274 let script = include_str!("../build.rs");
275 let line = format!("format_version: {},", crate::model::VERSION);
276 assert_eq!(script.matches("format_version: ").count(), 1);
277 assert!(script.contains(&line), "build.rs has no `{line}`");
278 }
279}