Updated logger options, changed filetype behavior
Replaced lots of warns by errors Filetype detection won’t return an error on unknown extension anymore, but will return an `Other` value so it can be handled by other functions not as an error already. See upcoming commits.
This commit is contained in:
parent
e9a161f526
commit
d200367ee0
@ -1,5 +1,5 @@
|
||||
extern crate log;
|
||||
use log::{info, warn};
|
||||
use log::{info, error};
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Result};
|
||||
@ -13,6 +13,10 @@ pub enum SettingsType {
|
||||
|
||||
/// Files ending with the `json` extension
|
||||
Json,
|
||||
|
||||
/// Other file type, used to describe files without an extension or with an
|
||||
/// unsupported extension
|
||||
Other,
|
||||
}
|
||||
|
||||
/// Read a file’s content into a `String`
|
||||
@ -27,7 +31,7 @@ pub fn read_file(path: &Path) -> Result<String> {
|
||||
let display = path.display();
|
||||
let mut file = match File::open(&path) {
|
||||
Err(why) => {
|
||||
warn!("Could not read {}: {}", display, why.to_string());
|
||||
error!("Could not read {}: {}", display, why.to_string());
|
||||
return Err(why);
|
||||
}
|
||||
Ok(file) => file,
|
||||
@ -35,7 +39,7 @@ pub fn read_file(path: &Path) -> Result<String> {
|
||||
let mut content = String::new();
|
||||
match file.read_to_string(&mut content) {
|
||||
Err(why) => {
|
||||
warn!("Could not read {}: {}", display, why.to_string());
|
||||
error!("Could not read {}: {}", display, why.to_string());
|
||||
return Err(why);
|
||||
}
|
||||
Ok(_) => {
|
||||
@ -61,14 +65,14 @@ where
|
||||
use std::io::prelude::*;
|
||||
let mut file = match File::create(&path) {
|
||||
Err(e) => {
|
||||
warn!("Could not open file {}: {}", path.display(), e.to_string());
|
||||
error!("Could not open file {}: {}", path.display(), e.to_string());
|
||||
return Err(e);
|
||||
}
|
||||
Ok(file) => file,
|
||||
};
|
||||
match file.write_all(content.to_string().as_bytes()) {
|
||||
Err(e) => {
|
||||
warn!(
|
||||
error!(
|
||||
"Could not write settings to file {}: {}",
|
||||
path.display(),
|
||||
e.to_string()
|
||||
@ -97,24 +101,19 @@ where
|
||||
/// ```
|
||||
/// let file_json = std::path::Path::new("file.json");
|
||||
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::Json,
|
||||
/// lang_evolve_core::settings::utils::get_file_type(&file_json).unwrap());
|
||||
/// lang_evolve_core::settings::utils::get_file_type(&file_json));
|
||||
///
|
||||
/// let file_yaml = std::path::Path::new("file.yaml");
|
||||
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::Yaml,
|
||||
/// lang_evolve_core::settings::utils::get_file_type(&file_yaml).unwrap());
|
||||
/// lang_evolve_core::settings::utils::get_file_type(&file_yaml));
|
||||
///
|
||||
/// let file_yml = std::path::Path::new("file.yml");
|
||||
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::Yaml,
|
||||
/// lang_evolve_core::settings::utils::get_file_type(&file_yml).unwrap());
|
||||
/// lang_evolve_core::settings::utils::get_file_type(&file_yml));
|
||||
/// ```
|
||||
pub fn get_file_type(path: &Path) -> Result<SettingsType> {
|
||||
pub fn get_file_type(path: &Path) -> SettingsType {
|
||||
let extension = match path.extension() {
|
||||
None => {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
"File has no extension",
|
||||
));
|
||||
}
|
||||
None => { return SettingsType::Other; }
|
||||
Some(val) => val,
|
||||
};
|
||||
let extension = extension
|
||||
@ -122,11 +121,8 @@ pub fn get_file_type(path: &Path) -> Result<SettingsType> {
|
||||
.expect("Could not get String out of extension")
|
||||
.to_lowercase();
|
||||
match extension.as_str() {
|
||||
"yml" | "yaml" => Ok(SettingsType::Yaml),
|
||||
"json" => Ok(SettingsType::Json),
|
||||
_ => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
"Invalid extension",
|
||||
)),
|
||||
"yml" | "yaml" => SettingsType::Yaml,
|
||||
"json" => SettingsType::Json,
|
||||
_ => SettingsType::Other,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user