use crate::full_text;
use crate::prefetch::Source;
use gpui_kit::{AppContext as _, Context, Task};
pub struct Identity {
pub endpoint: String,
pub account: String,
pub key: String,
}
pub struct Binding {
pub source: Source,
pub scope: full_text::Scope,
pub identity: String,
}
pub struct Run<C, K> {
pub source: Source,
pub scope: full_text::Scope,
pub identity: String,
pub client: C,
pub cache: K,
}
pub struct SourceId<'a> {
pub app: &'a str,
pub board: &'a str,
}
pub fn bind(
service: full_text::Service,
app: &str,
board: &str,
verify: impl FnOnce() -> Result<Identity, String>,
) -> Result<Option<Binding>, String> {
let mut identity = None;
let source = Source::bind(service, board, || {
let verified = verify()?;
let scope = scope(app, board, &verified.endpoint, &verified.account)?;
identity = Some((scope.clone(), verified.key));
Ok(scope)
})?;
match (source, identity) {
(Some(source), Some((scope, identity))) => Ok(Some(Binding {
source,
scope,
identity,
})),
(None, None) => Ok(None),
_ => Err("Search source activation did not retain its identity".into()),
}
}
pub fn bind_run<C, K>(
service: full_text::Service,
id: SourceId<'_>,
client: C,
cache: K,
verify: impl FnOnce(&C) -> Result<Identity, String>,
) -> Result<Option<Run<C, K>>, String> {
let binding = bind(service, id.app, id.board, || verify(&client))?;
Ok(binding.map(|binding| Run {
source: binding.source,
scope: binding.scope,
identity: binding.identity,
client,
cache,
}))
}
pub struct Bind<C, K, T> {
pub client: C,
pub cache: K,
pub loaded: Vec<T>,
pub verify: Box<dyn FnOnce(&C) -> Result<Identity, String> + Send>,
}
pub fn schedule<B, T, C, K>(
cx: &mut Context<B>,
service: full_text::Service,
board_name: &str,
bind: Bind<C, K, T>,
start: impl FnOnce(&mut B, Run<C, K>, Vec<T>, full_text::Service, &mut Context<B>) + Send + 'static,
) -> Task<()>
where
B: 'static,
T: Send + 'static,
C: Send + 'static,
K: Send + 'static,
{
let Bind {
client,
cache,
loaded,
verify,
} = bind;
let app = service.app_id().to_owned();
let board_name = board_name.to_owned();
let service_for_index = service.clone();
cx.spawn(async move |this, cx| {
let bound = cx
.background_spawn(async move {
bind_run(
service,
SourceId {
app: &app,
board: &board_name,
},
client,
cache,
verify,
)
.ok()?
})
.await;
let Some(bound) = bound else {
return;
};
let _ = this.update(cx, |board_state, cx| {
start(board_state, bound, loaded, service_for_index, cx)
});
})
}
pub fn scope(
app: &str,
board: &str,
endpoint: &str,
account: &str,
) -> Result<full_text::Scope, String> {
if app.trim().is_empty()
|| board.trim().is_empty()
|| endpoint.trim().is_empty()
|| account.trim().is_empty()
{
return Err("Search identity is incomplete".into());
}
Ok(full_text::Scope {
app: app.into(),
board: board.into(),
endpoint: endpoint.into(),
tenant: String::new(),
account: account.into(),
})
}
pub fn non_empty<T>(items: Vec<T>) -> Option<Vec<T>> {
(!items.is_empty()).then_some(items)
}
#[derive(Clone)]
pub struct Indexing {
pub service: full_text::Service,
pub scope: full_text::Scope,
}
pub fn inactive_scope(error: &str) -> bool {
error == "Search scope is inactive or caching is disabled"
}
pub fn bounded_text(text: String, limit: usize, marker: &str) -> String {
crate::context_text::cap_to(text, limit, marker)
}
#[cfg(test)]
mod tests {
use super::{Identity, bind, bounded_text, inactive_scope, scope};
use crate::full_text::{Policy, Service};
#[test]
fn disabled_source_does_not_verify_identity() {
let root = std::env::temp_dir().join(format!(
"subspace-search-adapter-{}-{}",
std::process::id(),
chrono::Utc::now().timestamp_nanos_opt().unwrap()
));
let service = Service::open(root.clone(), "fixture".into(), vec!["board".into()]).unwrap();
let mut verified = false;
let binding = bind(service.clone(), "fixture", "board", || {
verified = true;
Ok(identity())
})
.unwrap();
assert!(binding.is_none());
assert!(!verified);
service
.set_policy(
"board",
Policy {
enabled: true,
..Default::default()
},
)
.unwrap();
assert!(
bind(service, "fixture", "board", || Ok(identity()))
.unwrap()
.is_some()
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn scope_and_text_are_validated_and_bounded() {
assert!(scope("app", "board", "https://example.com", "account").is_ok());
assert!(scope("app", "board", "", "account").is_err());
assert_eq!(bounded_text("ééé".into(), 5, "!"), "éé!");
}
#[test]
fn write_refusal_for_an_inactive_scope_is_distinguished_from_a_real_failure() {
assert!(inactive_scope(
"Search scope is inactive or caching is disabled"
));
assert!(!inactive_scope("Search storage operation failed"));
}
fn identity() -> Identity {
Identity {
endpoint: "https://example.com".into(),
account: "config:token".into(),
key: "config:token@https://example.com".into(),
}
}
}