#[cfg(feature = "sql-browser-tokio")]
mod tokio;
#[cfg(feature = "sql-browser-async-std")]
mod async_std;
#[cfg(feature = "sql-browser-smol")]
mod smol;
use crate::client::Config;
use async_trait::async_trait;
#[async_trait]
pub trait SqlBrowser {
async fn connect_named(builder: &Config) -> crate::Result<Self>
where
Self: Sized + Send + Sync;
}
#[cfg(any(
feature = "sql-browser-async-std",
feature = "sql-browser-tokio",
feature = "sql-browser-smol"
))]
fn get_port_from_sql_browser_reply(
mut buf: Vec<u8>,
len: usize,
instance_name: &str,
) -> crate::Result<u16> {
const DELIMITER: &[u8] = b"tcp;";
buf.truncate(len);
let err = crate::Error::Conversion(
format!("Could not resolve SQL browser instance {}", instance_name).into(),
);
if len == 0 {
return Err(err);
}
let rsp = &buf[3..len];
let port: u16 = rsp
.windows(DELIMITER.len())
.rev()
.position(|window| window == DELIMITER)
.and_then(|pos| rsp[(rsp.len() - pos)..].split(|item| *item == b';').next())
.ok_or(err)
.and_then(|val| Ok(std::str::from_utf8(val)?.parse()?))?;
Ok(port)
}