tauri-react 0.1.0

Helper libraries (Rust and Typescript/Javascript) for tauri with react frontend
Documentation
/*
 * File: command.rs
 * Author: MarkAtk
 * Date: 09.12.20
 *
 * MIT License
 *
 * Copyright (c) 2020 MarkAtk
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

use std::sync::MutexGuard;
use std::marker::{Sync, Send};
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
#[serde(tag = "cmd", rename_all = "camelCase")]
pub enum Cmd {
    InitializeStore { callback: String, error: String },
    Action { action: String, data: serde_json::Value, callback: String, error: String }
}

pub trait ActionCallback<T: ApplicationState>: Fn(T, serde_json::Value) -> tauri::Result<T> + Send + Sync + 'static {}

#[derive(Debug, Clone, Serialize)]
pub struct CommandError {
    message: String
}

impl CommandError {
    pub fn new(message: String) -> Self {
        Self {
            message
        }
    }
}

impl std::fmt::Display for CommandError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl std::error::Error for CommandError {}

pub trait ApplicationState: Serialize + Clone + Default + Send + std::fmt::Display {}

pub trait StoreState<T: ApplicationState>: Send + Sync {
    fn get_data(&self) -> tauri::Result<MutexGuard<T>>;
}