1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::{env, path::Path};
#[derive(Clone)]
pub struct Config {
directory: String,
languages: Vec<String>,
}
impl Config {
/// Create a new configuration.
///
/// # Example
/// ```rust, ignore
/// use languages_rs::Config;
///
/// let config: Config = match Config::new("languages", vec!["en"]) {
/// Ok(config) => config,
/// Err(e) => {
/// eprintln!("Error: {}", e);
/// return;
/// },
/// };
/// ```
pub fn new(directory: &str, languages: Vec<&str>) -> anyhow::Result<Self> {
let path = Path::new(&env::current_dir()?).join(directory);
if !path.exists() {
return Err(anyhow::Error::msg(format!(
"Cannot find `{}` directory.",
path.display()
)));
} else if !path.is_dir() {
return Err(anyhow::Error::msg(format!(
"The path `{}` is not a directory.",
path.display()
)));
}
Ok(Self {
directory: path.display().to_string(),
languages: languages.iter().map(|e| String::from(*e)).collect(),
})
}
/// Get the default configuration.
///
/// # Default
/// ```json
/// {
/// "directory": "languages/",
/// "languages": []
/// ```
///
/// # Example
/// ```rust, ignore
/// use languages_rs::Config;
///
/// let config: Config = match Config::default() {
/// Ok(config) => config,
/// Err(e) => {
/// eprintln!("Error: {}", e);
/// return;
/// },
/// };
/// ```
pub fn default() -> anyhow::Result<Self> {
let path = Path::new(&env::current_dir()?).join("languages");
if !path.exists() {
std::fs::create_dir(&path)?;
} else if !path.is_dir() {
return Err(anyhow::Error::msg(format!(
"The path `{}` is not a directory.",
path.display()
)));
}
Ok(Self {
directory: path.display().to_string(),
languages: Vec::new(),
})
}
/// Get the languages directory.
///
/// # Example
/// ```rust, ignore
/// use std::{env, path};
///
/// use languages_rs::Config;
///
/// let config = Config::default().unwrap();
/// assert_eq!(
/// config.get_directory(),
/// format!(
/// "{}{}languages",
/// env::current_dir().unwrap().display(),
/// path::MAIN_SEPARATOR,
/// ),
/// );
/// ```
pub fn get_directory(&self) -> String {
self.directory.clone()
}
/// Change the languages directory.
///
/// # Example
/// ```rust, ignore
/// use std::env;
///
/// use languages_rs::Config;
///
/// let mut config = Config::default().unwrap();
/// assert!(config.set_directory("languages").is_ok());
/// ```
pub fn set_directory(&mut self, new_directory: &str) -> anyhow::Result<()> {
let path = Path::new(&env::current_dir()?).join(new_directory);
if !path.exists() {
return Err(anyhow::Error::msg(format!(
"Cannot find `{}` directory.",
path.display()
)));
} else if !path.is_dir() {
return Err(anyhow::Error::msg(format!(
"The path `{}` is not a directory.",
path.display()
)));
}
self.directory = path.display().to_string();
Ok(())
}
/// Get the availables languages.
///
/// # Example
/// ```rust, ignore
/// use languages_rs::Config;
///
/// let config = Config::default().unwrap();
/// assert_eq!(config.get_languages(), Vec::<String>::new());
/// ```
pub fn get_languages(&self) -> Vec<String> {
self.languages.clone()
}
/// Add a new language to the languages list if it does not exist.
///
/// # Example
/// ```rust, ignore
/// use languages_rs::Config;
///
/// let mut config = Config::default().unwrap();
/// assert_eq!(config.get_languages(), Vec::<String>::new());
/// assert!(config.add_language(String::from("en")).is_ok());
/// assert_eq!(config.get_languages(), vec![String::from("en")]);
/// ```
pub fn add_language(&mut self, language: String) -> anyhow::Result<()> {
if self.languages.contains(&language) {
return Err(anyhow::Error::msg(format!(
"The language `{}` already exists.",
language
)));
}
self.languages.push(language);
Ok(())
}
}