Skip to main content

yuru_core/
lib.rs

1//! Core candidate indexing, fuzzy matching, ranking, and source-span data types
2//! for Yuru.
3//!
4//! This crate is intentionally language-neutral. Japanese, Korean, and Chinese
5//! phonetic keys are supplied by separate backend crates through
6//! [`LanguageBackend`].
7
8pub mod candidate;
9pub mod config;
10pub mod fzf_query;
11pub mod matcher;
12pub mod normalize;
13pub mod query;
14pub mod rank;
15pub mod stats;
16
17use std::fmt;
18use std::str::FromStr;
19
20pub use candidate::{
21    build_candidate, build_index, dedup_and_limit_keys, Candidate, SearchKey, SourceSpan,
22};
23pub use config::{MatcherAlgo, SearchConfig, Tiebreak};
24pub use matcher::{
25    match_positions, score_exact_text, score_text, ExactMatcher, GreedyMatcher, MatchPositions,
26    MatcherBackend, NucleoMatcher,
27};
28pub use query::{
29    base_query_variants, dedup_and_limit_variants, key_kind_allowed, PlainBackend, QueryVariant,
30};
31pub use rank::{search, search_with_stats, ScoredCandidate};
32pub use stats::SearchStats;
33
34#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
35pub enum LangMode {
36    Plain,
37    Japanese,
38    Korean,
39    Chinese,
40}
41
42impl fmt::Display for LangMode {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            LangMode::Plain => f.write_str("plain"),
46            LangMode::Japanese => f.write_str("ja"),
47            LangMode::Korean => f.write_str("ko"),
48            LangMode::Chinese => f.write_str("zh"),
49        }
50    }
51}
52
53impl FromStr for LangMode {
54    type Err = String;
55
56    fn from_str(value: &str) -> Result<Self, Self::Err> {
57        match value {
58            "plain" => Ok(LangMode::Plain),
59            "ja" | "japanese" => Ok(LangMode::Japanese),
60            "ko" | "korean" => Ok(LangMode::Korean),
61            "zh" | "chinese" => Ok(LangMode::Chinese),
62            other => Err(format!("unsupported language mode: {other}")),
63        }
64    }
65}
66
67#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
68pub enum KeyKind {
69    Original,
70    Normalized,
71    KanaReading,
72    RomajiReading,
73    PinyinFull,
74    PinyinJoined,
75    PinyinInitials,
76    KoreanRomanized,
77    KoreanInitials,
78    KoreanKeyboard,
79    LearnedAlias,
80}
81
82#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
83pub enum QueryVariantKind {
84    Original,
85    Normalized,
86    Kana,
87    RomajiToKana,
88    Pinyin,
89    Initials,
90}
91
92pub trait LanguageBackend: Send + Sync {
93    fn mode(&self) -> LangMode;
94
95    fn normalize_candidate(&self, text: &str) -> String {
96        normalize::normalize(text)
97    }
98
99    fn build_candidate_keys(&self, text: &str) -> Vec<SearchKey>;
100
101    fn expand_query(&self, query: &str) -> Vec<QueryVariant>;
102}