#[derive(Debug, thiserror::Error, serde::Serialize, serde::Deserialize)] pub enum Error { #[error("Can't create index")] CantCreate, #[error("Failed to find records in bucket")] QueryFailed, } pub type Result = std::result::Result; 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, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Output { pub found: Option>, pub error: Option, } impl Output { pub fn found(found: Vec) -> Self { Self { found: Some(found), ..Default::default() } } pub fn error(error: Error) -> Self { Self { error: Some(error), ..Default::default() } } } } 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, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Output { pub found: Option<()>, pub error: Option, } 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 } }