1use std::fs::File;
2use std::io::ErrorKind::{IsADirectory, self};
3use std::io::Read;
4use std::path::PathBuf;
5use std::process::exit;
6
7use locale_config::Locale;
8
9#[macro_export]
10macro_rules! hashmap {
11 {} => { std::collections::HashMap::new() };
12 { $({$k:expr => $v:expr}),* } => {
13 {
14 #[allow(unused_mut)]
15 let mut m = std::collections::HashMap::new();
16 $(
17 m.insert($k, $v);
18 )*
19 m
20 }
21 };
22}
23
24#[derive(Debug)]
25pub enum FSErr {
26 IsADir,
27 Other(ErrorKind),
28}
29
30pub enum SupportedLanguage {
31 Turkish,
32 English,
33}
34
35pub fn error_print<T>(error_name: &str, error_explanation: T) -> !
36where
37 T: std::fmt::Debug,
38{
39 eprintln!("{}: {:?}", error_name, error_explanation);
40 exit(1);
41}
42
43pub fn read_file(path: &PathBuf) -> Result<String, FSErr> {
44 let mut file = match File::open(path.clone()) {
45 Err(e) => error_print(
46 "error opening file",
47 format!("{}: {}", e, path.as_path().display()),
48 ),
49 Ok(f) => f,
50 };
51
52 let mut buf = String::new();
53 match file.read_to_string(&mut buf) {
54 Ok(_) => (),
55 Err(err) => match err.kind() {
56 IsADirectory => return Err(FSErr::IsADir),
57 e => return Err(FSErr::Other(e)),
58 },
59 };
60 Ok(buf)
61}
62
63pub fn get_lang() -> SupportedLanguage {
66 let l = format!("{}", Locale::user_default());
67 let lang = match l.split_once(',') {
71 None => l,
72 Some((a, _)) => a.to_string(),
73 };
74 match lang.as_str() {
75 "tr-TR" => SupportedLanguage::Turkish,
76 _ => SupportedLanguage::English,
77 }
78}
79
80pub fn char_in_str(a: char, b: &str) -> bool {
81 for ch in b.chars() {
82 if ch == a {
83 return true;
84 }
85 }
86 false
87}
88
89pub fn in_vec<T>(a: &T, v: &Vec<T>) -> bool
93where
94 T: PartialEq,
95{
96 for item in v.iter() {
97 if item == a {
98 return true;
99 }
100 }
101 false
102}
103