2021-05-03 19:16:34 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
#![cfg_attr(
|
|
|
|
all(not(debug_assertions), target_os = "windows"),
|
|
|
|
windows_subsystem = "windows"
|
|
|
|
)]
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc, Mutex,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
use tauri::State;
|
|
|
|
|
|
|
|
struct Counter(AtomicUsize);
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Database(Arc<Mutex<HashMap<String, String>>>);
|
|
|
|
|
2021-05-20 16:40:18 +03:00
|
|
|
struct Client;
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
fn send(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Connection(Mutex<Option<Client>>);
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
fn connect(connection: State<Connection>) {
|
|
|
|
*connection.0.lock().unwrap() = Some(Client {});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
fn disconnect(connection: State<Connection>) {
|
|
|
|
// drop the connection
|
|
|
|
*connection.0.lock().unwrap() = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
fn connection_send(connection: State<Connection>) {
|
|
|
|
connection
|
|
|
|
.0
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
|
|
|
.expect("connection not initialize; use the `connect` command first")
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:16:34 +03:00
|
|
|
#[tauri::command]
|
2021-05-14 16:29:54 +03:00
|
|
|
fn increment_counter(counter: State<Counter>) -> usize {
|
2021-05-03 19:16:34 +03:00
|
|
|
counter.0.fetch_add(1, Ordering::Relaxed) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
2021-05-14 16:29:54 +03:00
|
|
|
fn db_insert(key: String, value: String, db: State<Database>) {
|
2021-05-03 19:16:34 +03:00
|
|
|
db.0.lock().unwrap().insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
2021-05-14 16:29:54 +03:00
|
|
|
fn db_read(key: String, db: State<Database>) -> Option<String> {
|
2021-05-03 19:16:34 +03:00
|
|
|
db.0.lock().unwrap().get(&key).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
tauri::Builder::default()
|
|
|
|
.manage(Counter(AtomicUsize::new(0)))
|
|
|
|
.manage(Database(Default::default()))
|
2021-05-20 16:40:18 +03:00
|
|
|
.manage(Connection(Default::default()))
|
2021-05-03 19:16:34 +03:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
increment_counter,
|
|
|
|
db_insert,
|
2021-05-20 16:40:18 +03:00
|
|
|
db_read,
|
|
|
|
connect,
|
|
|
|
disconnect,
|
|
|
|
connection_send
|
2021-05-03 19:16:34 +03:00
|
|
|
])
|
|
|
|
.run(tauri::generate_context!())
|
|
|
|
.expect("error while running tauri application");
|
|
|
|
}
|