move in_dir into impl

master
Brian Hicks 2020-05-29 17:01:32 -05:00
parent 64abb2b46e
commit da496e7110
2 changed files with 15 additions and 15 deletions

View File

@ -1,8 +1,8 @@
use std::path::Path;
use zettel;
use zettel::*;
fn main() {
match zettel::in_dir(Path::new("demo")) {
match Zettel::in_dir(Path::new("demo")) {
Ok(zettels) => println!(
"{}",
zettels

View File

@ -11,19 +11,6 @@ pub struct Zettel {
path: std::path::PathBuf,
}
// TODO: this could maybe be an iterator?
pub fn in_dir(directory: &Path) -> Result<Vec<Zettel>> {
let mut out = Vec::new();
for entry in fs::read_dir(directory)? {
out.push(Zettel {
path: entry?.path(),
})
}
Ok(out)
}
impl fmt::Debug for Zettel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Zettel").field("path", &self.path).finish()
@ -31,6 +18,19 @@ impl fmt::Debug for Zettel {
}
impl Zettel {
// TODO: this could maybe be an iterator?
pub fn in_dir(directory: &Path) -> Result<Vec<Zettel>> {
let mut out = Vec::new();
for entry in fs::read_dir(directory)? {
out.push(Zettel {
path: entry?.path(),
})
}
Ok(out)
}
// TODO: is this the most efficient way to do this?
// TODO: this mixes the logic of reading a file and parsing the first line. Separate!
pub fn title(self: &Zettel) -> Result<String> {