1use serde::{Deserialize, Serialize};
2
3use crate::Result;
4
5use super::request;
6
7#[derive(Debug, Serialize, Deserialize)]
11#[serde(tag = "type")]
12#[serde(rename_all = "lowercase")]
13pub enum UiComponent {
14 Panel { children: Vec<UiComponent> },
15 Copyable { value: String },
16 Divider,
17 Heading { value: String },
18 Spinner,
19 Text { value: String },
20}
21
22pub mod ui {
24 use super::UiComponent;
25
26 pub fn panel<T>(children: T) -> UiComponent
30 where
31 T: Into<Vec<UiComponent>>,
32 {
33 UiComponent::Panel {
34 children: children.into(),
35 }
36 }
37
38 pub fn copyable<T>(value: T) -> UiComponent
42 where
43 T: Into<String>,
44 {
45 UiComponent::Copyable {
46 value: value.into(),
47 }
48 }
49
50 pub fn divider() -> UiComponent {
54 UiComponent::Divider
55 }
56
57 pub fn heading<T>(value: T) -> UiComponent
61 where
62 T: Into<String>,
63 {
64 UiComponent::Heading {
65 value: value.into(),
66 }
67 }
68
69 pub fn spinner() -> UiComponent {
73 UiComponent::Spinner
74 }
75
76 pub fn text<T>(value: T) -> UiComponent
80 where
81 T: Into<String>,
82 {
83 UiComponent::Text {
84 value: value.into(),
85 }
86 }
87}
88
89#[derive(Debug, Serialize, Deserialize)]
90struct DialogParams {
91 #[serde(rename = "type")]
92 ty: &'static str,
93 content: UiComponent,
94 #[serde(skip_serializing_if = "Option::is_none")]
95 placeholder: Option<String>,
96}
97
98pub async fn alert(content: UiComponent) -> Result<()> {
102 let req = DialogParams {
103 ty: "alert",
104 content,
105 placeholder: None,
106 };
107
108 request("snap_dialog", req).await?;
109
110 Ok(())
111}
112
113pub async fn confirm(content: UiComponent) -> Result<bool> {
117 let req = DialogParams {
118 ty: "confirmation",
119 content,
120 placeholder: None,
121 };
122
123 Ok(request("snap_dialog", req).await?)
124}
125
126pub async fn prompt(content: UiComponent, placeholder: &str) -> Result<Option<String>> {
130 let req = DialogParams {
131 ty: "prompt",
132 content,
133 placeholder: Some(String::from(placeholder)),
134 };
135
136 Ok(request("snap_dialog", req).await?)
137}