about summary refs log tree commit diff
path: root/util/src
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-12-03 08:29:38 +0300
committerVika <vika@fireburn.ru>2024-12-03 08:36:37 +0300
commitca76b67e985583ebc4276d6dce9dc74fde3af3bc (patch)
treed30ebc7a384771ed1c89904ce94ea65c5cdf19cd /util/src
parented36a42f20758465d09961e8993911ca1d71f1c3 (diff)
downloadkittybox-ca76b67e985583ebc4276d6dce9dc74fde3af3bc.tar.zst
kittybox-util: bump to 0.3.0
Changed micropub::Error's description to Option<Cow<'static, str>> to
allow for that sweet sweet memory savings from not having to
heap-allocate strings for static errors.

Change-Id: Ic82e5ad5cacea766ea0a7e8677ce6a7f16ae8668
Diffstat (limited to 'util/src')
-rw-r--r--util/src/micropub.rs33
1 files changed, 25 insertions, 8 deletions
diff --git a/util/src/micropub.rs b/util/src/micropub.rs
index 6127079..9d2c525 100644
--- a/util/src/micropub.rs
+++ b/util/src/micropub.rs
@@ -98,8 +98,8 @@ pub struct Error {
     /// General kind of an error that occured.
     pub error: ErrorKind,
     /// A human-readable error description intended for application developers.
-    // TODO use Cow<'static, str> to save on heap allocations
-    pub error_description: String,
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub error_description: Option<std::borrow::Cow<'static, str>>,
 }
 
 impl std::error::Error for Error {}
@@ -113,9 +113,13 @@ impl std::fmt::Display for Error {
         };
 
         f.write_str(&s)?;
-        f.write_str(" (")?;
-        f.write_str(&self.error_description)?;
-        f.write_str(")")
+        if let Some(desc) = self.error_description.as_deref() {
+            f.write_str(" (")?;
+            f.write_str(desc)?;
+            f.write_str(")")?;
+        };
+
+        Ok(())
     }
 }
 
@@ -124,21 +128,34 @@ impl From<serde_json::Error> for Error {
         use ErrorKind::*;
         Self {
             error: InvalidRequest,
-            error_description: err.to_string(),
+            error_description: Some(err.to_string().into()),
         }
     }
 }
 
 impl Error {
     /// Create a new Micropub error.
-    pub fn new(error: ErrorKind, error_description: &str) -> Self {
+    pub fn new(error: ErrorKind, error_description: String) -> Self {
+        Self {
+            error,
+            error_description: Some(error_description.into()),
+        }
+    }
+    /// Create a new Micropub error from a static string.
+    pub const fn from_static(error: ErrorKind, error_description: &'static str) -> Self {
         Self {
             error,
-            error_description: error_description.to_owned(),
+            error_description: Some(std::borrow::Cow::Borrowed(error_description))
         }
     }
 }
 
+impl From<ErrorKind> for Error {
+    fn from(error: ErrorKind) -> Self {
+        Self { error, error_description: None }
+    }
+}
+
 #[cfg(feature = "http")]
 impl From<&Error> for http::StatusCode {
     fn from(err: &Error) -> Self {