You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fs.rs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. use std::io::prelude::*;
  2. use std::fs::{File, create_dir_all, read_dir};
  3. use std::path::{Path, PathBuf};
  4. use errors::{Result, ResultExt};
  5. /// Create a file with the content given
  6. pub fn create_file(path: &Path, content: &str) -> Result<()> {
  7. let mut file = File::create(&path)?;
  8. file.write_all(content.as_bytes())?;
  9. Ok(())
  10. }
  11. /// Create a directory at the given path if it doesn't exist already
  12. pub fn ensure_directory_exists(path: &Path) -> Result<()> {
  13. if !path.exists() {
  14. create_directory(path)?;
  15. }
  16. Ok(())
  17. }
  18. /// Very similar to `create_dir` from the std except it checks if the folder
  19. /// exists before creating it
  20. pub fn create_directory(path: &Path) -> Result<()> {
  21. if !path.exists() {
  22. create_dir_all(path)
  23. .chain_err(|| format!("Was not able to create folder {}", path.display()))?;
  24. }
  25. Ok(())
  26. }
  27. /// Return the content of a file, with error handling added
  28. pub fn read_file(path: &Path) -> Result<String> {
  29. let mut content = String::new();
  30. File::open(path)
  31. .chain_err(|| format!("Failed to open '{:?}'", path.display()))?
  32. .read_to_string(&mut content)?;
  33. Ok(content)
  34. }
  35. /// Looks into the current folder for the path and see if there's anything that is not a .md
  36. /// file. Those will be copied next to the rendered .html file
  37. pub fn find_related_assets(path: &Path) -> Vec<PathBuf> {
  38. let mut assets = vec![];
  39. for entry in read_dir(path).unwrap().filter_map(|e| e.ok()) {
  40. let entry_path = entry.path();
  41. if entry_path.is_file() {
  42. match entry_path.extension() {
  43. Some(e) => match e.to_str() {
  44. Some("md") => continue,
  45. _ => assets.push(entry_path.to_path_buf()),
  46. },
  47. None => continue,
  48. }
  49. }
  50. }
  51. assets
  52. }
  53. #[cfg(test)]
  54. mod tests {
  55. use std::fs::File;
  56. use tempdir::TempDir;
  57. use super::{find_related_assets};
  58. #[test]
  59. fn can_find_related_assets() {
  60. let tmp_dir = TempDir::new("example").expect("create temp dir");
  61. File::create(tmp_dir.path().join("index.md")).unwrap();
  62. File::create(tmp_dir.path().join("example.js")).unwrap();
  63. File::create(tmp_dir.path().join("graph.jpg")).unwrap();
  64. File::create(tmp_dir.path().join("fail.png")).unwrap();
  65. let assets = find_related_assets(tmp_dir.path());
  66. assert_eq!(assets.len(), 3);
  67. assert_eq!(assets.iter().filter(|p| p.extension().unwrap() != "md").count(), 3);
  68. assert_eq!(assets.iter().filter(|p| p.file_name().unwrap() == "example.js").count(), 1);
  69. assert_eq!(assets.iter().filter(|p| p.file_name().unwrap() == "graph.jpg").count(), 1);
  70. assert_eq!(assets.iter().filter(|p| p.file_name().unwrap() == "fail.png").count(), 1);
  71. }
  72. }