utils: clean fs
This commit is contained in:
parent
13d8340020
commit
6e7ce88396
|
@ -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::*;
|
||||||
|
|
Reference in a new issue