tauri_react/command.rs
1/*
2 * File: command.rs
3 * Author: MarkAtk
4 * Date: 09.12.20
5 *
6 * MIT License
7 *
8 * Copyright (c) 2020 MarkAtk
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in all
18 * copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29use std::sync::MutexGuard;
30use std::marker::{Sync, Send};
31use serde::{Deserialize, Serialize};
32
33#[derive(Deserialize)]
34#[serde(tag = "cmd", rename_all = "camelCase")]
35pub enum Cmd {
36 InitializeStore { callback: String, error: String },
37 Action { action: String, data: serde_json::Value, callback: String, error: String }
38}
39
40pub trait ActionCallback<T: ApplicationState>: Fn(T, serde_json::Value) -> tauri::Result<T> + Send + Sync + 'static {}
41
42#[derive(Debug, Clone, Serialize)]
43pub struct CommandError {
44 message: String
45}
46
47impl CommandError {
48 pub fn new(message: String) -> Self {
49 Self {
50 message
51 }
52 }
53}
54
55impl std::fmt::Display for CommandError {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "{}", self.message)
58 }
59}
60
61impl std::error::Error for CommandError {}
62
63pub trait ApplicationState: Serialize + Clone + Default + Send + std::fmt::Display {}
64
65pub trait StoreState<T: ApplicationState>: Send + Sync {
66 fn get_data(&self) -> tauri::Result<MutexGuard<T>>;
67}