xdgkit/
utils.rs

1/*!
2# Utilities
3
4This just wraps repetative functions with utility value
5*/
6
7// utils.rs
8// Rusified in 2021 Copyright Israel Dahl. All rights reserved.
9// 
10//        /VVVV\
11//      /V      V\
12//    /V          V\
13//   /      0 0     \
14//   \|\|\</\/\>/|/|/
15//        \_/\_/
16// 
17// This program is free software; you can redistribute it and/or modify
18// it under the terms of the GNU General Public License version 2 as
19// published by the Free Software Foundation.
20//
21// This program is distributed in the hope that it will be useful,
22// but WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24// GNU General Public License for more details.
25// 
26// You should have received a copy of the GNU General Public License
27// along with this program; if not, write to the Free Software
28// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29use std::env;
30/// This function changes an `Option<String>` into an `Option<bool>` via make_ascii_lowercase()
31#[allow(dead_code)]
32pub fn to_bool(value:Option<String>)->Option<bool> {
33    value.as_ref()?;
34    let mut val = value.unwrap();
35    val.make_ascii_lowercase();
36    if val == "true" {
37        return Some(true)
38    }
39    Some(false)
40}
41#[allow(dead_code)]
42/// This function changes an `Option<String>` into an `Option<i32>`
43pub fn to_int(value:Option<String>)->Option<i32> {
44    value.as_ref()?;
45
46    let val = value.unwrap();
47    let int = val.parse::<i32>();
48    if int.is_ok() {
49        return int.ok()
50    }
51    None
52}
53/// This returns Some(value) OR None a.k.a. `Option<String>`
54///
55/// env::var returns Result, so we look for a language based on env::var(LANG||LANGUAGE) stripping the utf encoding and return as `Some(lang_for_desktop_files)`
56#[allow(dead_code)]
57pub fn get_language() -> Option<String> {
58    let lang = "LANG";
59    match env::var(lang) {
60        Ok(value) => {
61            let mut lang_var: String = value;
62            let pos = lang_var.chars()
63                              .position(|c| c == '.');
64            if let Some(posi) = pos {
65                if posi < lang_var.len() {
66                    let _junk = lang_var.split_off(posi);
67                }
68            }
69            Some(lang_var)
70        },
71        Err(_error) => {
72            let langu = "LANGUAGE";
73            match env::var(langu) {
74                Err(_error) => None,
75                Ok(value) => Some(value),
76            }
77                
78        },
79    }
80}