agentmux_srv_core/ambient/
cli_locator.rs1use std::future::Future;
10use std::pin::Pin;
11use std::sync::OnceLock;
12
13pub type Locator = for<'a> fn(&'a str) -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'a>>;
15
16static LOCATOR: OnceLock<Locator> = OnceLock::new();
17
18pub fn set_locator(f: Locator) {
20 let _ = LOCATOR.set(f);
21}
22
23pub async fn locate(provider_id: &str) -> Option<String> {
25 match LOCATOR.get() {
26 Some(f) => f(provider_id).await,
27 None => None,
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34
35 fn found(id: &str) -> Pin<Box<dyn Future<Output = Option<String>> + Send + '_>> {
36 Box::pin(async move { (id == "known").then(|| "/bin/known".to_string()) })
37 }
38
39 #[tokio::test]
40 async fn it_asks_the_registered_lookup_and_finds_nothing_without_one() {
41 assert_eq!(locate("nope").await, None);
43 set_locator(found);
44 assert_eq!(locate("known").await.as_deref(), Some("/bin/known"));
45 assert_eq!(locate("nope").await, None);
46 }
47}