2020-03-26 19:10:07 +00:00
|
|
|
|
extern crate log;
|
2020-03-28 20:59:23 +00:00
|
|
|
|
use log::{info, error};
|
2020-03-26 19:10:07 +00:00
|
|
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::{Read, Result};
|
2020-03-27 17:25:20 +00:00
|
|
|
|
use std::path::Path;
|
2020-03-26 19:10:07 +00:00
|
|
|
|
|
2020-03-27 17:26:31 +00:00
|
|
|
|
/// Type of supported settings format: yaml or json
|
2020-03-27 22:26:16 +00:00
|
|
|
|
#[derive(Debug, PartialEq)]
|
2020-03-27 17:25:20 +00:00
|
|
|
|
pub enum SettingsType {
|
2020-03-27 17:26:31 +00:00
|
|
|
|
/// Files ending with the `yml` or `yaml` extension
|
2020-03-27 17:25:20 +00:00
|
|
|
|
Yaml,
|
2020-03-27 17:26:31 +00:00
|
|
|
|
|
|
|
|
|
/// Files ending with the `json` extension
|
2020-03-27 17:25:20 +00:00
|
|
|
|
Json,
|
2020-03-28 20:59:23 +00:00
|
|
|
|
|
|
|
|
|
/// Other file type, used to describe files without an extension or with an
|
|
|
|
|
/// unsupported extension
|
|
|
|
|
Other,
|
2020-03-27 17:25:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 17:26:31 +00:00
|
|
|
|
/// Read a file’s content into a `String`
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// let path = std::path::Path::new("./some/path/to/my/file.json");
|
|
|
|
|
/// let content = lang_evolve_core::settings::utils::read_file(&path).unwrap();
|
|
|
|
|
/// ```
|
2020-03-27 17:25:20 +00:00
|
|
|
|
pub fn read_file(path: &Path) -> Result<String> {
|
2020-03-26 19:10:07 +00:00
|
|
|
|
let display = path.display();
|
|
|
|
|
let mut file = match File::open(&path) {
|
|
|
|
|
Err(why) => {
|
2020-03-28 20:59:23 +00:00
|
|
|
|
error!("Could not read {}: {}", display, why.to_string());
|
2020-03-26 19:10:07 +00:00
|
|
|
|
return Err(why);
|
|
|
|
|
}
|
|
|
|
|
Ok(file) => file,
|
|
|
|
|
};
|
|
|
|
|
let mut content = String::new();
|
|
|
|
|
match file.read_to_string(&mut content) {
|
|
|
|
|
Err(why) => {
|
2020-03-28 20:59:23 +00:00
|
|
|
|
error!("Could not read {}: {}", display, why.to_string());
|
2020-03-26 19:10:07 +00:00
|
|
|
|
return Err(why);
|
|
|
|
|
}
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
info!("Content of {} read", display);
|
|
|
|
|
Ok(content)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-27 17:25:20 +00:00
|
|
|
|
|
2020-03-27 17:26:31 +00:00
|
|
|
|
/// Write a `String` into a file
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// let content = String::from("This is my content");
|
|
|
|
|
/// let path = std::path::Path::new("./path/to/my/file.txt");
|
|
|
|
|
/// lang_evolve_core::settings::utils::write_file(&path, &content).unwrap();
|
|
|
|
|
/// ```
|
2020-03-27 18:09:34 +00:00
|
|
|
|
pub fn write_file<S>(path: &Path, content: &S) -> Result<()>
|
|
|
|
|
where
|
|
|
|
|
S: std::string::ToString,
|
|
|
|
|
{
|
2020-03-27 17:25:20 +00:00
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
let mut file = match File::create(&path) {
|
|
|
|
|
Err(e) => {
|
2020-03-28 20:59:23 +00:00
|
|
|
|
error!("Could not open file {}: {}", path.display(), e.to_string());
|
2020-03-27 17:25:20 +00:00
|
|
|
|
return Err(e);
|
|
|
|
|
}
|
|
|
|
|
Ok(file) => file,
|
|
|
|
|
};
|
2020-03-27 18:09:34 +00:00
|
|
|
|
match file.write_all(content.to_string().as_bytes()) {
|
2020-03-27 17:25:20 +00:00
|
|
|
|
Err(e) => {
|
2020-03-28 20:59:23 +00:00
|
|
|
|
error!(
|
2020-03-27 17:25:20 +00:00
|
|
|
|
"Could not write settings to file {}: {}",
|
|
|
|
|
path.display(),
|
|
|
|
|
e.to_string()
|
|
|
|
|
);
|
|
|
|
|
return Err(e);
|
|
|
|
|
}
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
info!("Wrote settings to file {}", path.display());
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-03-27 22:26:16 +00:00
|
|
|
|
info!("Successfuly written {}", path.display());
|
2020-03-27 17:25:20 +00:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 22:27:00 +00:00
|
|
|
|
/// Get the type of file to be opened based on its extension. Currently
|
|
|
|
|
/// supported file types are:
|
|
|
|
|
/// * JSON - `.json` extension
|
|
|
|
|
/// * Yaml - `.yml` or `.yaml` extensions
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// - `path` - Path of the file to be determined
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// let file_json = std::path::Path::new("file.json");
|
|
|
|
|
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::Json,
|
2020-03-28 20:59:23 +00:00
|
|
|
|
/// lang_evolve_core::settings::utils::get_file_type(&file_json));
|
2020-03-27 22:27:00 +00:00
|
|
|
|
///
|
|
|
|
|
/// let file_yaml = std::path::Path::new("file.yaml");
|
|
|
|
|
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::Yaml,
|
2020-03-28 20:59:23 +00:00
|
|
|
|
/// lang_evolve_core::settings::utils::get_file_type(&file_yaml));
|
2020-03-27 22:27:00 +00:00
|
|
|
|
///
|
|
|
|
|
/// let file_yml = std::path::Path::new("file.yml");
|
|
|
|
|
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::Yaml,
|
2020-03-28 20:59:23 +00:00
|
|
|
|
/// lang_evolve_core::settings::utils::get_file_type(&file_yml));
|
2020-03-27 22:27:00 +00:00
|
|
|
|
/// ```
|
2020-03-28 20:59:23 +00:00
|
|
|
|
pub fn get_file_type(path: &Path) -> SettingsType {
|
2020-03-27 17:25:20 +00:00
|
|
|
|
let extension = match path.extension() {
|
2020-03-28 20:59:23 +00:00
|
|
|
|
None => { return SettingsType::Other; }
|
2020-03-27 17:25:20 +00:00
|
|
|
|
Some(val) => val,
|
|
|
|
|
};
|
|
|
|
|
let extension = extension
|
|
|
|
|
.to_str()
|
|
|
|
|
.expect("Could not get String out of extension")
|
|
|
|
|
.to_lowercase();
|
|
|
|
|
match extension.as_str() {
|
2020-03-28 20:59:23 +00:00
|
|
|
|
"yml" | "yaml" => SettingsType::Yaml,
|
|
|
|
|
"json" => SettingsType::Json,
|
|
|
|
|
_ => SettingsType::Other,
|
2020-03-27 17:25:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|