Skip to content

Commit

Permalink
Tweak the interface somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Sep 14, 2022
1 parent 671d2f0 commit 8bcbc6b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 19 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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", &[
Expand All @@ -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());
}
```

Expand Down
9 changes: 6 additions & 3 deletions src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ impl Language {
///
/// # Arguments
/// * `name` - String to find
pub fn get(&self, name: &str) -> Option<String> {
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())
}
}
}

Expand Down Expand Up @@ -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);
}
}
33 changes: 28 additions & 5 deletions src/language_set.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ops::Index;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand Down Expand Up @@ -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<String> {
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)
Expand All @@ -102,7 +103,7 @@ impl LanguageSet {
///
/// # Arguments
/// * `name` - String to find
pub fn get(&self, name: &str) -> Option<String> {
pub fn get(&self, name: &str) -> Option<&str> {
if let Some(s) = self.get_from_lang(&self.current, name) {
return Some(s)
}
Expand All @@ -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::*;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
}
}
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", &[
Expand All @@ -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)]

Expand Down
8 changes: 4 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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");
}
}

0 comments on commit 8bcbc6b

Please sign in to comment.