#[derive(Debug, Clone)]
pub(crate) enum QueryScope {
Documents(Vec<String>),
Workspace,
}
#[derive(Debug, Clone)]
pub struct QueryContext {
pub(crate) query: String,
pub(crate) scope: QueryScope,
pub(crate) timeout_secs: Option<u64>,
pub(crate) force_analysis: bool,
}
impl QueryContext {
pub fn new(query: impl Into<String>) -> Self {
Self {
query: query.into(),
scope: QueryScope::Workspace,
timeout_secs: None,
force_analysis: false,
}
}
pub fn with_doc_ids(mut self, doc_ids: Vec<String>) -> Self {
self.scope = QueryScope::Documents(doc_ids);
self
}
pub fn with_workspace(mut self) -> Self {
self.scope = QueryScope::Workspace;
self
}
pub fn with_timeout_secs(mut self, secs: u64) -> Self {
self.timeout_secs = Some(secs);
self
}
pub fn with_force_analysis(mut self, force: bool) -> Self {
self.force_analysis = force;
self
}
}
impl From<String> for QueryContext {
fn from(query: String) -> Self {
Self::new(query)
}
}
impl From<&str> for QueryContext {
fn from(query: &str) -> Self {
Self::new(query)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query_context_new() {
let ctx = QueryContext::new("What is this?");
assert_eq!(ctx.query, "What is this?");
}
#[test]
fn test_query_context_from_string() {
let ctx: QueryContext = "Hello".to_string().into();
assert_eq!(ctx.query, "Hello");
}
#[test]
fn test_query_context_from_str() {
let ctx: QueryContext = "Hello".into();
assert_eq!(ctx.query, "Hello");
}
#[test]
fn test_single_doc_scope() {
let ctx = QueryContext::new("test").with_doc_ids(vec!["doc-1".to_string()]);
assert!(
matches!(ctx.scope, QueryScope::Documents(ref ids) if ids == &["doc-1".to_string()])
);
}
#[test]
fn test_multi_doc_scope() {
let ctx = QueryContext::new("test").with_doc_ids(vec!["a".into(), "b".into()]);
assert!(matches!(ctx.scope, QueryScope::Documents(ref ids) if ids.len() == 2));
}
#[test]
fn test_workspace_scope() {
let ctx = QueryContext::new("test");
assert!(matches!(ctx.scope, QueryScope::Workspace));
}
#[test]
fn test_builder_options() {
let ctx = QueryContext::new("test")
.with_doc_ids(vec!["doc-1".to_string()])
.with_timeout_secs(60);
assert_eq!(ctx.timeout_secs, Some(60));
}
#[test]
fn test_query_context_timeout_default() {
let ctx = QueryContext::new("test");
assert_eq!(ctx.timeout_secs, None);
}
}