From 31d9f0ea07b27149f040a6ae22ad0ac8271e4719 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 3 Aug 2026 11:41:05 -0500 Subject: [PATCH] refactor: Migrate away from KString --- Cargo.lock | 41 ++++++++++++++++++++++--------- crates/typos-cli/Cargo.toml | 2 +- crates/typos-cli/src/config.rs | 18 +++++++------- crates/typos-cli/src/file_type.rs | 21 +++++++++------- crates/typos-cli/src/policy.rs | 20 ++++++++------- 5 files changed, 63 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d06071de..a6cec33a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,15 @@ dependencies = [ "serde_core", ] +[[package]] +name = "castaway" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a" +dependencies = [ + "rustversion", +] + [[package]] name = "cfg-if" version = "1.0.4" @@ -277,6 +286,20 @@ dependencies = [ "colorchoice", ] +[[package]] +name = "compact_str" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79fcda08c33bb58b97008b2cdada6622500e949e060f5913361763121abd2416" +dependencies = [ + "castaway", + "cfg-if", + "itoa", + "serde", + "static_assertions", + "zmij", +] + [[package]] name = "condtype" version = "1.3.0" @@ -760,16 +783,6 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" -[[package]] -name = "kstring" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "558bf9508a558512042d3095138b1f7b8fe90c5467d94f9f1da28b3731c5dbd1" -dependencies = [ - "serde", - "static_assertions", -] - [[package]] name = "lazy_static" version = "1.5.0" @@ -1156,6 +1169,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + [[package]] name = "ryu" version = "1.0.23" @@ -1639,6 +1658,7 @@ dependencies = [ "clap-cargo", "clap-verbosity-flag", "colorchoice-clap", + "compact_str", "content_inspector", "derive_more", "derive_setters", @@ -1650,7 +1670,6 @@ dependencies = [ "human-panic", "ignore", "itertools", - "kstring", "log", "maplit", "proc-exit", diff --git a/crates/typos-cli/Cargo.toml b/crates/typos-cli/Cargo.toml index 35b2fc29..e5634977 100644 --- a/crates/typos-cli/Cargo.toml +++ b/crates/typos-cli/Cargo.toml @@ -65,7 +65,6 @@ derive_more = { version = "2.1", features = ["from", "display"] } derive_setters = "0.1" itertools = "0.15" serde_json = "1.0" -kstring = { version = "2.0.2", features = ["serde"] } typed-arena = "2.0.2" thread_local = "1.1.9" globset = "0.4.18" @@ -80,6 +79,7 @@ schemars = { version = "1.2.1", features = ["preserve_order","semver1"], optiona annotate-snippets = "0.12.13" terminal_size = "0.4.3" supports-unicode = "3.0.0" +compact_str = { version = "0.10.0", features = ["serde"] } [dev-dependencies] assert_fs = "1.1" diff --git a/crates/typos-cli/src/config.rs b/crates/typos-cli/src/config.rs index f5f75907..2598cf2e 100644 --- a/crates/typos-cli/src/config.rs +++ b/crates/typos-cli/src/config.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -use kstring::KString; +use compact_str::CompactString; use crate::file_type_specifics; @@ -260,7 +260,7 @@ pub struct TypeEngineConfig { feature = "unstable-schema", schemars(schema_with = "hashmap_string_t::") )] - pub patterns: HashMap, + pub patterns: HashMap, } impl TypeEngineConfig { @@ -269,7 +269,7 @@ impl TypeEngineConfig { for no_check_type in file_type_specifics::NO_CHECK_TYPES { patterns.insert( - KString::from(*no_check_type), + CompactString::const_new(no_check_type), GlobEngineConfig { extend_glob: Vec::new(), engine: EngineConfig { @@ -282,7 +282,7 @@ impl TypeEngineConfig { for (typ, dict_config) in file_type_specifics::TYPE_SPECIFIC_DICTS { patterns.insert( - KString::from(*typ), + CompactString::const_new(typ), GlobEngineConfig { extend_glob: Vec::new(), engine: EngineConfig { @@ -317,7 +317,7 @@ impl TypeEngineConfig { } } - pub fn patterns(&self) -> impl Iterator + use<> { + pub fn patterns(&self) -> impl Iterator + use<> { let mut engine = Self::from_defaults(); engine.update(self); engine.patterns.into_iter() @@ -331,7 +331,7 @@ impl TypeEngineConfig { #[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))] pub struct GlobEngineConfig { #[cfg_attr(feature = "unstable-schema", schemars(schema_with = "vec_string"))] - pub extend_glob: Vec, + pub extend_glob: Vec, #[serde(flatten)] pub engine: EngineConfig, } @@ -478,7 +478,7 @@ pub struct DictConfig { feature = "unstable-schema", schemars(schema_with = "hashmap_string_string") )] - pub extend_identifiers: HashMap, + pub extend_identifiers: HashMap, #[serde(with = "serde_regex")] #[cfg_attr(feature = "unstable-schema", schemars(schema_with = "vec_string"))] pub extend_ignore_words_re: Vec, @@ -486,7 +486,7 @@ pub struct DictConfig { feature = "unstable-schema", schemars(schema_with = "hashmap_string_string") )] - pub extend_words: HashMap, + pub extend_words: HashMap, } impl DictConfig { @@ -725,7 +725,7 @@ mod test { let mut actual = base; actual.update(&extended); - let expected: Vec = vec!["*.foo".into(), "*.bar".into()]; + let expected: Vec = vec!["*.foo".into(), "*.bar".into()]; assert_data_eq!(actual.extend_glob.into_json(), expected.into_json()); } diff --git a/crates/typos-cli/src/file_type.rs b/crates/typos-cli/src/file_type.rs index 277badcc..c3242142 100644 --- a/crates/typos-cli/src/file_type.rs +++ b/crates/typos-cli/src/file_type.rs @@ -1,11 +1,11 @@ use std::collections::BTreeMap; use std::path::Path; -use kstring::KString; +use compact_str::CompactString; #[derive(Default, Clone, Debug)] pub(crate) struct TypesBuilder { - definitions: BTreeMap>, + definitions: BTreeMap>, } impl TypesBuilder { @@ -18,8 +18,11 @@ impl TypesBuilder { crate::default_types::DEFAULT_TYPES .iter() .map(|(name, glob)| { - let name = KString::from(*name); - let globs = glob.iter().map(|s| (KString::from(*s), 0)).collect(); + let name = CompactString::const_new(name); + let globs = glob + .iter() + .map(|s| (CompactString::const_new(s), 0)) + .collect(); (name, globs) }), ); @@ -29,7 +32,7 @@ impl TypesBuilder { self.definitions.contains_key(name) } - pub(crate) fn add(&mut self, name: impl Into, glob: impl Into) { + pub(crate) fn add(&mut self, name: impl Into, glob: impl Into) { let name = name.into(); let glob = glob.into(); let weight = self.definitions.len(); @@ -56,7 +59,7 @@ impl TypesBuilder { .iter() .map(|(_, _, name, glob)| (*glob, *name)) .collect::>(); - let mut unique_definitions = BTreeMap::>::new(); + let mut unique_definitions = BTreeMap::>::new(); for (glob, name) in rev_definitions { unique_definitions .entry(name.clone()) @@ -111,15 +114,15 @@ enum GlobPart<'s> { #[derive(Default, Clone, Debug)] pub(crate) struct Types { - definitions: BTreeMap>, - glob_to_name: Vec, + definitions: BTreeMap>, + glob_to_name: Vec, set: globset::GlobSet, /// Temporary storage for globs that match. matches: std::sync::Arc>>>, } impl Types { - pub(crate) fn definitions(&self) -> &BTreeMap> { + pub(crate) fn definitions(&self) -> &BTreeMap> { &self.definitions } diff --git a/crates/typos-cli/src/policy.rs b/crates/typos-cli/src/policy.rs index f7e9e2c6..ca70e82e 100644 --- a/crates/typos-cli/src/policy.rs +++ b/crates/typos-cli/src/policy.rs @@ -1,5 +1,7 @@ +use compact_str::CompactString; + pub struct ConfigStorage { - arena: std::sync::Mutex>, + arena: std::sync::Mutex>, } impl ConfigStorage { @@ -19,7 +21,7 @@ impl ConfigStorage { self.arena .lock() .unwrap() - .alloc(kstring::KString::from_ref(other)) + .alloc(CompactString::from(other)) .as_str(), ) } @@ -81,7 +83,7 @@ impl<'s> ConfigEngine<'s> { pub fn file_types( &self, cwd: &std::path::Path, - ) -> &std::collections::BTreeMap> { + ) -> &std::collections::BTreeMap> { debug_assert!(cwd.is_absolute(), "{} is not absolute", cwd.display()); let dir = self .configs @@ -199,7 +201,7 @@ impl<'s> ConfigEngine<'s> { } } else { for glob in type_engine.extend_glob.iter() { - type_matcher.add(type_name.as_ref(), glob.as_ref()); + type_matcher.add(type_name.clone(), glob.clone()); } } @@ -314,7 +316,7 @@ impl Default for Intern { struct DirConfig { walk: usize, default: FileConfig, - types: std::collections::HashMap, + types: std::collections::HashMap, type_matcher: crate::file_type::Types, } @@ -397,7 +399,7 @@ mod test { let mut engine = ConfigEngine::new(&storage); engine.set_isolated(true); - let type_name = kstring::KString::from_static("toml"); + let type_name = CompactString::const_new("toml"); let config = crate::config::Config { default: crate::config::EngineConfig { @@ -453,7 +455,7 @@ mod test { let mut engine = ConfigEngine::new(&storage); engine.set_isolated(true); - let type_name = kstring::KString::from_static(NEVER_EXIST_TYPE); + let type_name = CompactString::const_new(NEVER_EXIST_TYPE); let config = crate::config::Config { type_: crate::config::TypeEngineConfig { @@ -493,7 +495,7 @@ mod test { let mut engine = ConfigEngine::new(&storage); engine.set_isolated(true); - let type_name = kstring::KString::from_static(NEVER_EXIST_TYPE); + let type_name = CompactString::const_new(NEVER_EXIST_TYPE); let config = crate::config::Config { default: crate::config::EngineConfig { @@ -527,7 +529,7 @@ mod test { let mut engine = ConfigEngine::new(&storage); engine.set_isolated(true); - let type_name = kstring::KString::from_static(NEVER_EXIST_TYPE); + let type_name = CompactString::const_new(NEVER_EXIST_TYPE); let config = crate::config::Config { default: crate::config::EngineConfig {