Skip to content

Commit

Permalink
Avoid creating multiple Vecs for languages
Browse files Browse the repository at this point in the history
* Make get_languages_from_env push to the Vec from the config
  • Loading branch information
acuteenvy committed Dec 25, 2023
1 parent 085be53 commit 1866fbe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<'a> Cache<'a> {
for (lang_dir, mut archive) in archives {
let lang_dir_full = self.0.join(&lang_dir);
if lang_dir_full.is_dir() {
fs::remove_dir_all(self.0.join(&lang_dir))?;
fs::remove_dir_all(&lang_dir_full)?;
}

#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use yansi::{Color, Style};

use crate::cache::Cache;
use crate::error::{Error, ErrorKind, Result};
use crate::util::warnln;
use crate::util::{get_languages_from_env, warnln};

#[derive(Serialize, Deserialize, Default, Clone, Copy)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Config {

cfg_res.map(|mut cfg| {
if cfg.cache.languages.is_empty() {
cfg.cache.languages = crate::util::get_languages_from_env();
get_languages_from_env(&mut cfg.cache.languages);
} else {
// English pages should always be downloaded and searched.
cfg.cache.languages.push("en".to_string());
Expand Down
46 changes: 27 additions & 19 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ macro_rules! info_end {
pub(crate) use {info_end, info_start, infoln, warnln};

/// Get languages from environment variables according to the tldr client specification.
pub fn get_languages_from_env() -> Vec<String> {
pub fn get_languages_from_env(out_vec: &mut Vec<String>) {
// https://github.com/tldr-pages/tldr/blob/main/CLIENT-SPECIFICATION.md#language

let var_lang = env::var("LANG").ok();

if var_lang.is_none() {
return vec!["en".to_string()];
out_vec.push("en".to_string());
return;
}

let var_lang = var_lang.unwrap();
let var_language = env::var("LANGUAGE").ok();

let mut result = vec![];
let languages = var_language
.as_deref()
.unwrap_or("")
Expand All @@ -81,17 +81,15 @@ pub fn get_languages_from_env() -> Vec<String> {
for lang in languages {
if lang.len() >= 5 && lang.chars().nth(2) == Some('_') {
// <language>_<country> (ll_CC - 5 characters)
result.push(&lang[..5]);
out_vec.push(lang[..5].to_string());
// <language> (ll - 2 characters)
result.push(&lang[..2]);
out_vec.push(lang[..2].to_string());
} else if lang.len() == 2 {
result.push(lang);
out_vec.push(lang.to_string());
}
}

result.push("en");

result.into_iter().map(String::from).collect()
out_vec.push("en".to_string());
}

/// Prepend `pages.` to each `String`.
Expand Down Expand Up @@ -228,29 +226,39 @@ mod tests {

#[test]
fn env_languages() {
// These vectors contain duplicates - de-dupping is done in cache.update()
// This vector contains duplicates - de-dupping is done in cache.update()
// and cache.find(), because update() requires a sorted vector, whereas
// find() - an unsorted one.
let mut out_vec = vec![];

prepare_env(Some("cz"), Some("it:cz:de"));
assert_eq!(get_languages_from_env(), ["it", "cz", "de", "cz", "en"]);
get_languages_from_env(&mut out_vec);
assert_eq!(out_vec, ["it", "cz", "de", "cz", "en"]);

prepare_env(Some("cz"), Some("it:de:fr"));
assert_eq!(get_languages_from_env(), ["it", "de", "fr", "cz", "en"]);
out_vec.clear();
get_languages_from_env(&mut out_vec);
assert_eq!(out_vec, ["it", "de", "fr", "cz", "en"]);

prepare_env(Some("it"), None);
assert_eq!(get_languages_from_env(), ["it", "en"]);
out_vec.clear();
get_languages_from_env(&mut out_vec);
assert_eq!(out_vec, ["it", "en"]);

prepare_env(None, Some("it:cz"));
assert_eq!(get_languages_from_env(), ["en"]);
out_vec.clear();
get_languages_from_env(&mut out_vec);
assert_eq!(out_vec, ["en"]);

prepare_env(None, None);
assert_eq!(get_languages_from_env(), ["en"]);
out_vec.clear();
get_languages_from_env(&mut out_vec);
assert_eq!(out_vec, ["en"]);

prepare_env(Some("en_US.UTF-8"), Some("de_DE.UTF-8:pl:en"));
assert_eq!(
get_languages_from_env(),
["de_DE", "de", "pl", "en", "en_US", "en", "en"]
);
out_vec.clear();
get_languages_from_env(&mut out_vec);
assert_eq!(out_vec, ["de_DE", "de", "pl", "en", "en_US", "en", "en"]);
}

#[test]
Expand Down

0 comments on commit 1866fbe

Please sign in to comment.