This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
fs.rs
108 lines (93 loc) · 2.81 KB
/
fs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Some helpful functions to deal with file system operations.
//!
//! These are rather simple, but provide a quick and easy way to to common
//! tasks. Also, they have great error messages.
use std::path::{Path, PathBuf};
use std::io::{BufReader, BufWriter, Read, Write};
use std::fs::File;
use std::result::Result as StdResult;
use failure::{Error, ResultExt, ensure};
pub use std::fs::create_dir_all as create_dir;
pub use remove_dir_all::remove_dir_all;
/// Read file content into string
///
/// # Examples
///
/// ```rust
/// # use quicli::prelude::*;
/// # fn main() -> CliResult {
/// let x = read_file(".gitignore")?;
/// assert!(x.len() > 0);
/// # Ok(()) }
/// ```
pub fn read_file<P: AsRef<Path>>(path: P) -> Result<String, Error> {
let path = path.as_ref();
ensure!(
path.exists() && path.is_file(),
"Path {:?} is not a file!",
path
);
let file = File::open(path).with_context(|_| format!("Could not open file {:?}", path))?;
let mut file = BufReader::new(file);
let mut result = String::new();
file.read_to_string(&mut result)
.with_context(|_| format!("Could not read file {:?}", path))?;
Ok(result)
}
/// Write string to file
///
/// _Note:_ Replaces the current file content if the file already exists.
///
/// # Examples
///
/// ```rust
/// # use quicli::prelude::*;
/// # fn main() -> CliResult {
/// let dir = tempfile::tempdir()?;
/// let filepath = dir.path().join("asdasidz81zasda");
/// write_to_file(filepath, "foobar")?;
/// # Ok(()) }
/// ```
pub fn write_to_file<P: AsRef<Path>>(path: P, content: &str) -> Result<(), Error> {
let path = path.as_ref();
let file =
File::create(path).with_context(|_| format!("Could not create/open file {:?}", path))?;
let mut file = BufWriter::new(file);
file.write_all(content.as_bytes())
.with_context(|_| format!("Could not write to file {:?}", path))?;
Ok(())
}
/// Find files with glob pattern
///
/// Search for a pattern like `*.md` and get an iterator of Markdown files.
///
/// # Examples
///
/// ```rust
/// # use quicli::prelude::*;
/// # fn main() -> CliResult {
/// let markdown_files = glob("*.md")?;
/// assert_eq!(markdown_files.len(), 2);
///
/// let image_files = glob("**/*.{png,jpg,gif}");
/// assert!(image_files.is_err());
/// if let Err(e) = image_files {
/// assert_eq!(e.to_string(), "No files match pattern `**/*.{png,jpg,gif}`".to_string());
/// }
/// # Ok(()) }
/// ```
pub fn glob(patterns: &str) -> Result<Vec<PathBuf>, Error> {
use globwalk::glob;
let files: Vec<_> = glob(patterns)?
.max_depth(1)
.into_iter()
.filter_map(StdResult::ok)
.map(|dir_entry| dir_entry.path().to_owned())
.collect();
ensure!(
files.get(0).is_some(),
"No files match pattern `{}`",
patterns
);
Ok(files)
}