use std::fs; use std::path::{Path, PathBuf}; use crate::{Context, Result}; /// Validate and return a directory path. pub fn get_valid_dirpath>(path: P) -> Result where PathBuf: From

, { match PathBuf::from(path) { v if !v.exists() => bail!("path {} was not found or inaccessible", v.display()), v if !v.is_dir() => bail!("path {} is not a valid directory", v.display()), v => Ok(v), } } /// Get the directory name of a valid directory path. pub fn get_dirname>(path: P) -> Result where PathBuf: From

, { let path = get_valid_dirpath(path)?; match path.iter().last() { Some(v) => Ok(v.to_str().unwrap().to_owned()), _ => bail!( "directory name for path {} was not determined", path.display() ), } } /// Read the entire contents of a file into a bytes vector. pub fn read_bytes(path: &Path) -> Result> { fs::read(path).with_context(|| format!("failed to read file `{}`", path.display())) } /// Read the entire contents of a file into a bytes vector or default to empty. pub fn read_bytes_default(path: &Path) -> Vec { fs::read(path).unwrap_or_default() } /// Read an UTF-8 file from a specific path. pub fn read_file(path: &Path) -> Result { match String::from_utf8(read_bytes(path)?) { Ok(s) => Ok(s), Err(_) => bail!("path at `{}` was not valid utf-8", path.display()), } } pub fn stringify(dst: &mut String, path: &serde_ignored::Path<'_>) { use serde_ignored::Path; match *path { Path::Root => {} Path::Seq { parent, index } => { stringify(dst, parent); if !dst.is_empty() { dst.push('.'); } dst.push_str(&index.to_string()); } Path::Map { parent, ref key } => { stringify(dst, parent); if !dst.is_empty() { dst.push('.'); } dst.push_str(key); } Path::Some { parent } | Path::NewtypeVariant { parent } | Path::NewtypeStruct { parent } => stringify(dst, parent), } } #[cfg(unix)] /// In Unix-like systems it just casts the `PathBuf` into an string. pub fn adjust_canonicalization(p: PathBuf) -> String { p.display().to_string() } #[cfg(windows)] /// In Windows systems it adjusts the `PathBuf` stripping its `\\?\` prefix. pub fn adjust_canonicalization(p: PathBuf) -> String { const VERBATIM_PREFIX: &str = r#"\\?\"#; let p = p.to_str().unwrap_or_default(); let p = if p.starts_with(VERBATIM_PREFIX) { p.strip_prefix(VERBATIM_PREFIX).unwrap_or_default() } else { p }; p.to_owned() }