-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from falk-werner/feature/note.rs
Feature: note.rs
- Loading branch information
Showing
3 changed files
with
70 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::{fs::DirEntry, io::{Error, Result}, path::{Path, PathBuf}}; | ||
use crate::config::{Config}; | ||
|
||
pub struct Note { | ||
config: Config | ||
} | ||
|
||
impl Note { | ||
|
||
/// Creates a new Note struct. | ||
pub fn new(config: Config) -> Self { | ||
Note { config } | ||
} | ||
|
||
|
||
/// list the names of the notes | ||
/// | ||
/// list all directories in the base directory that have a `README.md` file inside | ||
pub fn list(&self) -> Vec<String> { | ||
let base_path = self.config.get_base_path().join(".notes"); | ||
match get_note_names(base_path) { | ||
Ok(note_names) => return note_names, | ||
Err(e) => error_handling(e.to_string()) | ||
} | ||
Vec::<String>::new() | ||
} | ||
|
||
} | ||
|
||
/// list all directories in `base_path` that have a `README.md` file inside | ||
fn get_note_names(base_path: PathBuf) -> Result<Vec<String>> { | ||
let mut note_names = Vec::<String>::new(); | ||
let dir_entries = std::fs::read_dir(base_path)?; | ||
for dir_entry in dir_entries { | ||
match get_note_name(dir_entry) { | ||
Ok(note_name) => note_names.push(note_name), | ||
Err(e) => error_handling(e.to_string()) | ||
} | ||
} | ||
Ok(note_names) | ||
} | ||
|
||
/// check if the `dir_entry` contains a `README.md` file inside and returns the folder name if so | ||
fn get_note_name(dir_entry: Result<DirEntry>) -> Result<String> { | ||
let dir_entry = dir_entry?; | ||
if dir_entry.file_type()?.is_dir() && Path::new(&dir_entry.path()).join("README.md").is_file() { | ||
return Ok(dir_entry.file_name().into_string().unwrap()); | ||
} | ||
Err( | ||
Error::new( | ||
std::io::ErrorKind::NotFound, | ||
format!("README.md not found for `{}`.", dir_entry.path().to_str().unwrap()) | ||
) | ||
) | ||
} | ||
|
||
fn error_handling(e: String) { | ||
// implementation up to discussion | ||
eprintln!("Warning: {e}"); | ||
} | ||
|