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