use crate::error::{Error, Result};
use crate::model::Model;
pub const SCHEME: &str = "embedded://";
const KNOWN: [(&str, &str); 4] = [
("ipadic", "embed-model-ipadic"),
("unidic", "embed-model-unidic"),
("unidic-cwj", "embed-model-unidic-cwj"),
("unidic-csj", "embed-model-unidic-csj"),
];
#[cfg(feature = "embed-model-ipadic")]
static IPADIC: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dependency-ipadic.model"));
#[cfg(feature = "embed-model-unidic")]
static UNIDIC: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dependency-unidic.model"));
#[cfg(feature = "embed-model-unidic-cwj")]
static UNIDIC_CWJ: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dependency-unidic-cwj.model"));
#[cfg(feature = "embed-model-unidic-csj")]
static UNIDIC_CSJ: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dependency-unidic-csj.model"));
pub fn dictionary_of(model: &str) -> Option<&str> {
model.strip_prefix(SCHEME)
}
pub fn bytes(dictionary: &str) -> Result<&'static [u8]> {
let Some((_, feature)) = KNOWN.iter().find(|(name, _)| *name == dictionary) else {
let known: Vec<&str> = KNOWN.iter().map(|(name, _)| *name).collect();
return Err(Error::Embedded(format!(
"no model can be embedded for the dictionary {dictionary}: there are models for {}",
known.join(", ")
)));
};
let embedded: Option<&'static [u8]> = match dictionary {
#[cfg(feature = "embed-model-ipadic")]
"ipadic" => Some(IPADIC),
#[cfg(feature = "embed-model-unidic")]
"unidic" => Some(UNIDIC),
#[cfg(feature = "embed-model-unidic-cwj")]
"unidic-cwj" => Some(UNIDIC_CWJ),
#[cfg(feature = "embed-model-unidic-csj")]
"unidic-csj" => Some(UNIDIC_CSJ),
_ => None,
};
answer(dictionary, feature, embedded)
}
fn answer(
dictionary: &str,
feature: &str,
embedded: Option<&'static [u8]>,
) -> Result<&'static [u8]> {
match embedded {
None => Err(Error::Embedded(format!(
"the model for {dictionary} is not embedded in this build: it needs a build with the \
feature `{feature}` of lindera-dependency, which lindera, its command and its \
bindings forward as `embed-dependency-{dictionary}`"
))),
Some([]) => Err(Error::Embedded(format!(
"the model for {dictionary} is empty in this build: a documentation build embeds no \
model"
))),
Some(bytes) => Ok(bytes),
}
}
pub fn model(dictionary: &str) -> Result<Model<&'static [u8]>> {
Model::new(bytes(dictionary)?)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_model_is_named_as_an_embedded_dictionary_is() {
assert_eq!(dictionary_of("embedded://ipadic"), Some("ipadic"));
assert_eq!(dictionary_of("embedded://"), Some(""));
assert_eq!(dictionary_of("ipadic"), None);
assert_eq!(dictionary_of("/models/embedded://ipadic"), None);
assert_eq!(dictionary_of("file:///models/ipadic.model"), None);
}
#[test]
fn a_dictionary_without_a_model_names_the_ones_there_are() {
let message = bytes("ko-dic").unwrap_err().to_string();
assert!(message.contains("ko-dic"), "{message}");
assert!(
message.contains("models for ipadic, unidic, unidic-cwj, unidic-csj"),
"{message}"
);
assert!(matches!(model("").unwrap_err(), Error::Embedded(_)));
}
#[cfg(not(feature = "embed-model-ipadic"))]
#[test]
fn a_build_without_the_feature_names_it() {
let message = bytes("ipadic").unwrap_err().to_string();
assert!(message.contains("not embedded in this build"), "{message}");
assert!(message.contains("`embed-model-ipadic`"), "{message}");
assert!(message.contains("`embed-dependency-ipadic`"), "{message}");
assert!(matches!(model("ipadic").unwrap_err(), Error::Embedded(_)));
}
#[cfg(feature = "embed-model-ipadic")]
#[test]
fn the_embedded_model_is_for_its_dictionary() {
let model = model("ipadic").expect("embedded model");
assert_eq!(model.dictionary(), "ipadic");
assert!(model.chunker().is_some());
assert!(model.parser().is_some());
}
fn has_feature(dictionary: &str) -> bool {
match dictionary {
"ipadic" => cfg!(feature = "embed-model-ipadic"),
"unidic" => cfg!(feature = "embed-model-unidic"),
"unidic-cwj" => cfg!(feature = "embed-model-unidic-cwj"),
"unidic-csj" => cfg!(feature = "embed-model-unidic-csj"),
other => panic!("no feature for {other}"),
}
}
#[test]
fn every_model_is_embedded_by_its_feature_and_by_no_other() {
for (dictionary, feature) in KNOWN {
assert_eq!(feature, format!("embed-model-{dictionary}"));
match model(dictionary) {
Ok(model) => {
assert!(has_feature(dictionary), "{dictionary}");
assert_eq!(model.dictionary(), dictionary);
assert!(model.chunker().is_some() && model.parser().is_some());
}
Err(error) => {
assert!(!has_feature(dictionary), "{dictionary}: {error}");
let message = error.to_string();
assert!(message.contains(&format!("`{feature}`")), "{message}");
assert!(
message.contains(&format!("`embed-dependency-{dictionary}`")),
"{message}"
);
}
}
}
}
#[test]
fn the_build_script_has_a_model_for_every_dictionary() {
let script = include_str!("../build.rs");
for (dictionary, feature) in KNOWN {
let variable = dictionary.to_uppercase().replace('-', "_");
for text in [
format!("cfg!(feature = \"{feature}\")"),
format!("\"lindera-dependency-{dictionary}\""),
format!("\"prebuilt-{dictionary}.json\""),
format!("\"LINDERA_BUILD_DEPENDENCY_MODEL_{variable}_FILE\""),
format!("\"dependency-{dictionary}.model\""),
] {
assert!(script.contains(&text), "build.rs has no {text}");
}
}
let count = script.matches("cfg!(feature = \"embed-model-").count();
assert_eq!(count, KNOWN.len());
}
#[test]
fn the_empty_file_of_a_documentation_build_is_an_error() {
let message = answer("ipadic", "embed-model-ipadic", Some(&[]))
.unwrap_err()
.to_string();
assert!(message.contains("is empty in this build"), "{message}");
assert_eq!(
answer("ipadic", "embed-model-ipadic", Some(b"bytes")).unwrap(),
b"bytes"
);
let message = answer("ipadic", "embed-model-ipadic", None)
.unwrap_err()
.to_string();
assert!(message.contains("`embed-model-ipadic`"), "{message}");
}
#[test]
fn the_build_script_pins_the_format_version_this_crate_reads() {
let script = include_str!("../build.rs");
let line = format!("format_version: {},", crate::model::VERSION);
assert_eq!(script.matches("format_version: ").count(), 1);
assert!(script.contains(&line), "build.rs has no `{line}`");
}
}