Skip to main content

ayame_spell_core/
lib.rs

1//! Core engine for [ayame-spell](https://github.com/hjosugi/ayame-spell):
2//! a fast, low-noise spell checker for code and prose, for English and
3//! Japanese.
4//!
5//! The engine has two English checking modes:
6//!
7//! - **corrections** (default): only known misspellings are flagged, using a
8//!   curated typo → fix table. Near-zero false positives; safe for CI.
9//! - **dictionary**: additionally flags words missing from configured
10//!   wordlists (project words, global words, registry dictionaries).
11//!
12//! Japanese checks are independent of the mode: katakana notation variants
13//! (表記ゆれ), fullwidth alphanumerics, halfwidth katakana, and fullwidth
14//! spaces.
15
16pub mod checker;
17pub mod config;
18pub mod corrections;
19pub mod dictionary;
20pub mod issue;
21pub mod japanese;
22pub mod registry_lock;
23mod syntax;
24pub mod tokenizer;
25
26pub use checker::Checker;
27pub use config::{Config, LoadedConfig, Mode};
28pub use issue::{Issue, IssueKind};
29
30use std::path::PathBuf;
31
32fn configured_dir(variable: &str) -> Option<PathBuf> {
33    std::env::var_os(variable)
34        .filter(|value| !value.is_empty())
35        .map(PathBuf::from)
36}
37
38/// Application cache directory (`~/.cache/ayame-spell`).
39pub fn cache_dir() -> Option<PathBuf> {
40    configured_dir("AYAME_SPELL_CACHE_DIR")
41        .or_else(|| dirs::cache_dir().map(|directory| directory.join("ayame-spell")))
42}
43
44/// Directory where registry dictionaries are cached
45/// (`~/.cache/ayame-spell/dicts`).
46pub fn registry_cache_dir() -> Option<PathBuf> {
47    cache_dir().map(|directory| directory.join("dicts"))
48}
49
50/// Path of the cached registry dictionary `name`, if the cache directory is
51/// known. The file may or may not exist yet.
52pub fn registry_cache_path(name: &str) -> Option<PathBuf> {
53    registry_cache_dir().map(|d| d.join(format!("{name}.txt")))
54}
55
56/// Cached registry index used by offline and dynamic completion.
57pub fn registry_index_cache_path() -> Option<PathBuf> {
58    cache_dir().map(|directory| directory.join("index.json"))
59}
60
61/// Directory for incremental per-file scan results.
62pub fn scan_cache_dir() -> Option<PathBuf> {
63    cache_dir().map(|directory| directory.join("scan"))
64}
65
66/// Cached output from `words collect`, used by dynamic completion.
67pub fn completion_words_cache_path() -> Option<PathBuf> {
68    cache_dir().map(|directory| directory.join("completion-words.json"))
69}
70
71/// Directory for global (per-user) configuration
72/// (`~/.config/ayame-spell`).
73pub fn global_config_dir() -> Option<PathBuf> {
74    configured_dir("AYAME_SPELL_CONFIG_DIR")
75        .or_else(|| dirs::config_dir().map(|d| d.join("ayame-spell")))
76}
77
78/// Path of the global word list (`~/.config/ayame-spell/words.txt`).
79pub fn global_words_path() -> Option<PathBuf> {
80    global_config_dir().map(|d| d.join("words.txt"))
81}