From a24bb4f4b88eff184f3722c3e4e9acf0d91ef418 Mon Sep 17 00:00:00 2001 From: Rushmore Mushambi Date: Sat, 31 Dec 2022 01:53:45 +0200 Subject: [PATCH] Allow `memory` as an address endpoint in CLI `sql` command (#1563) --- lib/src/api/engines/any/mod.rs | 13 ++++++------- lib/tests/api.rs | 6 ++++++ src/cli/mod.rs | 8 +++++++- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/src/api/engines/any/mod.rs b/lib/src/api/engines/any/mod.rs index 8175f39c..ff69a0c5 100644 --- a/lib/src/api/engines/any/mod.rs +++ b/lib/src/api/engines/any/mod.rs @@ -115,8 +115,12 @@ pub trait IntoEndpoint { impl IntoEndpoint for &str { fn into_endpoint(self) -> Result { + let url = match self { + "memory" => "mem://", + _ => self, + }; Ok(Endpoint { - endpoint: Url::parse(self).map_err(|_| Error::InvalidUrl(self.to_owned()))?, + endpoint: Url::parse(url).map_err(|_| Error::InvalidUrl(self.to_owned()))?, strict: false, #[cfg(any(feature = "native-tls", feature = "rustls"))] tls_config: None, @@ -132,12 +136,7 @@ impl IntoEndpoint for &String { impl IntoEndpoint for String { fn into_endpoint(self) -> Result { - Ok(Endpoint { - endpoint: Url::parse(&self).map_err(|_| Error::InvalidUrl(self))?, - strict: false, - #[cfg(any(feature = "native-tls", feature = "rustls"))] - tls_config: None, - }) + self.as_str().into_endpoint() } } diff --git a/lib/tests/api.rs b/lib/tests/api.rs index 970ae0b5..53347134 100644 --- a/lib/tests/api.rs +++ b/lib/tests/api.rs @@ -93,6 +93,7 @@ mod http { #[cfg(feature = "kv-mem")] mod mem { use super::*; + use surrealdb::engines::any; use surrealdb::engines::local::Db; use surrealdb::engines::local::Mem; @@ -100,6 +101,11 @@ mod mem { Surreal::new::(()).await.unwrap() } + #[tokio::test] + async fn memory_allowed_as_address() { + any::connect("memory").await.unwrap(); + } + include!("api/mod.rs"); include!("api/backup.rs"); } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index f910b09e..df39a654 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -28,7 +28,13 @@ We would love it if you could star the repository (https://github.com/surrealdb/ "; fn split_endpoint(v: &str) -> (&str, &str) { - v.split_once("://").unwrap_or_default() + match v { + "memory" => ("mem", ""), + v => match v.split_once("://") { + Some(parts) => parts, + None => v.split_once(':').unwrap_or_default(), + }, + } } fn file_valid(v: &str) -> Result<(), String> {