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;
|
extern crate log;
|
||||||
use log::{info, warn};
|
use log::{info, error};
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Read, Result};
|
use std::io::{Read, Result};
|
||||||
@ -13,6 +13,10 @@ pub enum SettingsType {
|
|||||||
|
|
||||||
/// Files ending with the `json` extension
|
/// Files ending with the `json` extension
|
||||||
Json,
|
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`
|
/// 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 display = path.display();
|
||||||
let mut file = match File::open(&path) {
|
let mut file = match File::open(&path) {
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
warn!("Could not read {}: {}", display, why.to_string());
|
error!("Could not read {}: {}", display, why.to_string());
|
||||||
return Err(why);
|
return Err(why);
|
||||||
}
|
}
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
@ -35,7 +39,7 @@ pub fn read_file(path: &Path) -> Result<String> {
|
|||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
match file.read_to_string(&mut content) {
|
match file.read_to_string(&mut content) {
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
warn!("Could not read {}: {}", display, why.to_string());
|
error!("Could not read {}: {}", display, why.to_string());
|
||||||
return Err(why);
|
return Err(why);
|
||||||
}
|
}
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
@ -61,14 +65,14 @@ where
|
|||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
let mut file = match File::create(&path) {
|
let mut file = match File::create(&path) {
|
||||||
Err(e) => {
|
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);
|
return Err(e);
|
||||||
}
|
}
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
};
|
};
|
||||||
match file.write_all(content.to_string().as_bytes()) {
|
match file.write_all(content.to_string().as_bytes()) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!(
|
error!(
|
||||||
"Could not write settings to file {}: {}",
|
"Could not write settings to file {}: {}",
|
||||||
path.display(),
|
path.display(),
|
||||||
e.to_string()
|
e.to_string()
|
||||||
@ -97,24 +101,19 @@ where
|
|||||||
/// ```
|
/// ```
|
||||||
/// let file_json = std::path::Path::new("file.json");
|
/// let file_json = std::path::Path::new("file.json");
|
||||||
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::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");
|
/// let file_yaml = std::path::Path::new("file.yaml");
|
||||||
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::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");
|
/// let file_yml = std::path::Path::new("file.yml");
|
||||||
/// assert_eq!(lang_evolve_core::settings::utils::SettingsType::Yaml,
|
/// 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() {
|
let extension = match path.extension() {
|
||||||
None => {
|
None => { return SettingsType::Other; }
|
||||||
return Err(std::io::Error::new(
|
|
||||||
std::io::ErrorKind::InvalidInput,
|
|
||||||
"File has no extension",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
Some(val) => val,
|
Some(val) => val,
|
||||||
};
|
};
|
||||||
let extension = extension
|
let extension = extension
|
||||||
@ -122,11 +121,8 @@ pub fn get_file_type(path: &Path) -> Result<SettingsType> {
|
|||||||
.expect("Could not get String out of extension")
|
.expect("Could not get String out of extension")
|
||||||
.to_lowercase();
|
.to_lowercase();
|
||||||
match extension.as_str() {
|
match extension.as_str() {
|
||||||
"yml" | "yaml" => Ok(SettingsType::Yaml),
|
"yml" | "yaml" => SettingsType::Yaml,
|
||||||
"json" => Ok(SettingsType::Json),
|
"json" => SettingsType::Json,
|
||||||
_ => Err(std::io::Error::new(
|
_ => SettingsType::Other,
|
||||||
std::io::ErrorKind::InvalidInput,
|
|
||||||
"Invalid extension",
|
|
||||||
)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user