bazzar/crates/channels/src/search.rs

109 lines
2.6 KiB
Rust
Raw Normal View History

2022-11-05 19:04:38 +01:00
#[derive(Debug, thiserror::Error, serde::Serialize, serde::Deserialize)]
2022-11-05 12:31:18 +01:00
pub enum Error {
#[error("Can't create index")]
CantCreate,
#[error("Failed to find records in bucket")]
QueryFailed,
}
pub type Result<T> = std::result::Result<T, Error>;
pub mod search {
use crate::search::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub query: String,
pub collection: String,
pub lang: String,
}
2022-11-05 19:04:38 +01:00
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
2022-11-05 12:31:18 +01:00
pub struct Output {
pub found: Option<Vec<String>>,
2022-11-05 19:04:38 +01:00
pub error: Option<Error>,
}
impl Output {
pub fn found(found: Vec<String>) -> Self {
Self {
found: Some(found),
..Default::default()
}
}
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
2022-11-05 12:31:18 +01:00
}
}
pub mod create_index {
use crate::search::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub key: String,
pub value: String,
pub collection: String,
pub lang: String,
}
2022-11-05 19:04:38 +01:00
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
2022-11-05 12:31:18 +01:00
pub struct Output {
pub found: Option<()>,
2022-11-05 19:04:38 +01:00
pub error: Option<Error>,
}
impl Output {
pub fn ok() -> Self {
Self {
found: Some(()),
..Default::default()
}
}
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
}
}
pub mod rpc {
use config::SharedAppConfig;
use crate::search::{create_index, search};
#[tarpc::service]
pub trait Search {
/// Search all matching indices.
async fn search(input: search::Input) -> search::Output;
/// Create new search index.
async fn create_index(input: create_index::Input) -> create_index::Output;
}
pub async fn create_client(config: SharedAppConfig) -> SearchClient {
use tarpc::client;
use tarpc::tokio_serde::formats::Bincode;
let l = config.lock();
let transport =
tarpc::serde_transport::tcp::connect(l.search().rpc_addr(), Bincode::default);
let client = SearchClient::new(
client::Config::default(),
transport.await.expect("Failed to connect to search server"),
)
.spawn();
client
2022-11-05 12:31:18 +01:00
}
}