add staged in status
This commit is contained in:
27
src/utils/index.rs
Normal file
27
src/utils/index.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::fs::OpenOptions;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
use crate::utils::read;
|
||||
use std::io;
|
||||
|
||||
pub fn read_only(mut path: PathBuf) -> File {
|
||||
path.push("index");
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.open(path).expect("Cannot open index file")
|
||||
}
|
||||
|
||||
pub fn open(mut path: PathBuf) -> File {
|
||||
path.push("index");
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(path).expect("Cannot open index file")
|
||||
}
|
||||
|
||||
pub fn read_line(mut path: PathBuf) -> io::Result<io::Lines<io::BufReader<File>>> {
|
||||
path.push("index");
|
||||
read::read_lines(path)
|
||||
}
|
||||
20
src/utils/read.rs
Normal file
20
src/utils/read.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::io::{self, BufRead};
|
||||
use std::fs::{self, File};
|
||||
|
||||
// The output is wrapped in a Result to allow matching on errors
|
||||
// Returns an Iterator to the Reader of the lines of the file.
|
||||
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
|
||||
pub fn read_folder(path: PathBuf) -> io::Result<Vec<PathBuf>> {
|
||||
let mut entries = fs::read_dir(path)?
|
||||
.map(|res| res.map(|e| e.path()))
|
||||
.collect::<Result<Vec<_>, io::Error>>()?;
|
||||
|
||||
entries.sort();
|
||||
Ok(entries)
|
||||
}
|
||||
Reference in New Issue
Block a user