utils: clean fs

This commit is contained in:
dece 2020-05-24 16:12:25 +02:00
parent 13d8340020
commit 6e7ce88396

View file

@ -2,17 +2,6 @@ use std::fs;
use std::io::{self, Read}; use std::io::{self, Read};
use std::path; use std::path;
/// Ensure a directory exists, creating it with parents if necessary.
pub fn ensure_dir_exists(path: &path::Path) -> Result<(), io::Error> {
if !path.is_dir() {
if path.exists() {
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Not a directory."));
}
fs::create_dir_all(&path)?;
}
Ok(())
}
/// Strip the extension from a file path. /// Strip the extension from a file path.
pub fn strip_extension(path: &path::PathBuf) -> Option<path::PathBuf> { pub fn strip_extension(path: &path::PathBuf) -> Option<path::PathBuf> {
let mut pb = path::PathBuf::from(&path); let mut pb = path::PathBuf::from(&path);
@ -33,6 +22,17 @@ pub fn open_file_to_vec(path: &path::Path) -> Result<Vec<u8>, io::Error> {
Ok(data) Ok(data)
} }
/// Ensure a directory exists, creating it with parents if necessary.
pub fn ensure_dir_exists(path: &path::Path) -> Result<(), io::Error> {
if !path.is_dir() {
if path.exists() {
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Not a directory."));
}
fs::create_dir_all(&path)?;
}
Ok(())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;