mod read;
mod write;
#[cfg(all(test, feature = "kdb-integration-test"))]
mod integration_tests;
pub use read::*;
pub use write::*;
pub use kdb_plus_fixed::ipc::error::Error as KdbError;
pub use kdb_plus_fixed::ipc::K;
use std::collections::HashSet;
use std::sync::Arc;
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct Sym(Arc<str>);
impl std::fmt::Debug for Sym {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::fmt::Display for Sym {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Default for Sym {
fn default() -> Self {
Sym(Arc::from(""))
}
}
#[derive(Default)]
pub struct SymbolInterner {
set: HashSet<Arc<str>>,
}
impl SymbolInterner {
pub fn intern(&mut self, s: &str) -> Sym {
if let Some(existing) = self.set.get(s) {
Sym(Arc::clone(existing))
} else {
let arc: Arc<str> = Arc::from(s);
self.set.insert(Arc::clone(&arc));
Sym(arc)
}
}
}
#[derive(Debug, Clone)]
pub struct KdbConnection {
pub host: String,
pub port: u16,
pub credentials: Option<KdbCredentials>,
}
#[derive(Debug, Clone)]
pub struct KdbCredentials {
pub username: String,
pub password: String,
}
impl KdbConnection {
pub fn new(host: impl Into<String>, port: u16) -> Self {
Self {
host: host.into(),
port,
credentials: None,
}
}
pub fn with_credentials(
mut self,
username: impl Into<String>,
password: impl Into<String>,
) -> Self {
self.credentials = Some(KdbCredentials {
username: username.into(),
password: password.into(),
});
self
}
pub fn credentials_string(&self) -> String {
match &self.credentials {
Some(creds) => format!("{}:{}", creds.username, creds.password),
None => String::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_kdb_connection_new() {
let conn = KdbConnection::new("localhost", 5000);
assert_eq!(conn.host, "localhost");
assert_eq!(conn.port, 5000);
assert!(conn.credentials.is_none());
}
#[test]
fn test_kdb_connection_with_credentials() {
let conn = KdbConnection::new("localhost", 5000).with_credentials("user", "pass");
assert!(conn.credentials.is_some());
let creds = conn.credentials.unwrap();
assert_eq!(creds.username, "user");
assert_eq!(creds.password, "pass");
}
#[test]
fn test_credentials_string() {
let conn = KdbConnection::new("localhost", 5000);
assert_eq!(conn.credentials_string(), "");
let conn_with_creds =
KdbConnection::new("localhost", 5000).with_credentials("user", "pass");
assert_eq!(conn_with_creds.credentials_string(), "user:pass");
}
}