summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
author zaaarf <me@zaaarf.foo>2024-05-15 19:00:21 +0200
committer zaaarf <me@zaaarf.foo>2024-05-15 19:00:21 +0200
commit066e74ea1d111086e2e2fcf3269791f08186e2e2 (patch)
tree5b16a2d6511258a169f531660d1de63c6b853ee5 /src/lib.rs
parent31f03928787ec29e72ebb91507b84b0ca68841d6 (diff)
feat: made more genericHEAD0.2.1dev
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 34d390c..074fc9d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -43,10 +43,10 @@ impl Localiser {
/// in the second case the same will be done to their chilren instead.
/// [FluentResource]s within a same folder will be considered part of a same [FluentBundle],
/// forming a single localisation for all intents and purposes.
- pub fn try_load(path: &str, default_language: &str) -> Result<Self> {
+ pub fn try_load(path: impl Into<String>, default_language: impl Into<String>) -> Result<Self> {
let mut bundles = HashMap::new();
let mut available_languages = HashMap::new();
- let paths = std::fs::read_dir(path)?
+ let paths = std::fs::read_dir(path.into())?
.filter_map(|res| res.ok())
.map(|dir_entry| dir_entry.path())
.filter_map(|path| {
@@ -58,7 +58,7 @@ impl Localiser {
}).collect::<Vec<_>>();
// validate default
- let default_language = default_language.parse::<LanguageIdentifier>()?.to_string();
+ let default_language = default_language.into().parse::<LanguageIdentifier>()?.to_string();
for path in paths {
// validate filename as language code
@@ -118,12 +118,15 @@ impl Localiser {
}
/// Extracts a message from the requested bundle, or from the default one if absent.
- pub fn try_get_message(&self, key: &str, language: &str, args: Option<&FluentArgs>) -> Result<String> {
- let bundle = self.bundles.get(language)
+ pub fn try_get_message(&self, key: impl Into<String>, language: impl Into<String>, args: Option<&FluentArgs>) -> Result<String> {
+ let key = key.into();
+ let language = language.into();
+
+ let bundle = self.bundles.get(&language)
.or_else(|| self.bundles.get(&self.default_language))
.ok_or(error::Error::GenericError("Failed to get default bundle! This is not supposed to happen!".to_string()))?;
- let pattern = bundle.get_message(key)
+ let pattern = bundle.get_message(&key)
.and_then(|msg| msg.value())
.ok_or(error::Error::MissingMessageError(format!("No such message {} for language {}!", key, language)))?;
@@ -137,8 +140,9 @@ impl Localiser {
}
/// Similar to [Localiser::try_get_message], but returns the given key on failure.
- pub fn get_message(&self, key: &str, language: &str, args: Option<&FluentArgs>) -> String {
- self.try_get_message(key, language, args)
- .unwrap_or(key.to_string())
+ pub fn get_message(&self, key: impl Into<String>, language: impl Into<String>, args: Option<&FluentArgs>) -> String {
+ let key = key.into();
+ self.try_get_message(&key, language, args)
+ .unwrap_or(key)
}
}