Skip to content

Commit

Permalink
Merge pull request #16 from falk-werner/feature/note.rs
Browse files Browse the repository at this point in the history
Feature: note.rs
  • Loading branch information
falk-werner authored Jan 28, 2024
2 parents 30ccf6c + c7fb5d9 commit 1c0dec6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Config {

Ok(Config {
filename: filename.into(),
config_file: config_file
config_file
})
}

Expand All @@ -122,7 +122,7 @@ impl Config {
fn save(&self) {
let text = serde_yaml::to_string(&self.config_file).unwrap();
if let Err(error) = fs::write(self.filename.as_ref(), text.as_bytes()) {
eprintln!("error: failed to save config: {:?}: {}", self.filename, error.to_string());
eprintln!("error: failed to save config: {:?}: {}", self.filename, error);
}
else {
eprintln!("info: saved config: {:?}", self.filename);
Expand Down
48 changes: 7 additions & 41 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::{fs::DirEntry, io::{Error, Result}, path::{Path, PathBuf}};

mod config;
mod note;

use config::{Config};
use note::{Note};

fn main() {
let config = Config::from_default_file();
let note = Note::new(config);

tauri::Builder::default()
.manage(config)
.manage(note)
.invoke_handler(tauri::generate_handler![
list
])
Expand All @@ -22,43 +24,7 @@ fn main() {
///
/// list all directories in the base directory that have a `README.md` file inside
#[tauri::command]
async fn list(config: tauri::State<'_, Config>) -> std::result::Result<Vec<String>, String> {
let base_path = config.get_base_path().join(".notes");
match get_note_names(base_path) {
Ok(note_names) => return Ok(note_names),
Err(e) => error_handling(e.to_string())
}
Ok(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)
async fn list(note: tauri::State<'_, Note>) -> std::result::Result<Vec<String>, String> {
Ok(note.list())
}

/// 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}");
}
61 changes: 61 additions & 0 deletions src-tauri/src/note.rs
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}");
}

0 comments on commit 1c0dec6

Please sign in to comment.