|
1 |
| -pub mod path; |
| 1 | +use std::path::{Path, PathBuf, Component}; |
| 2 | +use std::error::Error; |
| 3 | +use std::fs::{self, metadata, File}; |
| 4 | + |
| 5 | + |
| 6 | +/// Takes a path and returns a path containing just enough `../` to point to the root of the given path. |
| 7 | +/// |
| 8 | +/// This is mostly interesting for a relative path to point back to the directory from where the |
| 9 | +/// path starts. |
| 10 | +/// |
| 11 | +/// ```ignore |
| 12 | +/// let mut path = Path::new("some/relative/path"); |
| 13 | +/// |
| 14 | +/// println!("{}", path_to_root(&path)); |
| 15 | +/// ``` |
| 16 | +/// |
| 17 | +/// **Outputs** |
| 18 | +/// |
| 19 | +/// ```text |
| 20 | +/// "../../" |
| 21 | +/// ``` |
| 22 | +/// |
| 23 | +/// **note:** it's not very fool-proof, if you find a situation where it doesn't return the correct |
| 24 | +/// path. Consider [submitting a new issue](https://github.com/azerupi/mdBook/issues) or a |
| 25 | +/// [pull-request](https://github.com/azerupi/mdBook/pulls) to improve it. |
| 26 | +
|
| 27 | +pub fn path_to_root(path: &Path) -> String { |
| 28 | + debug!("[fn]: path_to_root"); |
| 29 | + // Remove filename and add "../" for every directory |
| 30 | + |
| 31 | + path.to_path_buf().parent().expect("") |
| 32 | + .components().fold(String::new(), |mut s, c| { |
| 33 | + match c { |
| 34 | + Component::Normal(_) => s.push_str("../"), |
| 35 | + _ => { |
| 36 | + debug!("[*]: Other path component... {:?}", c); |
| 37 | + } |
| 38 | + } |
| 39 | + s |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +/// This function checks for every component in a path if the directory exists, |
| 44 | +/// if it does not it is created. |
| 45 | +
|
| 46 | +pub fn create_path(path: &Path) -> Result<(), Box<Error>> { |
| 47 | + debug!("[fn]: create_path"); |
| 48 | + |
| 49 | + // Create directories if they do not exist |
| 50 | + let mut constructed_path = PathBuf::new(); |
| 51 | + |
| 52 | + for component in path.components() { |
| 53 | + |
| 54 | + let mut dir; |
| 55 | + match component { |
| 56 | + Component::Normal(_) => { dir = PathBuf::from(component.as_os_str()); }, |
| 57 | + Component::RootDir => { |
| 58 | + debug!("[*]: Root directory"); |
| 59 | + // This doesn't look very compatible with Windows... |
| 60 | + constructed_path.push("/"); |
| 61 | + continue |
| 62 | + }, |
| 63 | + _ => continue, |
| 64 | + } |
| 65 | + |
| 66 | + constructed_path.push(&dir); |
| 67 | + debug!("[*]: {:?}", constructed_path); |
| 68 | + |
| 69 | + // Check if path exists |
| 70 | + match metadata(&constructed_path) { |
| 71 | + // Any way to combine the Err and first Ok branch ?? |
| 72 | + Err(_) => { |
| 73 | + try!(fs::create_dir(&constructed_path)); |
| 74 | + debug!("[*]: Directory created {:?}", constructed_path); |
| 75 | + }, |
| 76 | + Ok(f) => { |
| 77 | + if !f.is_dir() { |
| 78 | + try!(fs::create_dir(&constructed_path)); |
| 79 | + debug!("[*]: Directory created {:?}", constructed_path); |
| 80 | + } else { |
| 81 | + debug!("[*]: Directory exists {:?}", constructed_path); |
| 82 | + continue |
| 83 | + } |
| 84 | + }, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + debug!("[*]: Constructed path: {:?}", constructed_path); |
| 89 | + |
| 90 | + Ok(()) |
| 91 | +} |
| 92 | + |
| 93 | +/// This function creates a file and returns it. But before creating the file it checks every |
| 94 | +/// directory in the path to see if it exists, and if it does not it will be created. |
| 95 | +
|
| 96 | +pub fn create_file(path: &Path) -> Result<File, Box<Error>> { |
| 97 | + debug!("[fn]: create_file"); |
| 98 | + |
| 99 | + // Construct path |
| 100 | + if let Some(p) = path.parent() { |
| 101 | + try!(create_path(p)); |
| 102 | + } |
| 103 | + |
| 104 | + debug!("[*]: Create file: {}", path); |
| 105 | + let f = try!(File::create(path)); |
| 106 | + |
| 107 | + Ok(f) |
| 108 | +} |
0 commit comments