Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: TauriNoteProvider #17

Merged
merged 7 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 0 additions & 88 deletions frontend/fakenoteprovider.js

This file was deleted.

4 changes: 2 additions & 2 deletions frontend/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "lineicons/web-font/lineicons.css"
import { slider_attach } from "./slider.js"
import { init_titlebar } from "./titlebar.js"
import { init_settings } from "./settings.js"
import { FakeNoteProvider } from "./fakenoteprovider.js"
import { TauriNoteProvider } from "./taurinoteprovider.js"
import { NoteList } from "./notelist.js"
import { Editor } from "./editor.js"
import { TagList } from "./taglist.js"
Expand All @@ -30,7 +30,7 @@ document.querySelector("#toggle-mode").addEventListener("click", () => {
});


const noteProvider = new FakeNoteProvider();
const noteProvider = new TauriNoteProvider();

const taglist_elemnt = document.querySelector("#taglist");
const taglist = new TagList(taglist_elemnt, noteProvider);
Expand Down
47 changes: 47 additions & 0 deletions frontend/taurinoteprovider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { tauri } from "@tauri-apps/api";
import { NoteProvider } from "./noteprovider";

class TauriNoteProvider extends NoteProvider {
async list() {
return await tauri.invoke("list");
}

async read(name) {
return await tauri.invoke("read", { name: name });
}

async create() {
return await tauri.invoke("create");
}

async rename(old_name, new_name) {
return await tauri.invoke("rename", {
oldName: old_name,
newName: new_name
});
}

async write(name, content) {
await tauri.invoke("write", {
name: name,
content: content
});
}

async remove(name) {
await tauri.invoke("remove", { name: name});
}

async read_tags(name) {
return await tauri.invoke("read_tags", { name: name});
}

async write_tags(name, tags) {
await tauri.invoke("write_tags", {
name: name,
tags: tags
});
}
}

export { TauriNoteProvider }
26 changes: 5 additions & 21 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use home::{home_dir};

use std::fs;
use std::path::{Path, PathBuf};
use core::fmt::Display;

use crate::noteerror::{NoteError,NoteResult};

const DEFAULT_CONFIG_FILENAME: &str = ".noters.yaml";

Expand Down Expand Up @@ -32,23 +33,6 @@ fn get_home_dir() -> PathBuf {
home_dir().unwrap_or(PathBuf::from("."))
}

#[derive(Debug)]
struct ConfigError {
message: String
}

impl ConfigError {
fn new(message: &str) -> Self {
ConfigError { message: message.into() }
}
}

impl<E: Display> From<E> for ConfigError {
fn from(value: E) -> Self {
ConfigError { message: value.to_string() }
}
}

impl Config {

/// Creates a new config with default values.
Expand Down Expand Up @@ -83,7 +67,7 @@ impl Config {
match Config::from_file(filename.as_path()) {
Ok(config) => config,
Err(error) => {
eprintln!("warning: failed to load config {:?}: {}", filename, error.message);
eprintln!("warning: failed to load config {:?}: {}", filename, error.to_string());
let config = Config::new(filename.as_path());
config.save();
config
Expand All @@ -92,12 +76,12 @@ impl Config {
}

/// Try to load the config from a given path.
fn from_file(filename: &Path) -> Result<Self, ConfigError> {
fn from_file(filename: &Path) -> NoteResult<Self> {
let text = fs::read_to_string(filename)?;
let config_file = serde_yaml::from_str::<ConfigFile>(&text)?;

if config_file.meta.version != 1 {
return Err(ConfigError::new("unknown file format (version mismatch)"));
return Err(NoteError::new("unknown file format (version mismatch)"));
}

Ok(Config {
Expand Down
49 changes: 46 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

mod config;
mod note;
mod noteerror;

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

fn main() {
let config = Config::from_default_file();
Expand All @@ -14,7 +16,14 @@ fn main() {
tauri::Builder::default()
.manage(note)
.invoke_handler(tauri::generate_handler![
list
list,
read,
create,
rename,
write,
remove,
read_tags,
write_tags
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand All @@ -24,7 +33,41 @@ fn main() {
///
/// list all directories in the base directory that have a `README.md` file inside
#[tauri::command]
async fn list(note: tauri::State<'_, Note>) -> std::result::Result<Vec<String>, String> {
Ok(note.list())
async fn list(note: tauri::State<'_, Note>) -> NoteResult<Vec<String>> {
note.list()
}

#[tauri::command]
async fn read(note: tauri::State<'_, Note>, name: &str) -> NoteResult<String> {
note.read(name)
}

#[tauri::command]
async fn create(note: tauri::State<'_, Note>) -> NoteResult<String> {
note.create()
}

#[tauri::command]
async fn rename(note: tauri::State<'_, Note>, old_name: &str, new_name: &str) -> NoteResult<()> {
note.rename(old_name, new_name)
}

#[tauri::command]
async fn write(note: tauri::State<'_, Note>, name: &str, content: &str) -> NoteResult<()> {
note.write(name, content)
}

#[tauri::command]
async fn remove(note: tauri::State<'_, Note>, name: &str) -> NoteResult<()> {
note.remove(name)
}

#[tauri::command]
async fn read_tags(note: tauri::State<'_, Note>, name: &str) -> NoteResult<Vec<String>> {
note.read_tags(name)
}

#[tauri::command]
async fn write_tags(note: tauri::State<'_, Note>, name: &str, tags: Vec<String>) -> NoteResult<()> {
note.write_tags(name, &tags)
}
Loading