hyper_scripter/query/
list_query_handler.rs

1use super::{do_script_query_strict, ListQuery, ScriptQuery};
2use crate::error::{Error, Result};
3use crate::script_repo::{RepoEntry, ScriptRepo};
4
5// SAFETY: 此特徵的實作應保證永遠不改動 repo 本身(只可回傳其可變參照),否則先前存下的參照可能會無效化
6pub unsafe trait ListQueryHandler {
7    type Item;
8    async fn handle_query<'a>(
9        &mut self,
10        query: ScriptQuery,
11        repo: &'a mut ScriptRepo,
12    ) -> Result<Option<RepoEntry<'a>>>;
13    fn handle_item(&mut self, item: Self::Item) -> Option<ListQuery>;
14    fn should_raise_dont_fuzz_on_empty() -> bool;
15    fn should_return_all_on_empty() -> bool;
16}
17
18pub struct DefaultListQueryHandler;
19
20// SAFETY: 實作永不改動 repo 本身
21unsafe impl ListQueryHandler for DefaultListQueryHandler {
22    type Item = ListQuery;
23    async fn handle_query<'a>(
24        &mut self,
25        query: ScriptQuery,
26        repo: &'a mut ScriptRepo,
27    ) -> Result<Option<RepoEntry<'a>>> {
28        match do_script_query_strict(&query, repo).await {
29            Ok(script) => Ok(Some(script)),
30            Err(Error::DontFuzz) => Ok(None),
31            Err(err) => Err(err),
32        }
33    }
34    fn handle_item(&mut self, item: Self::Item) -> Option<ListQuery> {
35        Some(item)
36    }
37    fn should_raise_dont_fuzz_on_empty() -> bool {
38        true
39    }
40    fn should_return_all_on_empty() -> bool {
41        true
42    }
43}