1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
//! # ejdict-rs //! //! This library is an English-Japanese dictionary that can be used via implemented API by Rust language. //! //! ## Overview //! //! This library is available through a simple API. //! Since the dictionary data to be referenced is embedded in this crate, //! The Japanese-English dictionary can be used immediately by simply obtaining the crate from crates.io without depending on the database or file. //! //! The dictionary data of this library is "ejdict" which is a public domain dictionary. //! See the following URL for details. //! //! https://github.com/kujirahand/EJDict //! //! ## Examples //! //! This library is used through two functions. //! //! **case1**: Look up words from dictionary. //! //! ``` //! use ejdict_rs::SearchMode; //! //! # fn main() -> ejdict_rs::Result<()> { //! let word = ejdict_rs::look("apple", SearchMode::Exact)?; //! assert_eq!(word.mean(), "『リンゴ』;リンゴの木"); //! # Ok(()) //! # } //! ``` //! //! **case2**: Candidate list from dictionary. //! //! ``` //! use ejdict_rs::SearchMode; //! //! # fn main() -> ejdict_rs::Result<()> { //! let candidates = ejdict_rs::candidates("apple", SearchMode::Fuzzy)?; //! for word in candidates { //! // something ... //! } //! # Ok(()) //! # } //! ``` //! //! ## Install //! //! Write the following contents in Cargo.toml. //! //! ```toml //! [dependencies] //! ejdict_rs = { "0.0.4" } //! ``` //! //! If you use the development version or a specific version, write as follows. //! //! ```toml //! [dependencies] //! ejdict_rs = { git = "https://github.com/tomo3110/ejdict-rs" } //! ``` //! //! For details, check the following URL. //! //! https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories //! //! ## Dependencies //! //! - crates //! - failure //! - Apache 2.0, MIT //! - Error management //! - lazy_static //! - Apache 2.0, MIT //! - Copyright (c) 2010 The Rust Project Developers //! - A small macro for defining lazy evaluated static variables in Rust. //! - serde_json //! - Apache 2.0, MIT //! - Strongly typed JSON library. //! - dictionary data //! - ejdict-hand //! - MIT //! - Copyright (c) 2016 kujirahand //! - English-Japanese Dictionary data (Public Domain) //! //! Thanks for the great crates and dictionary data. //! //! ## License //! //! This software is under [MIT License](https://github.com/tomo3110/ejdict-rs/blob/master/LICENCE). //! //! ## Author //! //! [tomo3110](https://github.com/tomo3110) //! use lazy_static::lazy_static; use std::env; mod errors; pub use ejdict_rs_core::{Dictionary, SearchMode, Word}; pub use errors::{Error, ErrorKind, Result}; /// List of candidates that can be obtained as search results pub type Candidates<T> = ejdict_rs_core::Candidates<std::vec::IntoIter<T>>; lazy_static! { static ref EJDICT_DISCIONARY: Dictionary = load_dictionary().unwrap(); } #[cfg(windows)] fn get_ejdict_json<'a>() -> &'a str { include_str!(concat!(env!("OUT_DIR"), "\\ejdict.json")) } #[cfg(not(windows))] fn get_ejdict_json<'a>() -> &'a str { include_str!(concat!(env!("OUT_DIR"), "/ejdict.json")) } fn load_dictionary() -> Result<Dictionary> { let src = get_ejdict_json(); let dict = serde_json::from_str::<Dictionary>(src)?; Ok(dict) } /// Look up words from an English-Japanese Dictionary. /// /// # Example /// /// The following example shows how to Look up words. /// /// /// ``` /// use ejdict_rs::SearchMode; /// /// # fn main() -> ejdict_rs::Result<()> { /// let word = ejdict_rs::look("apple", SearchMode::Exact)?; /// assert_eq!(word.mean(), "『リンゴ』;リンゴの木"); /// # Ok(()) /// # } /// ``` /// pub fn look(word: &str, mode: SearchMode) -> Result<&Word> { let ref dict: Dictionary = *EJDICT_DISCIONARY; dict.look(word, mode).ok_or_else(|| { let kind = ErrorKind::NotFound { en: word.to_owned(), }; Error::from(kind) }) } /// Get matching candidate words. /// /// # Example /// /// ``` /// use ejdict_rs::SearchMode; /// /// # fn main() -> ejdict_rs::Result<()> { /// let candidates = ejdict_rs::candidates("apple", SearchMode::Fuzzy)?; /// for word in candidates { /// // something ... /// } /// # Ok(()) /// # } /// ``` /// pub fn candidates(word: &str, mode: SearchMode) -> Result<Candidates<Word>> { let dict = load_dictionary()?; Ok(dict.candidates(word, mode)) }