From d17ce92f815fa145fa56307a7e9b3993e0f4270d Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 9 Oct 2024 23:46:16 -0400 Subject: Return an instance of the client when opening a channel URL directly. --- src/db/mod.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/db') diff --git a/src/db/mod.rs b/src/db/mod.rs index b9c59ef..36d888f 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -97,24 +97,33 @@ pub enum Error { pub trait NotFound { type Ok; + type Error; + fn not_found(self, map: F) -> Result where - E: From, + E: From, F: FnOnce() -> E; + + fn optional(self) -> Result, Self::Error>; } impl NotFound for Result { type Ok = T; + type Error = sqlx::Error; + + fn optional(self) -> Result, sqlx::Error> { + match self { + Ok(value) => Ok(Some(value)), + Err(sqlx::Error::RowNotFound) => Ok(None), + Err(other) => Err(other), + } + } fn not_found(self, map: F) -> Result where E: From, F: FnOnce() -> E, { - match self { - Err(sqlx::Error::RowNotFound) => Err(map()), - Err(other) => Err(other.into()), - Ok(value) => Ok(value), - } + self.optional()?.ok_or_else(map) } } -- cgit v1.2.3