tauri_interop/
event.rs

1#[cfg(any(feature = "initial_value", doc))]
2use serde::Deserialize;
3use serde::{de::DeserializeOwned, Serialize};
4#[cfg(not(target_family = "wasm"))]
5use tauri::{AppHandle, Error, Wry};
6
7#[cfg(not(target_family = "wasm"))]
8#[doc(cfg(not(target_family = "wasm")))]
9pub use emit::*;
10#[cfg(any(target_family = "wasm", doc))]
11#[doc(cfg(target_family = "wasm"))]
12pub use listen::*;
13#[cfg(doc)]
14use tauri_interop_macro::{Emit, EmitField, Event, Listen, ListenField};
15
16/// traits for event emitting in the host code
17#[cfg(not(target_family = "wasm"))]
18#[doc(cfg(not(target_family = "wasm")))]
19mod emit;
20
21/// related generic struct and functions for autogenerated listen functions
22#[cfg(any(target_family = "wasm", doc))]
23#[doc(cfg(target_family = "wasm"))]
24mod listen;
25
26#[allow(clippy::needless_doctest_main)]
27/// Trait defining a [Field] to a related struct implementing [Parent] with the related [Field::Type]
28///
29/// When using [Event], [Emit] or [Listen], for each field of the struct, a struct named after the
30/// field is generated. The field naming is snake_case to PascalCase, but because of the possibility
31/// that the type and the field name are the same, the generated field has a "F" appended at the
32/// beginning to separate each other and avoid type collision.
33///
34/// ```
35/// use serde::{Deserialize, Serialize};
36/// use tauri_interop::Event;
37///
38/// #[derive(Default, Clone, Serialize, Deserialize)]
39/// struct Bar {
40///     foo: u16
41/// }
42///
43/// #[derive(Event)]
44/// struct Test {
45///     bar: Bar
46/// }
47///
48/// #[cfg(feature = "initial_value")]
49/// impl tauri_interop::event::ManagedEmit for Test {}
50///
51/// fn main() {
52///     let _ = test::FBar;
53/// }
54/// ```
55pub trait Field<P>
56where
57    P: Parent,
58    Self::Type: Default + Clone + Serialize + DeserializeOwned + 'static,
59{
60    /// The type of the field
61    type Type;
62
63    /// The event of the field
64    const EVENT_NAME: &'static str;
65
66    /// Tries to retrieve the current value from the backend
67    #[allow(async_fn_in_trait)]
68    #[cfg(any(all(target_family = "wasm", feature = "initial_value"), doc))]
69    #[doc(cfg(all(target_family = "wasm", feature = "initial_value")))]
70    async fn get_value() -> Result<Self::Type, EventError>;
71
72    #[cfg(not(target_family = "wasm"))]
73    #[doc(cfg(not(target_family = "wasm")))]
74    /// Emits event of the related field with their value
75    ///
76    /// not in wasm available
77    fn emit(parent: &P, handle: &AppHandle<Wry>) -> Result<(), Error>;
78
79    #[cfg(not(target_family = "wasm"))]
80    #[doc(cfg(not(target_family = "wasm")))]
81    /// Updates the related field and emit its event
82    ///
83    /// not in wasm available
84    fn update(s: &mut P, handle: &AppHandle<Wry>, v: Self::Type) -> Result<(), Error>;
85}
86
87#[cfg(any(feature = "initial_value", doc))]
88#[doc(cfg(feature = "initial_value"))]
89/// General errors that can happen during event exchange
90#[derive(Debug, Serialize, Deserialize, thiserror::Error)]
91pub enum EventError {
92    /// The given name (struct) is not as tauri::State registered
93    #[error("{0} is not as tauri state registered")]
94    StateIsNotRegistered(String),
95}