use std::sync::Arc;
use crate::search_index::{IndexKind, IndexPerson};
use crate::search_preview::SearchPreview;
use super::{SearchRow, scope_contains};
#[derive(Debug, Default, Eq)]
pub struct SearchEntry {
pub within: Option<String>,
pub row: Option<SearchRow>,
pub anchor: Option<String>,
pub title: String,
pub text: String,
pub action: Option<tilo_actions::ActionAddress>,
pub group: Option<String>,
pub subtitle: Option<String>,
pub icon: Option<String>,
pub kind: Option<IndexKind>,
pub people: Vec<IndexPerson>,
pub place: Option<IndexPerson>,
pub when: Option<i64>,
pub preview: Option<SearchPreview>,
pub boost: i32,
pub body: Option<SearchBody>,
pub fields: Vec<(String, SearchWeight)>,
pub facets: Vec<(String, String, String)>,
pub secret: bool,
pub picture: Option<String>,
}
impl Clone for SearchEntry {
fn clone(&self) -> Self {
Self {
within: self.within.clone(),
row: self.row.clone(),
anchor: self.anchor.clone(),
title: self.title.clone(),
text: self.text.clone(),
action: self.action.clone(),
group: self.group.clone(),
subtitle: self.subtitle.clone(),
icon: self.icon.clone(),
kind: self.kind,
people: self.people.clone(),
place: self.place.clone(),
when: self.when,
preview: self.preview.clone(),
boost: self.boost,
body: self.body.clone(),
fields: self.fields.clone(),
facets: self.facets.clone(),
secret: self.secret,
picture: self.picture.clone(),
}
}
}
impl PartialEq for SearchEntry {
fn eq(&self, other: &Self) -> bool {
self.within == other.within
&& self.row == other.row
&& self.anchor == other.anchor
&& self.title == other.title
&& self.text == other.text
&& self.action == other.action
&& self.group == other.group
&& self.subtitle == other.subtitle
&& self.icon == other.icon
&& self.kind == other.kind
&& self.people == other.people
&& self.place == other.place
&& self.when == other.when
&& self.preview == other.preview
&& self.boost == other.boost
&& self.body == other.body
&& self.fields == other.fields
&& self.facets == other.facets
&& self.secret == other.secret
&& self.picture == other.picture
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SearchWeight {
Title,
Identity,
Subtitle,
Metadata,
Body,
Unranked,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SearchPiece<'a> {
pub text: &'a str,
pub space_before: bool,
}
pub trait SearchText: Send + Sync {
fn segment_count(&self) -> usize;
fn piece_count(&self, segment: usize) -> usize;
fn piece(&self, segment: usize, piece: usize) -> Option<SearchPiece<'_>>;
fn locates(&self, segment: usize) -> bool {
let _ = segment;
false
}
fn location(&self, segment: usize) -> Option<SearchEntry> {
let _ = segment;
None
}
}
#[derive(Clone)]
pub struct SearchBody(Arc<dyn SearchText>);
impl SearchBody {
#[must_use]
pub fn new(text: Arc<dyn SearchText>) -> Self {
Self(text)
}
#[must_use]
pub fn text(&self) -> &dyn SearchText {
self.0.as_ref()
}
#[must_use]
pub fn shared(&self) -> Arc<dyn SearchText> {
Arc::clone(&self.0)
}
}
impl PartialEq for SearchBody {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for SearchBody {}
impl std::fmt::Debug for SearchBody {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("SearchBody")
.field("segments", &self.0.segment_count())
.finish()
}
}
impl SearchEntry {
#[must_use]
pub fn facet(
mut self,
key: impl Into<String>,
label: impl Into<String>,
value: impl Into<String>,
) -> Self {
self.facets.push((key.into(), label.into(), value.into()));
self
}
#[must_use]
pub fn in_scope(&self, scope: &str) -> bool {
[self.within.as_deref(), self.anchor.as_deref()]
.into_iter()
.flatten()
.any(|anchor| scope_contains(scope, anchor))
}
pub fn new(title: impl Into<String>, text: impl Into<String>) -> Self {
Self::titled(title.into(), text.into())
}
#[inline(never)]
fn titled(title: String, text: String) -> Self {
Self {
title,
text,
..Self::default()
}
}
#[must_use]
pub fn inside(mut self, scope: impl Into<String>) -> Self {
self.within = Some(scope.into());
self
}
#[must_use]
pub fn anchored(mut self, anchor: impl Into<String>) -> Self {
self.anchor = Some(anchor.into());
self
}
#[must_use]
pub fn invoking(mut self, action: tilo_actions::ActionAddress) -> Self {
self.action = Some(action);
self
}
#[must_use]
pub fn opening<E: crate::Effect>(mut self, row: impl Into<String>, effect: &E) -> Self {
let row = row.into();
self.action = Some(tilo_actions::ActionAddress::new(row.clone(), effect.id()));
self.anchor = Some(row);
self
}
#[must_use]
pub fn grouped(mut self, group: impl Into<String>) -> Self {
self.group = Some(group.into());
self
}
#[must_use]
pub fn subtitled(mut self, subtitle: impl Into<String>) -> Self {
self.subtitle = Some(subtitle.into());
self
}
#[must_use]
pub fn with_icon(mut self, icon: impl Into<String>) -> Self {
self.icon = Some(icon.into());
self
}
#[must_use]
pub fn of_kind(mut self, kind: IndexKind) -> Self {
self.kind = Some(kind);
self
}
#[must_use]
pub fn showing(mut self, people: Vec<IndexPerson>) -> Self {
self.people = people;
self
}
#[must_use]
pub fn in_place(mut self, place: IndexPerson) -> Self {
self.place = Some(place);
self
}
#[must_use]
pub fn at(mut self, when: i64) -> Self {
self.when = Some(when);
self
}
#[must_use]
pub fn previewed(mut self, preview: SearchPreview) -> Self {
self.preview = Some(preview);
self
}
#[must_use]
pub fn boosted(mut self, boost: i32) -> Self {
self.boost = boost;
self
}
#[must_use]
pub fn field(mut self, text: impl Into<String>, weight: SearchWeight) -> Self {
let text = text.into();
if !text.trim().is_empty() {
self.fields.push((text, weight));
}
self
}
#[must_use]
pub fn with_body(mut self, body: SearchBody) -> Self {
self.body = Some(body);
self
}
#[must_use]
pub fn secret_bearing(mut self) -> Self {
self.secret = true;
self
}
}