tauri_plugin_dialog/
models.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use serde::{Deserialize, Deserializer, Serialize, Serializer};
6
7/// Types of message, ask and confirm dialogs.
8#[non_exhaustive]
9#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
10pub enum MessageDialogKind {
11    /// Information dialog.
12    Info,
13    /// Warning dialog.
14    Warning,
15    /// Error dialog.
16    Error,
17}
18
19impl Default for MessageDialogKind {
20    fn default() -> Self {
21        Self::Info
22    }
23}
24
25impl<'de> Deserialize<'de> for MessageDialogKind {
26    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
27    where
28        D: Deserializer<'de>,
29    {
30        let s = String::deserialize(deserializer)?;
31        Ok(match s.to_lowercase().as_str() {
32            "info" => MessageDialogKind::Info,
33            "warning" => MessageDialogKind::Warning,
34            "error" => MessageDialogKind::Error,
35            _ => MessageDialogKind::Info,
36        })
37    }
38}
39
40impl Serialize for MessageDialogKind {
41    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
42    where
43        S: Serializer,
44    {
45        match self {
46            Self::Info => serializer.serialize_str("info"),
47            Self::Warning => serializer.serialize_str("warning"),
48            Self::Error => serializer.serialize_str("error"),
49        }
50    }
51}
52
53/// Set of button that will be displayed on the dialog
54#[non_exhaustive]
55#[derive(Debug, Default, Clone)]
56pub enum MessageDialogButtons {
57    #[default]
58    /// A single `Ok` button with OS default dialog text
59    Ok,
60    /// 2 buttons `Ok` and `Cancel` with OS default dialog texts
61    OkCancel,
62    /// 2 buttons `Yes` and `No` with OS default dialog texts
63    YesNo,
64    /// A single `Ok` button with custom text
65    OkCustom(String),
66    /// 2 buttons `Ok` and `Cancel` with custom texts
67    OkCancelCustom(String, String),
68}