leptos-use/src/storage/codec_string.rs
2023-10-29 11:41:59 +00:00

52 lines
1.5 KiB
Rust

use super::{Codec, UseStorageOptions};
use std::str::FromStr;
/// A codec for strings that relies on [`FromStr`] and [`ToString`] to parse.
///
/// This makes simple key / value easy to use for primitive types. It is also useful for encoding simple data structures without depending on serde.
///
/// ## Example
/// ```
/// # use leptos::*;
/// # use leptos_use::storage::{StorageType, use_local_storage, use_session_storage, use_storage_with_options, UseStorageOptions, StringCodec, JsonCodec, ProstCodec};
/// # use serde::{Deserialize, Serialize};
/// #
/// # pub fn Demo() -> impl IntoView {
/// let (get, set, remove) = use_local_storage::<i32, StringCodec>("my-key");
/// # view! { }
/// # }
/// ```
#[derive(Clone, Default, PartialEq)]
pub struct StringCodec();
impl<T: FromStr + ToString> Codec<T> for StringCodec {
type Error = T::Err;
fn encode(&self, val: &T) -> Result<String, Self::Error> {
Ok(val.to_string())
}
fn decode(&self, str: String) -> Result<T, Self::Error> {
T::from_str(&str)
}
}
impl<T: Default + FromStr + ToString> UseStorageOptions<T, StringCodec> {
/// Constructs a new `UseStorageOptions` with a [`StringCodec`] relying on [`FromStr`] to parse.
pub fn string_codec() -> Self {
Self::new(StringCodec())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_string_codec() {
let s = String::from("party time 🎉");
let codec = StringCodec();
assert_eq!(codec.encode(&s), Ok(s.clone()));
assert_eq!(codec.decode(s.clone()), Ok(s));
}
}