added rust files

This commit is contained in:
Fractal-Tess
2023-05-18 06:51:16 +03:00
parent b49cc62b62
commit 2954bce715
6 changed files with 230 additions and 74 deletions

34
src-tauri/src/commands.rs Normal file
View File

@@ -0,0 +1,34 @@
use sha2::{Digest, Sha256};
use specta::collect_types;
use tauri::{Builder, Wry};
use tauri_specta::ts;
// Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers.
pub fn register_commands(builder: Builder<Wry>) -> Builder<Wry> {
// Specta generating typed binding interfaces
#[cfg(debug_assertions)]
ts::export(
collect_types![hello_tauri, hash256sum],
"../src/lib/bindings.ts",
)
.expect("unable to generate specta types");
builder.invoke_handler(tauri::generate_handler![hash256sum, hello_tauri])
}
// An example command
#[tauri::command]
#[specta::specta]
fn hello_tauri() -> String {
"Hi from Tauri".to_owned()
}
// Another command
#[tauri::command]
#[specta::specta]
fn hash256sum(hash_input: String) -> String {
let mut hasher = Sha256::new();
hasher.update(hash_input.as_bytes());
let result = hasher.finalize();
format!("{:X}", result)
}

10
src-tauri/src/error.rs Normal file
View File

@@ -0,0 +1,10 @@
#![allow(unused)]
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("{0}")]
Other(String),
#[error(transparent)]
IO(#[from] std::io::Error),
}

View File

@@ -3,13 +3,19 @@
windows_subsystem = "windows"
)]
use sha2::{Digest, Sha256};
use commands::register_commands;
use prelude::*;
use tauri::RunEvent;
mod commands;
mod error;
mod prelude;
fn main() {
let app = tauri::Builder::default()
.plugin(tauri_plugin_window_state::Builder::default().build())
.invoke_handler(tauri::generate_handler![called_from_js, hash256sum])
let app =
tauri::Builder::default().plugin(tauri_plugin_window_state::Builder::default().build());
let app = register_commands(app)
.build(tauri::generate_context!())
.expect("error while running tauri application");
@@ -20,18 +26,3 @@ fn main() {
_ => {}
})
}
#[tauri::command]
fn called_from_js() -> String {
// The print macro is problematic in release environment (crashes the application if not ran from a terminal)
// println!("Returning from tauri");
"Hi from Tauri".to_owned()
}
#[tauri::command]
fn hash256sum(hash_input: String) -> String {
let mut hasher = Sha256::new();
hasher.update(hash_input.as_bytes());
let result = hasher.finalize();
format!("{:X}", result)
}

6
src-tauri/src/prelude.rs Normal file
View File

@@ -0,0 +1,6 @@
#![allow(unused)]
pub use crate::error::Error;
pub type Result<T> = core::result::Result<T, Error>;
pub struct W<T>(pub T);