2016-12-13 06:22:24 +00:00
|
|
|
use std::io::prelude::*;
|
2017-03-10 11:39:58 +00:00
|
|
|
use std::fs::{File, create_dir};
|
2016-12-13 06:22:24 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
use errors::{Result, ResultExt};
|
2016-12-13 06:22:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
pub fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
|
|
|
|
let mut file = File::create(&path)?;
|
|
|
|
file.write_all(content.as_bytes())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-10 11:39:58 +00:00
|
|
|
|
2017-03-10 13:19:36 +00:00
|
|
|
/// Very similar to `create_dir` from the std except it checks if the folder
|
2017-03-10 11:39:58 +00:00
|
|
|
/// exists before creating it
|
|
|
|
pub fn create_directory<P: AsRef<Path>>(path: P) -> Result<()> {
|
|
|
|
let path = path.as_ref();
|
|
|
|
if !path.exists() {
|
|
|
|
create_dir(path)
|
|
|
|
.chain_err(|| format!("Was not able to create folder {}", path.display()))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-14 12:25:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Return the content of a file, with error handling added
|
|
|
|
pub fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
|
|
|
|
let path = path.as_ref();
|
|
|
|
|
|
|
|
let mut content = String::new();
|
|
|
|
File::open(path)
|
|
|
|
.chain_err(|| format!("Failed to open '{:?}'", path.display()))?
|
|
|
|
.read_to_string(&mut content)?;
|
|
|
|
|
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Takes a full path to a .md and returns only the components after the `content` directory
|
|
|
|
/// Will not return the filename as last component
|
|
|
|
pub fn find_content_components<P: AsRef<Path>>(path: P) -> Vec<String> {
|
|
|
|
let path = path.as_ref();
|
|
|
|
let mut is_in_content = false;
|
|
|
|
let mut components = vec![];
|
|
|
|
|
|
|
|
for section in path.parent().unwrap().components() {
|
|
|
|
let component = section.as_ref().to_string_lossy();
|
|
|
|
|
|
|
|
if is_in_content {
|
|
|
|
components.push(component.to_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if component == "content" {
|
|
|
|
is_in_content = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
components
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::{find_content_components};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_content_components() {
|
|
|
|
let res = find_content_components("/home/vincent/code/site/content/posts/tutorials/python.md");
|
|
|
|
assert_eq!(res, ["posts".to_string(), "tutorials".to_string()]);
|
|
|
|
}
|
|
|
|
}
|