surrealdb/api/opt/endpoint/
mod.rs1#[cfg(feature = "protocol-http")]
2mod http;
3#[cfg(feature = "protocol-ws")]
4mod ws;
5
6#[cfg(feature = "kv-fdb")]
7mod fdb;
8#[cfg(feature = "kv-indxdb")]
9mod indxdb;
10#[cfg(feature = "kv-mem")]
11mod mem;
12#[cfg(feature = "kv-rocksdb")]
13mod rocksdb;
14#[cfg(feature = "kv-speedb")]
15mod speedb;
16#[cfg(feature = "kv-tikv")]
17mod tikv;
18
19use crate::api::Connection;
20use crate::api::Result;
21use url::Url;
22
23use super::Config;
24
25#[derive(Debug)]
27#[allow(dead_code)] pub struct Endpoint {
29 #[doc(hidden)]
30 pub url: Url,
31 #[doc(hidden)]
32 pub path: String,
33 pub(crate) config: Config,
34}
35
36pub trait IntoEndpoint<Scheme> {
38 type Client: Connection;
40 fn into_endpoint(self) -> Result<Endpoint>;
42}
43
44fn replace_tilde(path: &str) -> String {
45 if path.starts_with("~/") {
46 let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_owned());
47 path.replacen("~/", &format!("{home}/"), 1)
48 } else if path.starts_with("~\\") {
49 let home = std::env::var("HOMEPATH").unwrap_or_else(|_| ".".to_owned());
50 path.replacen("~\\", &format!("{home}\\"), 1)
51 } else {
52 path.to_owned()
53 }
54}
55
56#[allow(dead_code)]
57pub(crate) fn path_to_string(protocol: &str, path: impl AsRef<std::path::Path>) -> String {
58 use path_clean::PathClean;
59 use std::path::Path;
60
61 let path = path.as_ref().display().to_string();
62 let expanded = replace_tilde(&path);
63 let cleaned = Path::new(&expanded).clean();
64 format!("{protocol}{}", cleaned.display())
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_path_to_string() {
73 let paths = [
74 "path/to/db",
76 "/path/to/db",
77 "path\\to\\db",
79 "\\path\\to\\db",
80 "c:path\\to\\db",
81 "c:\\path\\to\\db",
82 ];
83
84 let scheme = "scheme://";
85
86 for path in paths {
87 let expanded = replace_tilde(path);
88 assert_eq!(expanded, path, "failed to replace `{path}`");
89
90 let converted = path_to_string(scheme, path);
91 assert_eq!(converted, format!("{scheme}{path}"), "failed to convert `{path}`");
92 }
93 }
94}