Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,963 changes: 0 additions & 2,963 deletions crates/orbit-core/src/command/search.rs

This file was deleted.

117 changes: 117 additions & 0 deletions crates/orbit-core/src/command/search/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use super::types::GlobalSearchHit;

pub(super) fn lexical_task_hit(task: &orbit_common::types::Task) -> GlobalSearchHit {
GlobalSearchHit {
kind: "task".to_string(),
source: "lexical".to_string(),
id: Some(task.id.clone()),
path: None,
title: Some(task.title.clone()),
summary: Some(task.description.clone()),
status: Some(task.status.to_string()),
best_field: None,
snippet: None,
score: None,
matched_by: None,
}
}

pub(super) fn semantic_hit_to_global(hit: orbit_search::SemanticHit) -> GlobalSearchHit {
GlobalSearchHit {
kind: hit.source_kind,
source: "semantic".to_string(),
id: Some(hit.source_id),
path: None,
title: None,
summary: None,
status: None,
best_field: Some(hit.best_field),
snippet: Some(hit.snippet),
score: Some(hit.score),
matched_by: None,
}
}

pub(super) fn doc_result_to_global(
result: orbit_search::DocSearchResult,
source: &str,
score: Option<f32>,
) -> GlobalSearchHit {
GlobalSearchHit {
kind: "doc".to_string(),
source: source.to_string(),
id: None,
path: Some(result.record.path),
title: None,
summary: Some(result.record.summary),
status: Some(result.record.doc_type),
best_field: None,
snippet: None,
score,
matched_by: Some(result.matched_by),
}
}

pub(super) fn adr_result_to_global(
result: orbit_search::AdrSearchResult,
source: &str,
) -> GlobalSearchHit {
GlobalSearchHit {
kind: "adr".to_string(),
source: source.to_string(),
id: Some(result.id),
path: Some(result.path.to_string_lossy().into_owned()),
title: Some(result.title),
summary: None,
status: Some(result.status.to_string()),
best_field: None,
snippet: None,
score: Some(result.score as f32),
matched_by: Some(result.matched_by),
}
}

pub(super) fn adr_to_global_hit(
adr: orbit_common::types::Adr,
matched_by: Option<Vec<String>>,
) -> GlobalSearchHit {
adr_to_global_hit_with_source(adr, "lexical", matched_by)
}

pub(super) fn adr_to_global_hit_with_source(
adr: orbit_common::types::Adr,
source: &str,
matched_by: Option<Vec<String>>,
) -> GlobalSearchHit {
let path = std::path::PathBuf::from(".orbit")
.join("adrs")
.join(adr.status.cli_name())
.join(&adr.id)
.join("body.md");
GlobalSearchHit {
kind: "adr".to_string(),
source: source.to_string(),
id: Some(adr.id),
path: Some(path.to_string_lossy().into_owned()),
title: Some(adr.title),
summary: None,
status: Some(adr.status.to_string()),
best_field: None,
snippet: None,
score: None,
matched_by,
}
}

pub(super) fn filter_matched_by(tag_filter: &[String], path: Option<&str>) -> Option<Vec<String>> {
let mut matched = Vec::new();
matched.extend(tag_filter.iter().map(|tag| format!("tag:{tag}")));
if let Some(path) = path {
matched.push(format!("path:{path}"));
}
if matched.is_empty() {
None
} else {
Some(matched)
}
}
213 changes: 213 additions & 0 deletions crates/orbit-core/src/command/search/filters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
use std::str::FromStr;

use orbit_common::types::{AdrStatus, LearningStatus, OrbitError, TaskStatus};

use super::types::GlobalSearchParams;

pub(super) fn task_has_all_tags(task: &orbit_common::types::Task, tag_filter: &[String]) -> bool {
tag_filter.iter().all(|needle| {
task.tags
.iter()
.any(|candidate| candidate.eq_ignore_ascii_case(needle))
})
}

pub(super) fn learning_has_all_tags(
learning: &orbit_common::types::Learning,
tag_filter: &[String],
) -> bool {
tag_filter.iter().all(|needle| {
learning
.scope
.tags
.iter()
.any(|candidate| candidate.eq_ignore_ascii_case(needle))
})
}

pub(super) fn doc_has_all_tags(record: &crate::DocRecord, tag_filter: &[String]) -> bool {
tag_filter.iter().all(|needle| {
record
.frontmatter
.tags
.iter()
.any(|candidate| candidate.eq_ignore_ascii_case(needle))
})
}

pub(super) fn adr_has_all_tags(adr: &orbit_common::types::Adr, tag_filter: &[String]) -> bool {
tag_filter.iter().all(|needle| {
adr.tags
.iter()
.any(|candidate| candidate.eq_ignore_ascii_case(needle))
})
}

