]> git.proxmox.com Git - ui/proxmox-yew-widget-toolkit.git/commitdiff
move SubmitCallback to props folder
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 2 Oct 2024 07:20:13 +0000 (09:20 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 2 Oct 2024 07:20:53 +0000 (09:20 +0200)
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
src/props/mod.rs
src/props/submit_callback.rs [new file with mode: 0644]
src/widget/form/mod.rs
src/widget/form/submit_callback.rs [deleted file]

index 3c5cc9aa5b2c06e87d7ea7c9528f3b5db898c03a..52ed1151ea05f4fd7eb976b41109e852c9694dab 100644 (file)
@@ -114,6 +114,9 @@ pub use extract_key_function::{ExtractKeyFn, ExtractPrimaryKey, IntoExtractKeyFn
 mod load_callback;
 pub use load_callback::{set_http_get_method, IntoLoadCallback, LoadCallback};
 
+mod submit_callback;
+pub use submit_callback::{IntoSubmitCallback, SubmitCallback};
+
 mod field_std_props;
 pub use field_std_props::FieldStdProps;
 
diff --git a/src/props/submit_callback.rs b/src/props/submit_callback.rs
new file mode 100644 (file)
index 0000000..474c903
--- /dev/null
@@ -0,0 +1,54 @@
+use std::future::Future;
+use std::pin::Pin;
+use std::rc::Rc;
+
+use anyhow::Error;
+use derivative::Derivative;
+
+/// A [SubmitCallback] is an async callback ([Future]) that gets the
+/// data to be submitted as parameter, returning the [Result] of the submit
+/// operation.
+#[derive(Derivative)]
+#[derivative(Clone, PartialEq)]
+pub struct SubmitCallback<T>(
+    #[derivative(PartialEq(compare_with = "Rc::ptr_eq"))]
+    Rc<dyn Fn(T) -> Pin<Box<dyn Future<Output = Result<(), Error>>>>>,
+);
+
+impl<T> SubmitCallback<T> {
+    pub fn new<F, R>(callback: F) -> Self
+    where
+        F: 'static + Fn(T) -> R,
+        R: 'static + Future<Output = Result<(), Error>>,
+    {
+        Self(Rc::new(move |state: T| {
+            let future = callback(state);
+            Box::pin(future)
+        }))
+    }
+
+    pub async fn apply(&self, form_ctx: T) -> Result<(), Error> {
+        (self.0)(form_ctx).await
+    }
+}
+
+/// Helper trait to create an optional [SubmitCallback] property.
+pub trait IntoSubmitCallback<T> {
+    fn into_submit_callback(self) -> Option<SubmitCallback<T>>;
+}
+
+impl<T> IntoSubmitCallback<T> for Option<SubmitCallback<T>> {
+    fn into_submit_callback(self) -> Option<SubmitCallback<T>> {
+        self
+    }
+}
+
+impl<F, R, T> IntoSubmitCallback<T> for F
+where
+    F: 'static + Fn(T) -> R,
+    R: 'static + Future<Output = Result<(), Error>>,
+{
+    fn into_submit_callback(self) -> Option<SubmitCallback<T>> {
+        Some(SubmitCallback::new(self))
+    }
+}
index 3b29db0c2834d69c1e3975e935f39309286eb41f..67f3905224db6da6d77fedc3bd1961e834665a0e 100644 (file)
@@ -44,9 +44,6 @@ pub use reset_button::{PwtResetButton, ResetButton};
 mod selector;
 pub use selector::{PwtSelector, Selector, SelectorRenderArgs};
 
-mod submit_callback;
-pub use submit_callback::{IntoSubmitCallback, SubmitCallback};
-
 mod submit_button;
 pub use submit_button::{PwtSubmitButton, SubmitButton};
 
diff --git a/src/widget/form/submit_callback.rs b/src/widget/form/submit_callback.rs
deleted file mode 100644 (file)
index 474c903..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-use std::future::Future;
-use std::pin::Pin;
-use std::rc::Rc;
-
-use anyhow::Error;
-use derivative::Derivative;
-
-/// A [SubmitCallback] is an async callback ([Future]) that gets the
-/// data to be submitted as parameter, returning the [Result] of the submit
-/// operation.
-#[derive(Derivative)]
-#[derivative(Clone, PartialEq)]
-pub struct SubmitCallback<T>(
-    #[derivative(PartialEq(compare_with = "Rc::ptr_eq"))]
-    Rc<dyn Fn(T) -> Pin<Box<dyn Future<Output = Result<(), Error>>>>>,
-);
-
-impl<T> SubmitCallback<T> {
-    pub fn new<F, R>(callback: F) -> Self
-    where
-        F: 'static + Fn(T) -> R,
-        R: 'static + Future<Output = Result<(), Error>>,
-    {
-        Self(Rc::new(move |state: T| {
-            let future = callback(state);
-            Box::pin(future)
-        }))
-    }
-
-    pub async fn apply(&self, form_ctx: T) -> Result<(), Error> {
-        (self.0)(form_ctx).await
-    }
-}
-
-/// Helper trait to create an optional [SubmitCallback] property.
-pub trait IntoSubmitCallback<T> {
-    fn into_submit_callback(self) -> Option<SubmitCallback<T>>;
-}
-
-impl<T> IntoSubmitCallback<T> for Option<SubmitCallback<T>> {
-    fn into_submit_callback(self) -> Option<SubmitCallback<T>> {
-        self
-    }
-}
-
-impl<F, R, T> IntoSubmitCallback<T> for F
-where
-    F: 'static + Fn(T) -> R,
-    R: 'static + Future<Output = Result<(), Error>>,
-{
-    fn into_submit_callback(self) -> Option<SubmitCallback<T>> {
-        Some(SubmitCallback::new(self))
-    }
-}