use crate::api::engine::local::Db;
use crate::api::engine::local::SurrealKv;
use crate::api::err::Error;
use crate::api::opt::Config;
use crate::api::opt::Endpoint;
use crate::api::opt::IntoEndpoint;
use crate::api::Result;
use crate::Connect;
use std::path::Path;
use std::path::PathBuf;
use url::Url;
const VERSIONED_SCHEME: &str = "surrealkv+versioned";
macro_rules! endpoints {
($($name:ty),*) => {
$(
impl IntoEndpoint<SurrealKv> for $name {
type Client = Db;
fn into_endpoint(self) -> Result<Endpoint> {
let protocol = "surrealkv://";
let url = Url::parse(protocol)
.unwrap_or_else(|_| unreachable!("`{protocol}` should be static and valid"));
let mut endpoint = Endpoint::new(url);
endpoint.path = super::path_to_string(protocol, self);
Ok(endpoint)
}
}
impl IntoEndpoint<SurrealKv> for ($name, Config) {
type Client = Db;
fn into_endpoint(self) -> Result<Endpoint> {
let mut endpoint = IntoEndpoint::<SurrealKv>::into_endpoint(self.0)?;
endpoint.config = self.1;
Ok(endpoint)
}
}
)*
}
}
endpoints!(&str, &String, String, &Path, PathBuf);
impl<R> Connect<Db, R> {
pub fn versioned(mut self) -> Self {
let replace_scheme = |mut endpoint: Endpoint| -> Result<Endpoint> {
match endpoint.url.scheme() {
"surrealkv" => {
endpoint.url.set_scheme(VERSIONED_SCHEME).unwrap_or_else(|_| {
unreachable!("`{VERSIONED_SCHEME}` should be static and valid")
});
if let Some((_, rest)) = endpoint.path.split_once(':') {
endpoint.path = format!("{VERSIONED_SCHEME}:{rest}");
}
Ok(endpoint)
}
self::VERSIONED_SCHEME => Ok(endpoint),
scheme => Err(Error::VersionsNotSupported(scheme.to_owned()).into()),
}
};
self.address = self.address.and_then(replace_scheme);
self
}
}