From 8bcbc6b5ed50d897531a03af2559a87b5b6ba158 Mon Sep 17 00:00:00 2001 From: Richard Carson Date: Wed, 14 Sep 2022 16:38:36 -0400 Subject: [PATCH] Tweak the interface somewhat --- .github/workflows/dependabot.yml | 9 +++++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 4 ++-- src/language.rs | 9 ++++++--- src/language_set.rs | 33 +++++++++++++++++++++++++++----- src/lib.rs | 6 +++--- src/macros.rs | 8 ++++---- 8 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/dependabot.yml diff --git a/.github/workflows/dependabot.yml b/.github/workflows/dependabot.yml new file mode 100644 index 0000000..05378e7 --- /dev/null +++ b/.github/workflows/dependabot.yml @@ -0,0 +1,9 @@ +version: 2 +updates: + # Enable version updates for cargo + - package-ecosystem: "cargo" + # Look for `package.json` and `lock` files in the `root` directory + directory: "/" + # Check the npm registry for updates every day (weekdays) + schedule: + interval: "daily" \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 052221e..8bfe71c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "embedded-lang" -version = "0.6.0" +version = "0.7.0" dependencies = [ "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index b6895b6..83d14bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ keywords = ["language", "text", "json", "embedded"] categories = ["accessibility", "encoding", "localization"] repository = "https://github.com/rscarson/embedded_lang" readme = "readme.md" -version = "0.6.0" +version = "0.7.0" edition = "2021" [dependencies] diff --git a/README.md b/README.md index e731291..53a70ce 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Please see the examples directory for language file samples Usage example: ```rust -use embedded_lang::{ LanguageSet, embedded_language, get_string }; +use embedded_lang::{ LanguageSet, embedded_language }; fn main() { let mut translator = LanguageSet::new("fr", &[ @@ -19,7 +19,7 @@ fn main() { ]); translator.set_fallback_language("en"); - assert_eq!(get_string!(translator, "tree"), "arbre".to_string()); + assert_eq!(translator["tree"], "arbre".to_string()); } ``` diff --git a/src/language.rs b/src/language.rs index 14d17e7..257791b 100644 --- a/src/language.rs +++ b/src/language.rs @@ -65,8 +65,11 @@ impl Language { /// /// # Arguments /// * `name` - String to find - pub fn get(&self, name: &str) -> Option { - self.strings.get(name).cloned() + pub fn get(&self, name: &str) -> Option<&str> { + match self.strings.get(name) { + None => None, + Some(s) => Some(s.as_str()) + } } } @@ -106,7 +109,7 @@ mod test_token { fn test_get() { let lang = embedded_language!("../examples/en.lang.json"); - assert_eq!(lang.get("hello_msg"), Some("hello world!".to_string())); + assert_eq!(lang.get("hello_msg"), Some("hello world!")); assert_eq!(lang.get("goodbye_msg"), None); } } \ No newline at end of file diff --git a/src/language_set.rs b/src/language_set.rs index f325613..d2804aa 100644 --- a/src/language_set.rs +++ b/src/language_set.rs @@ -1,3 +1,4 @@ +use std::ops::Index; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -88,7 +89,7 @@ impl LanguageSet { /// # Arguments /// * `language` - Language to search /// * `name` - String to find - pub fn get_from_lang(&self, language: &str, name: &str) -> Option { + pub fn get_from_lang(&self, language: &str, name: &str) -> Option<&str> { if let Some(lang) = self.languages.get(language) { if let Some(s) = lang.get(name) { return Some(s) @@ -102,7 +103,7 @@ impl LanguageSet { /// /// # Arguments /// * `name` - String to find - pub fn get(&self, name: &str) -> Option { + pub fn get(&self, name: &str) -> Option<&str> { if let Some(s) = self.get_from_lang(&self.current, name) { return Some(s) } @@ -115,6 +116,15 @@ impl LanguageSet { } } +impl Index<&str> for LanguageSet { + type Output = str; + + fn index(&self, name: &str) -> &Self::Output { + self.get(name).unwrap_or_default() + } +} + + #[cfg(test)] mod test_token { use super::*; @@ -204,7 +214,7 @@ mod test_token { ]); set.set_fallback_language("en"); - assert_eq!(set.get_from_lang("fr", "tree"), Some("arbre".to_string())); + assert_eq!(set.get_from_lang("fr", "tree"), Some("arbre")); assert_eq!(set.get_from_lang("fr", "mustard"), None); assert_eq!(set.get_from_lang("en", "nope"), None); } @@ -217,8 +227,21 @@ mod test_token { ]); set.set_fallback_language("en"); - assert_eq!(set.get("tree"), Some("arbre".to_string())); - assert_eq!(set.get("mustard"), Some("mustard".to_string())); + assert_eq!(set.get("tree"), Some("arbre")); + assert_eq!(set.get("mustard"), Some("mustard")); assert_eq!(set.get("nope"), None); } + + #[test] + fn test_index() { + let mut set = LanguageSet::new("fr", &[ + embedded_language!("../examples/en.lang.json"), + embedded_language!("../examples/fr.lang.json"), + ]); + set.set_fallback_language("en"); + + assert_eq!(set["tree"], "arbre".to_string()); + assert_eq!(set["mustard"], "mustard".to_string()); + assert_eq!(set["nope"], "".to_string()); + } } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 92818cb..7a76b18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ //! //! Usage example: //! ```rust -//! use embedded_lang::{ LanguageSet, embedded_language, get_string }; +//! use embedded_lang::{ LanguageSet, embedded_language }; //! //! fn main() { //! let mut translator = LanguageSet::new("fr", &[ @@ -14,13 +14,13 @@ //! ]); //! translator.set_fallback_language("en"); //! -//! assert_eq!(get_string!(translator, "tree"), "arbre".to_string()); +//! assert_eq!(translator["tree"], "arbre".to_string()); //! } //! ``` //! //! LanguageSets have a current language, and a fallback language from which strings will be fetched //! if the current language is missing the requested string. -#![doc(html_root_url = "https://docs.rs/embedded-lang/0.6.0")] +#![doc(html_root_url = "https://docs.rs/embedded-lang/0.7.0")] #![warn(missing_docs)] #![warn(rustdoc::missing_doc_code_examples)] diff --git a/src/macros.rs b/src/macros.rs index 242c5fb..1ddb826 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -28,7 +28,7 @@ mod test_token { #[test] fn test_embedded_language() { let lang = embedded_language!("../examples/en.lang.json"); - assert_eq!(lang.get("hello_msg"), Some("hello world!".to_string())); + assert_eq!(lang.get("hello_msg"), Some("hello world!")); } #[test] @@ -42,8 +42,8 @@ mod test_token { assert_eq!(get_string!(LanguageSet::new("fr", &[ embedded_language!("../examples/en.lang.json"), embedded_language!("../examples/fr.lang.json"), - ]), "foobar"), "".to_string()); - assert_eq!(get_string!(set, "foobar"), "".to_string()); - assert_eq!(get_string!(set, "mustard"), "mustard".to_string()); + ]), "foobar"), ""); + assert_eq!(get_string!(set, "foobar"), ""); + assert_eq!(get_string!(set, "mustard"), "mustard"); } } \ No newline at end of file