find_target/
lib.rs

1use std::{
2    fs::create_dir_all,
3    io::{Error, Result},
4    path::{Path, PathBuf},
5};
6
7pub use find_dir::{find_directory, find_directory_or_create, this_directory};
8
9mod find_dir;
10mod find_file;
11
12/// Ensure path is dir
13///
14/// # Arguments
15///
16/// * `path`:
17///
18/// returns: Result<PathBuf, Error>
19///
20/// # Examples
21///
22/// ```
23/// use find-target::ensure_directory;
24/// ```
25pub fn ensure_directory(path: &Path) -> Result<PathBuf> {
26    if path.is_dir() {
27        path.canonicalize()
28    }
29    else {
30        match path.parent() {
31            Some(s) => s.canonicalize(),
32            None => Err(Error::from_raw_os_error(10006)),
33        }
34    }
35}
36
37/// Ensure path is file
38///
39/// # Arguments
40///
41/// * `path`:
42/// * `name`:
43///
44/// returns: Result<PathBuf, Error>
45///
46/// # Examples
47///
48/// ```
49/// use find-target::ensure_file;
50/// ```
51pub fn ensure_file(path: &Path, name: &str) -> Result<PathBuf> {
52    if path.is_file() { path.canonicalize() } else { Ok(path.canonicalize()?.join(name)) }
53}