about summary refs log tree commit diff
path: root/kittybox-rs/src/database/file
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2023-06-22 20:25:28 +0300
committerVika <vika@fireburn.ru>2023-06-22 20:25:28 +0300
commitff358723da641af9f4ae1f742eb1b382f4630220 (patch)
tree32a2319a0bea909b9d267e44fa3a632991ddda12 /kittybox-rs/src/database/file
parent0285e630f3cae1ee2df2c7c465998b1ce669944f (diff)
downloadkittybox-ff358723da641af9f4ae1f742eb1b382f4630220.tar.zst
StorageError: use std::borrow::Cow for msg field
This allows avoiding an unnecessary allocation whenever the error
message is static.
Diffstat (limited to 'kittybox-rs/src/database/file')
-rw-r--r--kittybox-rs/src/database/file/mod.rs19
1 files changed, 10 insertions, 9 deletions
diff --git a/kittybox-rs/src/database/file/mod.rs b/kittybox-rs/src/database/file/mod.rs
index 9319b67..531f7b1 100644
--- a/kittybox-rs/src/database/file/mod.rs
+++ b/kittybox-rs/src/database/file/mod.rs
@@ -3,6 +3,7 @@ use crate::database::{filter_post, ErrorKind, Result, settings, Storage, Storage
 use async_trait::async_trait;
 use futures::{stream, StreamExt, TryStreamExt};
 use serde_json::json;
+use std::borrow::Cow;
 use std::collections::HashMap;
 use std::io::ErrorKind as IOErrorKind;
 use std::path::{Path, PathBuf};
@@ -19,7 +20,7 @@ impl From<std::io::Error> for StorageError {
                 IOErrorKind::AlreadyExists => ErrorKind::Conflict,
                 _ => ErrorKind::Backend,
             },
-            "file I/O error",
+            Cow::Owned(format!("file I/O error: {}", &source)),
             Box::new(source),
         )
     }
@@ -29,7 +30,7 @@ impl From<tokio::time::error::Elapsed> for StorageError {
     fn from(source: tokio::time::error::Elapsed) -> Self {
         Self::with_source(
             ErrorKind::Backend,
-            "timeout on I/O operation",
+            Cow::Borrowed("timeout on I/O operation"),
             Box::new(source),
         )
     }
@@ -135,7 +136,7 @@ fn modify_post(post: &serde_json::Value, update: &serde_json::Value) -> Result<s
                     .or_default()
                     .extend(v.clone());
             } else {
-                return Err(StorageError::new(
+                return Err(StorageError::from_static(
                     ErrorKind::BadRequest,
                     "Malformed update object",
                 ));
@@ -147,7 +148,7 @@ fn modify_post(post: &serde_json::Value, update: &serde_json::Value) -> Result<s
             if let Some(v) = v.as_array() {
                 add_keys.insert(k.to_string(), v.clone());
             } else {
-                return Err(StorageError::new(
+                return Err(StorageError::from_static(
                     ErrorKind::BadRequest,
                     "Malformed update object",
                 ));
@@ -160,7 +161,7 @@ fn modify_post(post: &serde_json::Value, update: &serde_json::Value) -> Result<s
             if let Some(v) = v.as_array() {
                 add_keys.insert(k.to_string(), v.clone());
             } else {
-                return Err(StorageError::new(
+                return Err(StorageError::from_static(
                     ErrorKind::BadRequest,
                     "Malformed update object",
                 ));
@@ -358,7 +359,7 @@ impl Storage for FileStorage {
                     let orig = path.clone();
                     // We're supposed to have a parent here.
                     let basedir = link.parent().ok_or_else(|| {
-                        StorageError::new(
+                        StorageError::from_static(
                             ErrorKind::Backend,
                             "Failed to calculate parent directory when creating a symlink",
                         )
@@ -551,7 +552,7 @@ impl Storage for FileStorage {
                         Err(err) => {
                             return Err(StorageError::with_source(
                                 ErrorKind::Other,
-                                "Feed assembly error",
+                                Cow::Owned(format!("Feed assembly error: {}", &err)),
                                 Box::new(err),
                             ));
                         }
@@ -560,7 +561,7 @@ impl Storage for FileStorage {
                 hydrate_author(&mut feed, user, self).await;
                 Ok(Some(feed))
             } else {
-                Err(StorageError::new(
+                Err(StorageError::from_static(
                     ErrorKind::PermissionDenied,
                     "specified user cannot access this post",
                 ))
@@ -599,7 +600,7 @@ impl Storage for FileStorage {
         let settings: HashMap<&str, serde_json::Value> = serde_json::from_str(&content)?;
         match settings.get(S::ID) {
             Some(value) => Ok(serde_json::from_value::<S>(value.clone())?),
-            None => Err(StorageError::new(ErrorKind::Backend, "Setting not set"))
+            None => Err(StorageError::from_static(ErrorKind::Backend, "Setting not set"))
         }
     }