chore (backend-rs): don't use unsafe unwrap() in examples

This commit is contained in:
naskya 2024-05-29 19:20:42 +09:00
parent bd7dad8b59
commit fac69f8eb5
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C

View file

@ -56,17 +56,20 @@ fn wildcard(category: Category) -> String {
/// ///
/// ``` /// ```
/// # use backend_rs::database::cache; /// # use backend_rs::database::cache;
/// # tokio_test::block_on(async { /// # async fn f() -> Result<(), Box<dyn std::error::Error>> {
/// let key = "apple"; /// let key = "apple";
/// let data = "I want to cache this string".to_string(); /// let data = "I want to cache this string".to_string();
/// ///
/// // caches the data for 10 seconds /// // caches the data for 10 seconds
/// cache::set(key, &data, 10).await; /// cache::set(key, &data, 10).await?;
/// ///
/// // get the cache /// // get the cache
/// let cached_data = cache::get::<String>(key).await.unwrap(); /// let cached_data = cache::get::<String>(key).await?;
///
/// assert!(cached_data.is_some());
/// assert_eq!(data, cached_data.unwrap()); /// assert_eq!(data, cached_data.unwrap());
/// # }) /// # Ok(())
/// # }
/// ``` /// ```
pub async fn set<V: for<'a> Deserialize<'a> + Serialize>( pub async fn set<V: for<'a> Deserialize<'a> + Serialize>(
key: &str, key: &str,
@ -97,21 +100,23 @@ pub async fn set<V: for<'a> Deserialize<'a> + Serialize>(
/// ///
/// ``` /// ```
/// # use backend_rs::database::cache; /// # use backend_rs::database::cache;
/// # tokio_test::block_on(async { /// # async fn f() -> Result<(), Box<dyn std::error::Error>> {
/// let key = "banana"; /// let key = "banana";
/// let data = "I want to cache this string".to_string(); /// let data = "I want to cache this string".to_string();
/// ///
/// // set cache /// // set cache
/// cache::set(key, &data, 10).await.unwrap(); /// cache::set(key, &data, 10).await?;
/// ///
/// // get cache /// // get cache
/// let cached_data = cache::get::<String>(key).await.unwrap(); /// let cached_data = cache::get::<String>(key).await?;
/// assert!(cached_data.is_some());
/// assert_eq!(data, cached_data.unwrap()); /// assert_eq!(data, cached_data.unwrap());
/// ///
/// // get nonexistent (or expired) cache /// // get nonexistent (or expired) cache
/// let no_cache = cache::get::<String>("nonexistent").await.unwrap(); /// let no_cache = cache::get::<String>("nonexistent").await?;
/// assert!(no_cache.is_none()); /// assert!(no_cache.is_none());
/// # }) /// # Ok(())
/// # }
/// ``` /// ```
pub async fn get<V: for<'a> Deserialize<'a> + Serialize>(key: &str) -> Result<Option<V>, Error> { pub async fn get<V: for<'a> Deserialize<'a> + Serialize>(key: &str) -> Result<Option<V>, Error> {
let serialized_value: Option<Vec<u8>> = redis_conn().await?.get(prefix_key(key)).await?; let serialized_value: Option<Vec<u8>> = redis_conn().await?.get(prefix_key(key)).await?;
@ -134,21 +139,22 @@ pub async fn get<V: for<'a> Deserialize<'a> + Serialize>(key: &str) -> Result<Op
/// ///
/// ``` /// ```
/// # use backend_rs::database::cache; /// # use backend_rs::database::cache;
/// # tokio_test::block_on(async { /// # async fn f() -> Result<(), Box<dyn std::error::Error>> {
/// let key = "chocolate"; /// let key = "chocolate";
/// let value = "I want to cache this string".to_string(); /// let value = "I want to cache this string".to_string();
/// ///
/// // set cache /// // set cache
/// cache::set(key, &value, 10).await.unwrap(); /// cache::set(key, &value, 10).await?;
/// ///
/// // delete the cache /// // delete the cache
/// cache::delete("foo").await.unwrap(); /// cache::delete("foo").await?;
/// cache::delete("nonexistent").await.unwrap(); // this is okay /// cache::delete("nonexistent").await?; // this is okay
/// ///
/// // the cache is gone /// // the cache is gone
/// let cached_value = cache::get::<String>("foo").await.unwrap(); /// let cached_value = cache::get::<String>("foo").await?;
/// assert!(cached_value.is_none()); /// assert!(cached_value.is_none());
/// # }) /// # Ok(())
/// # }
/// ``` /// ```
pub async fn delete(key: &str) -> Result<(), Error> { pub async fn delete(key: &str) -> Result<(), Error> {
Ok(redis_conn().await?.del(prefix_key(key)).await?) Ok(redis_conn().await?.del(prefix_key(key)).await?)