pub(super) fn adr_result_has_all_tags(
result: &orbit_search::AdrSearchResult,
tag_filter: &[String],
) -> bool {
tag_filter.iter().all(|needle| {
result
.tags
.iter()
.any(|candidate| candidate.eq_ignore_ascii_case(needle))
})
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(super) struct SearchStatusFilters {
pub(super) task: Option<Vec<TaskStatus>>,
pub(super) doc_active: Option<bool>,
pub(super) learning: Option<Vec<LearningStatus>>,
pub(super) adr: Option<Vec<AdrStatus>>,
}

impl SearchStatusFilters {
pub(super) fn parse(raw_statuses: &[String]) -> Result<Self, OrbitError> {
// ADR-0179: status tokens are kind-qualified to avoid cross-corpus ambiguity.
let mut filters = Self::default();
for raw in raw_statuses {
for token in raw
.split(',')
.map(str::trim)
.filter(|token| !token.is_empty())
{
let Some((kind, value)) = token.split_once(':') else {
return Err(OrbitError::InvalidInput(format!(
"status token `{token}` must use `kind:value` form"
)));
};
let kind = kind.trim().to_ascii_lowercase();
let value = value.trim().to_ascii_lowercase();
if kind.is_empty() || value.is_empty() {
return Err(OrbitError::InvalidInput(format!(
"status token `{token}` must use `kind:value` form"
)));
}
match kind.as_str() {
"task" => filters.push_task_status(&value)?,
"doc" => filters.set_doc_status(&value)?,
"learning" => filters.push_learning_status(&value)?,
"adr" => filters.push_adr_status(&value)?,
other => {
return Err(OrbitError::InvalidInput(format!(
"invalid status kind `{other}` in token `{token}`; expected task, doc, learning, or adr"
)));
}
}
}
}
Ok(filters)
}

fn push_task_status(&mut self, value: &str) -> Result<(), OrbitError> {
let statuses = self.task.get_or_insert_with(Vec::new);
if value == "open" {
extend_unique(statuses, task_open_statuses());
return Ok(());
}
let status = TaskStatus::from_str(value).map_err(|_| {
OrbitError::InvalidInput(format!(
"invalid status `{value}` for kind `task`; expected open, proposed, friction, backlog, in-progress, review, done, blocked, archived, rejected, or someday"
))
})?;
push_unique(statuses, status);
Ok(())
}

fn set_doc_status(&mut self, value: &str) -> Result<(), OrbitError> {
if value != "active" {
return Err(OrbitError::InvalidInput(format!(
"invalid status `{value}` for kind `doc`; expected active"
)));
}
self.doc_active = Some(true);
Ok(())
}

fn push_learning_status(&mut self, value: &str) -> Result<(), OrbitError> {
let status = LearningStatus::from_str(value).map_err(|_| {
OrbitError::InvalidInput(format!(
"invalid status `{value}` for kind `learning`; expected active or superseded"
))
})?;
let statuses = self.learning.get_or_insert_with(Vec::new);
push_unique(statuses, status);
Ok(())
}

fn push_adr_status(&mut self, value: &str) -> Result<(), OrbitError> {
let status = AdrStatus::from_str(value).map_err(|_| {
OrbitError::InvalidInput(format!(
"invalid status `{value}` for kind `adr`; expected proposed, accepted, superseded, or deleted"
))
})?;
let statuses = self.adr.get_or_insert_with(Vec::new);
push_unique(statuses, status);
Ok(())
}
}

fn push_unique<T: PartialEq>(values: &mut Vec<T>, value: T) {
if !values.contains(&value) {
values.push(value);
}
}

fn extend_unique<T: Copy + PartialEq>(values: &mut Vec<T>, incoming: &[T]) {
for value in incoming {
push_unique(values, *value);
}
}

fn task_open_statuses() -> &'static [TaskStatus] {
&[
TaskStatus::Proposed,
TaskStatus::Backlog,
TaskStatus::InProgress,
TaskStatus::Review,
]
}

pub(super) fn resolve_task_statuses(
params: &GlobalSearchParams,
status_filters: &SearchStatusFilters,
) -> Vec<TaskStatus> {
if let Some(statuses) = &status_filters.task {
return statuses.clone();
}
let mut set = task_open_statuses().to_vec();
if params.all {
set.extend([TaskStatus::Done, TaskStatus::Rejected, TaskStatus::Archived]);
}
set
}

pub(super) fn resolve_learning_statuses(
params: &GlobalSearchParams,
status_filters: &SearchStatusFilters,
) -> Vec<LearningStatus> {
if let Some(statuses) = &status_filters.learning {
return statuses.clone();
}
let mut set = vec![LearningStatus::Active];
if params.all {
set.push(LearningStatus::Superseded);
}
set
}

pub(super) fn resolve_adr_statuses(
params: &GlobalSearchParams,
status_filters: &SearchStatusFilters,
) -> Vec<AdrStatus> {
if let Some(statuses) = &status_filters.adr {
return statuses.clone();
}
let mut set = vec![AdrStatus::Proposed, AdrStatus::Accepted];
if params.all {
set.push(AdrStatus::Superseded);
}
set
}
Loading
Loading