From 880eca30dab234e2498d90ac74c0098c8f8e1182 Mon Sep 17 00:00:00 2001 From: Maccesch Date: Sun, 16 Jul 2023 14:04:42 +0100 Subject: [PATCH] added is_ok and is_err --- CHANGELOG.md | 2 ++ docs/book/src/SUMMARY.md | 2 ++ docs/book/src/utilities/is_err.md | 3 +++ docs/book/src/utilities/is_ok.md | 3 +++ examples/Cargo.toml | 2 ++ src/is_err.rs | 27 +++++++++++++++++++++++++++ src/is_ok.rs | 27 +++++++++++++++++++++++++++ src/lib.rs | 4 ++++ 8 files changed, 70 insertions(+) create mode 100644 docs/book/src/utilities/is_err.md create mode 100644 docs/book/src/utilities/is_ok.md create mode 100644 src/is_err.rs create mode 100644 src/is_ok.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fef6d1..7b3bff0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### New Functions 🚀 +- `is_err` +- `is_ok` - `is_none` - `is_some` - `use_raf_fn` diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index ef6ee63..697f3de 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -63,7 +63,9 @@ # Utilities +- [is_err](utilities/is_err.md) - [is_none](utilities/is_none.md) +- [is_ok](utilities/is_ok.md) - [is_some](utilities/is_some.md) - [use_cycle_list](utilities/use_cycle_list.md) - [use_debounce_fn](utilities/use_debounce_fn.md) diff --git a/docs/book/src/utilities/is_err.md b/docs/book/src/utilities/is_err.md new file mode 100644 index 0000000..bdf2ae7 --- /dev/null +++ b/docs/book/src/utilities/is_err.md @@ -0,0 +1,3 @@ +# is_err + + diff --git a/docs/book/src/utilities/is_ok.md b/docs/book/src/utilities/is_ok.md new file mode 100644 index 0000000..6897289 --- /dev/null +++ b/docs/book/src/utilities/is_ok.md @@ -0,0 +1,3 @@ +# is_ok + + diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 8d1f86c..e664151 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -2,7 +2,9 @@ resolver = "2" members = [ + "is_err", "is_none", + "is_ok", "is_some", "on_click_outside", "use_abs", diff --git a/src/is_err.rs b/src/is_err.rs new file mode 100644 index 0000000..5698220 --- /dev/null +++ b/src/is_err.rs @@ -0,0 +1,27 @@ +use crate::utils::use_derive_signal; +use leptos::*; + +use_derive_signal!( + /// Reactive `Result::is_err()`. + /// + /// ## Usage + /// + /// ``` + /// # use leptos::*; + /// # use leptos_use::is_err; + /// # + /// # #[component] + /// # fn Demo(cx: Scope) -> impl IntoView { + /// let (example, set_example) = create_signal( + /// cx, + /// if js_sys::Math::random() < 0.5 { Ok("Example") } else { Err(()) } + /// ); + /// + /// let is_error = is_err(cx, example); + /// # + /// # view! { cx, } + /// # } + /// ``` + is_err, T: 'static> -> bool + |value| value.is_err() +); diff --git a/src/is_ok.rs b/src/is_ok.rs new file mode 100644 index 0000000..9be36cc --- /dev/null +++ b/src/is_ok.rs @@ -0,0 +1,27 @@ +use crate::utils::use_derive_signal; +use leptos::*; + +use_derive_signal!( + /// Reactive `Result::is_ok()`. + /// + /// ## Usage + /// + /// ``` + /// # use leptos::*; + /// # use leptos_use::is_ok; + /// # + /// # #[component] + /// # fn Demo(cx: Scope) -> impl IntoView { + /// let (example, set_example) = create_signal( + /// cx, + /// if js_sys::Math::random() < 0.5 { Ok("Example") } else { Err(()) } + /// ); + /// + /// let is_ok = is_ok(cx, example); + /// # + /// # view! { cx, } + /// # } + /// ``` + is_ok, T: 'static> -> bool + |value| value.is_ok() +); diff --git a/src/lib.rs b/src/lib.rs index b2c6ead..9086995 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,8 @@ cfg_if! { if #[cfg(web_sys_unstable_apis)] { }} mod on_click_outside; +mod is_err; +mod is_ok; mod is_none; mod is_some; mod use_active_element; @@ -58,6 +60,8 @@ mod watch_throttled; mod whenever; pub use on_click_outside::*; +pub use is_err::*; +pub use is_ok::*; pub use is_none::*; pub use is_some::*; pub use use_active_element::*;