Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.3 KB
Raw Blame
#![deny(missing_docs)]
//! This crate contains queries to search over macro db entities by their name

mod channel;
mod chat;
mod highlight;

pub use channel::{ChannelNameSearchScope, search_channel_names};
pub use chat::search_chat_names;
pub use highlight::highlight_name;
pub use models_opensearch::SearchEntityType;
use models_search_cursor::{PaginatedResult, SearchCursorAttributes};
/// Escapes special regex characters in a search term
pub fn escape_regex(term: &str) -> String {
    let special_chars = [
        '\\', '.', '+', '*', '?', '(', ')', '[', ']', '{', '}', '^', '$', '|',
    ];
    let mut escaped = String::with_capacity(term.len() * 2);
    for c in term.chars() {
        if special_chars.contains(&c) {
            escaped.push('\\');
        }
        escaped.push(c);
    }
    escaped
}

/// Escapes `LIKE` wildcards in a search term so it matches literally.
pub fn escape_like(term: &str) -> String {
    let mut escaped = String::with_capacity(term.len() * 2);
    for c in term.chars() {
        if matches!(c, '\\' | '%' | '_') {
            escaped.push('\\');
        }
        escaped.push(c);
    }
    escaped
}

/// Errors for name search crate
#[derive(Debug, thiserror::Error)]
pub enum NameSearchError {
    /// Database error
    #[error("database error occurred")]
    DatabaseError(#[from] sqlx::Error),
    /// Empty search term
    #[error("empty search term provided")]
    EmptySearchTerm,
    /// Empty ids provided with ids only set to true
    #[error("empty ids provided with ids only set to true")]
    EmptyIdsWithIdsOnly,
    /// Cursor variant doesn't match this search method.
    #[error("incompatible cursor variant for name search")]
    IncompatibleCursor,
}

/// Name search result match
#[derive(Debug, Clone, serde::Serialize)]
pub struct NameSearchResult {
    /// The id of the entity
    pub entity_id: uuid::Uuid,
    /// The type of the entity
    pub entity_type: SearchEntityType,
    /// The name that was matched
    pub name: String,
    /// The timestamp used for cursor-based pagination
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl SearchCursorAttributes for NameSearchResult {
    fn entity_id(&self) -> uuid::Uuid {
        self.entity_id
    }

    fn updated_at(&self) -> chrono::DateTime<chrono::Utc> {
        self.updated_at
    }
}