use crate::{
schema::{DatabaseSchema, MonitorRequest, TableUpdate},
transports::{ipc, tcp},
};
use jsonrpsee::{async_client::ClientBuilder, core::client::SubscriptionClientT, proc_macros::rpc};
use std::{collections::HashMap, path::Path};
use tokio::net::ToSocketAddrs;
#[rpc(client)]
pub trait Rpc {
#[method(name = "list_dbs")]
async fn list_databases(&self) -> Result<Vec<String>, ErrorObjectOwned>;
#[method(name = "get_schema")]
async fn get_schema(&self, db_name: &str) -> Result<DatabaseSchema, ErrorObjectOwned>;
#[method(name = "monitor")]
async fn monitor(
&self,
db_name: &str,
matcher: Option<&str>,
requests: HashMap<String, MonitorRequest>,
) -> Result<TableUpdate<serde_json::Value>, ErrorObjectOwned>;
}
pub async fn connect_tcp(
tcp: impl ToSocketAddrs,
) -> Result<impl SubscriptionClientT, std::io::Error> {
let (sender, receiver) = tcp::connect(tcp).await?;
Ok(ClientBuilder::default().build_with_tokio(sender, receiver))
}
pub async fn connect_unix(
socket_path: impl AsRef<Path>,
) -> Result<impl SubscriptionClientT, std::io::Error> {
let (sender, receiver) = ipc::connect(socket_path).await?;
Ok(ClientBuilder::default().build_with_tokio(sender, receiver))
}