use std::sync::Arc;
use crate::{
item::Eventable,
report::{ReportContext, ReportHandler},
};
#[myko_macros::myko_report_output]
pub struct EntitySearchResult {
pub ids: Vec<Arc<str>>,
}
#[myko_macros::myko_report(EntitySearchResult)]
pub struct EntitySearch {
pub entity_type: String,
pub query: String,
#[serde(default = "default_limit")]
pub limit: usize,
}
fn default_limit() -> usize {
100
}
impl EntitySearch {
pub fn for_type<T: Eventable>(query: &str) -> Self {
Self::for_type_with_limit::<T>(query, 100)
}
pub fn for_type_with_limit<T: Eventable>(query: &str, limit: usize) -> Self {
Self {
entity_type: T::ENTITY_NAME_STATIC.to_string(),
query: query.to_string(),
limit,
}
}
}
impl ReportHandler for EntitySearch {
type Output = EntitySearchResult;
fn compute(
&self,
ctx: ReportContext,
) -> hyphae::Cell<Arc<Self::Output>, hyphae::CellImmutable> {
let ids = ctx.search(&self.entity_type, &self.query, self.limit);
hyphae::Cell::new(Arc::new(EntitySearchResult { ids })).lock()
}
}