mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 09:09:21 -05:00
Problem: use_storage defaults to StringCodec. Allow turbo fish usage and rely on Default
This commit is contained in:
parent
bbebd8a67f
commit
d56c5cf514
4 changed files with 40 additions and 12 deletions
|
@ -1,6 +1,6 @@
|
||||||
use super::{Codec, UseStorageOptions};
|
use super::{Codec, UseStorageOptions};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, Default, PartialEq)]
|
||||||
pub struct JsonCodec();
|
pub struct JsonCodec();
|
||||||
|
|
||||||
impl<T: serde::Serialize + serde::de::DeserializeOwned> Codec<T> for JsonCodec {
|
impl<T: serde::Serialize + serde::de::DeserializeOwned> Codec<T> for JsonCodec {
|
||||||
|
|
|
@ -2,7 +2,7 @@ use super::{Codec, UseStorageOptions};
|
||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, Default, PartialEq)]
|
||||||
pub struct ProstCodec();
|
pub struct ProstCodec();
|
||||||
|
|
||||||
#[derive(Error, Debug, PartialEq)]
|
#[derive(Error, Debug, PartialEq)]
|
||||||
|
|
|
@ -4,4 +4,9 @@ mod codec_json;
|
||||||
mod codec_prost;
|
mod codec_prost;
|
||||||
mod use_storage;
|
mod use_storage;
|
||||||
|
|
||||||
|
pub use crate::core::StorageType;
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
pub use codec_json::*;
|
||||||
|
#[cfg(feature = "prost")]
|
||||||
|
pub use codec_prost::*;
|
||||||
pub use use_storage::*;
|
pub use use_storage::*;
|
||||||
|
|
|
@ -34,17 +34,22 @@ pub enum UseStorageError<Err> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hook for using local storage. Returns a result of a signal and a setter / deleter.
|
/// Hook for using local storage. Returns a result of a signal and a setter / deleter.
|
||||||
pub fn use_local_storage<T>(key: impl AsRef<str>) -> (Memo<T>, impl Fn(Option<T>) -> ())
|
pub fn use_local_storage<T, C>(key: impl AsRef<str>) -> (Memo<T>, impl Fn(Option<T>) -> () + Clone)
|
||||||
where
|
where
|
||||||
T: Clone + Default + FromStr + PartialEq + ToString,
|
T: Clone + Default + PartialEq,
|
||||||
|
C: Codec<T> + Default,
|
||||||
{
|
{
|
||||||
use_storage_with_options(StorageType::Local, key, UseStorageOptions::string_codec())
|
use_storage_with_options(
|
||||||
|
StorageType::Local,
|
||||||
|
key,
|
||||||
|
UseStorageOptions::<T, C>::default(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_local_storage_with_options<T, C>(
|
pub fn use_local_storage_with_options<T, C>(
|
||||||
key: impl AsRef<str>,
|
key: impl AsRef<str>,
|
||||||
options: UseStorageOptions<T, C>,
|
options: UseStorageOptions<T, C>,
|
||||||
) -> (Memo<T>, impl Fn(Option<T>) -> ())
|
) -> (Memo<T>, impl Fn(Option<T>) -> () + Clone)
|
||||||
where
|
where
|
||||||
T: Clone + PartialEq,
|
T: Clone + PartialEq,
|
||||||
C: Codec<T>,
|
C: Codec<T>,
|
||||||
|
@ -53,17 +58,24 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hook for using session storage. Returns a result of a signal and a setter / deleter.
|
/// Hook for using session storage. Returns a result of a signal and a setter / deleter.
|
||||||
pub fn use_session_storage<T>(key: impl AsRef<str>) -> (Memo<T>, impl Fn(Option<T>) -> ())
|
pub fn use_session_storage<T, C>(
|
||||||
|
key: impl AsRef<str>,
|
||||||
|
) -> (Memo<T>, impl Fn(Option<T>) -> () + Clone)
|
||||||
where
|
where
|
||||||
T: Clone + Default + FromStr + PartialEq + ToString,
|
T: Clone + Default + PartialEq,
|
||||||
|
C: Codec<T> + Default,
|
||||||
{
|
{
|
||||||
use_storage_with_options(StorageType::Session, key, UseStorageOptions::string_codec())
|
use_storage_with_options(
|
||||||
|
StorageType::Session,
|
||||||
|
key,
|
||||||
|
UseStorageOptions::<T, C>::default(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_session_storage_with_options<T, C>(
|
pub fn use_session_storage_with_options<T, C>(
|
||||||
key: impl AsRef<str>,
|
key: impl AsRef<str>,
|
||||||
options: UseStorageOptions<T, C>,
|
options: UseStorageOptions<T, C>,
|
||||||
) -> (Memo<T>, impl Fn(Option<T>) -> ())
|
) -> (Memo<T>, impl Fn(Option<T>) -> () + Clone)
|
||||||
where
|
where
|
||||||
T: Clone + PartialEq,
|
T: Clone + PartialEq,
|
||||||
C: Codec<T>,
|
C: Codec<T>,
|
||||||
|
@ -76,7 +88,7 @@ pub fn use_storage_with_options<T, C>(
|
||||||
storage_type: StorageType,
|
storage_type: StorageType,
|
||||||
key: impl AsRef<str>,
|
key: impl AsRef<str>,
|
||||||
options: UseStorageOptions<T, C>,
|
options: UseStorageOptions<T, C>,
|
||||||
) -> (Memo<T>, impl Fn(Option<T>) -> ())
|
) -> (Memo<T>, impl Fn(Option<T>) -> () + Clone)
|
||||||
where
|
where
|
||||||
T: Clone + PartialEq,
|
T: Clone + PartialEq,
|
||||||
C: Codec<T>,
|
C: Codec<T>,
|
||||||
|
@ -206,6 +218,17 @@ fn decode_item<T, C: Codec<T>>(
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Default + 'static, C: Codec<T> + Default> Default for UseStorageOptions<T, C> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
codec: C::default(),
|
||||||
|
on_error: Rc::new(|_err| ()),
|
||||||
|
listen_to_storage_changes: true,
|
||||||
|
default_value: MaybeSignal::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: Clone + Default, C: Codec<T>> UseStorageOptions<T, C> {
|
impl<T: Clone + Default, C: Codec<T>> UseStorageOptions<T, C> {
|
||||||
pub(super) fn new(codec: C) -> Self {
|
pub(super) fn new(codec: C) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -244,7 +267,7 @@ pub trait Codec<T>: Clone + 'static {
|
||||||
fn decode(&self, str: String) -> Result<T, Self::Error>;
|
fn decode(&self, str: String) -> Result<T, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, Default, PartialEq)]
|
||||||
pub struct StringCodec();
|
pub struct StringCodec();
|
||||||
|
|
||||||
impl<T: FromStr + ToString> Codec<T> for StringCodec {
|
impl<T: FromStr + ToString> Codec<T> for StringCodec {
|
||||||
|
|
Loading…
Add table
Reference in a new issue