From 5c133e24c34b63cd6f76583359437c5e0c0579df Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 29 Sep 2026 21:23:14 +0200 Subject: [PATCH] refactor: isolate core and lsp crates --- AGENTS.md | 10 +- Cargo.lock | 34 +++++- Cargo.toml | 13 ++- crates/john-slop-core/Cargo.toml | 22 ++++ {src => crates/john-slop-core/src}/config.rs | 4 +- .../john-slop-core/src}/detector.rs | 0 .../john-slop-core/src}/diagnostic.rs | 0 {src => crates/john-slop-core/src}/error.rs | 0 .../john-slop-core/src}/input/archive.rs | 0 .../john-slop-core/src}/input/file.rs | 0 .../john-slop-core/src}/input/mod.rs | 0 .../john-slop-core/src}/input/tests.rs | 0 .../john-slop-core/src}/input/walk.rs | 0 crates/john-slop-core/src/lib.rs | 104 +++++++++++++++++ {src => crates/john-slop-core/src}/model.rs | 0 .../john-slop-core/src}/replacement.rs | 0 crates/john-slop-lsp/Cargo.toml | 17 +++ src/lsp.rs => crates/john-slop-lsp/src/lib.rs | 2 +- docs/architecture.md | 20 +++- src/lib.rs | 105 +----------------- tasks.md | 2 +- 21 files changed, 207 insertions(+), 126 deletions(-) create mode 100644 crates/john-slop-core/Cargo.toml rename {src => crates/john-slop-core/src}/config.rs (99%) rename {src => crates/john-slop-core/src}/detector.rs (100%) rename {src => crates/john-slop-core/src}/diagnostic.rs (100%) rename {src => crates/john-slop-core/src}/error.rs (100%) rename {src => crates/john-slop-core/src}/input/archive.rs (100%) rename {src => crates/john-slop-core/src}/input/file.rs (100%) rename {src => crates/john-slop-core/src}/input/mod.rs (100%) rename {src => crates/john-slop-core/src}/input/tests.rs (100%) rename {src => crates/john-slop-core/src}/input/walk.rs (100%) create mode 100644 crates/john-slop-core/src/lib.rs rename {src => crates/john-slop-core/src}/model.rs (100%) rename {src => crates/john-slop-core/src}/replacement.rs (100%) create mode 100644 crates/john-slop-lsp/Cargo.toml rename src/lsp.rs => crates/john-slop-lsp/src/lib.rs (99%) diff --git a/AGENTS.md b/AGENTS.md index 6046370..c0f84c2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -26,16 +26,14 @@ layout is: ```text src/ - lib.rs public library facade - config.rs typed loading, validation, and provenance - diagnostic.rs miette conversion and presentation-neutral spans - input/ files, directories, archives, and ignore policy - detection/ engine, score aggregation, and rule implementations - replacement.rs pure replacement/edit generation + lib.rs compatibility facade over workspace libraries bin/ john-slop.rs command-line detector john-slop-fmt.rs replacement formatter john-slop-lsp.rs stdio language server +crates/ + john-slop-core/src/ config, input, detection, diagnostics, and edits + john-slop-lsp/src/ protocol state, UTF-16 mapping, and code actions assets/ config.toml embedded replacement defaults heuristics.toml embedded detection rules diff --git a/Cargo.lock b/Cargo.lock index 1e43ff1..a6a079f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -697,19 +697,46 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "john-slop-core" +version = "0.1.0" +dependencies = [ + "directories", + "figment", + "globset", + "miette", + "regex", + "serde", + "tempfile", + "thiserror", + "toml 1.1.6+spec-1.1.0", + "walkdir", + "zip", +] + +[[package]] +name = "john-slop-lsp" +version = "0.1.0" +dependencies = [ + "john-slop-core", + "serde_json", + "thiserror", + "tokio", + "tower-lsp-server", +] + [[package]] name = "john_slop" version = "0.1.0" dependencies = [ "clap", "criterion", - "directories", - "figment", "futures", "globset", "insta", + "john-slop-core", + "john-slop-lsp", "miette", - "regex", "serde", "serde_json", "sha2", @@ -720,7 +747,6 @@ dependencies = [ "toml 1.1.6+spec-1.1.0", "tower", "tower-lsp-server", - "walkdir", "zip", ] diff --git a/Cargo.toml b/Cargo.toml index 18e8f5e..bfb1c1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,8 @@ +[workspace] +members = ["crates/john-slop-core", "crates/john-slop-lsp"] +default-members = [".", "crates/john-slop-core", "crates/john-slop-lsp"] +resolver = "3" + [package] name = "john_slop" version = "0.1.0" @@ -25,11 +30,10 @@ fancy-diagnostics = ["miette/fancy"] [dependencies] clap = { version = "4.6.7", default-features = false, features = ["derive", "error-context", "help", "std", "suggestions", "usage"] } -directories = "6.0.0" -figment = { version = "0.10.19", default-features = false, features = ["toml"] } globset = { version = "0.4.20", default-features = false } +john-slop-core = { path = "crates/john-slop-core" } +john-slop-lsp-engine = { package = "john-slop-lsp", path = "crates/john-slop-lsp" } miette = { version = "7.6.0", default-features = false, features = ["derive"] } -regex = "1.13.1" serde = { version = "1.0.229", features = ["derive"] } serde_json = "1.0.151" similar = "2.7.0" @@ -38,8 +42,6 @@ thiserror = "2.0.21" tokio = { version = "1.53.1", default-features = false, features = ["io-std", "macros", "rt-multi-thread", "sync"] } toml = "1.1.6" tower-lsp-server = { version = "0.23.0", default-features = false, features = ["runtime-tokio"] } -walkdir = "2.5.0" -zip = { version = "8.6.0", default-features = false, features = ["deflate-flate2-zlib-rs"] } [dev-dependencies] criterion = { version = "0.8.2", default-features = false } @@ -47,6 +49,7 @@ futures = { version = "0.3.34", default-features = false, features = ["std"] } insta = { version = "1.48.0", default-features = false } sha2 = "0.11.0" tower = { version = "0.5.3", default-features = false, features = ["util"] } +zip = { version = "8.6.0", default-features = false, features = ["deflate-flate2-zlib-rs"] } [[bench]] name = "core" diff --git a/crates/john-slop-core/Cargo.toml b/crates/john-slop-core/Cargo.toml new file mode 100644 index 0000000..365dd58 --- /dev/null +++ b/crates/john-slop-core/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "john-slop-core" +version = "0.1.0" +edition = "2024" +rust-version = "1.98.1" +license = "MIT" +repository = "https://git.dandaedre.com/idle-org/john_slop" + +[dependencies] +directories = "6.0.0" +figment = { version = "0.10.19", default-features = false, features = ["toml"] } +globset = { version = "0.4.20", default-features = false } +miette = { version = "7.6.0", default-features = false, features = ["derive"] } +regex = "1.13.1" +serde = { version = "1.0.229", features = ["derive"] } +thiserror = "2.0.21" +walkdir = "2.5.0" +zip = { version = "8.6.0", default-features = false, features = ["deflate-flate2-zlib-rs"] } + +[dev-dependencies] +tempfile = "3.27.0" +toml = "1.1.6" diff --git a/src/config.rs b/crates/john-slop-core/src/config.rs similarity index 99% rename from src/config.rs rename to crates/john-slop-core/src/config.rs index 3affadf..4b49de4 100644 --- a/src/config.rs +++ b/crates/john-slop-core/src/config.rs @@ -13,8 +13,8 @@ use serde::{Deserialize, Serialize}; use crate::{Category, FindingSeverity}; -const EMBEDDED_CONFIG: &str = include_str!("../assets/config.toml"); -const EMBEDDED_HEURISTICS: &str = include_str!("../assets/heuristics.toml"); +const EMBEDDED_CONFIG: &str = include_str!("../../../assets/config.toml"); +const EMBEDDED_HEURISTICS: &str = include_str!("../../../assets/heuristics.toml"); /// A validated named configuration without path syntax or a file extension. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] diff --git a/src/detector.rs b/crates/john-slop-core/src/detector.rs similarity index 100% rename from src/detector.rs rename to crates/john-slop-core/src/detector.rs diff --git a/src/diagnostic.rs b/crates/john-slop-core/src/diagnostic.rs similarity index 100% rename from src/diagnostic.rs rename to crates/john-slop-core/src/diagnostic.rs diff --git a/src/error.rs b/crates/john-slop-core/src/error.rs similarity index 100% rename from src/error.rs rename to crates/john-slop-core/src/error.rs diff --git a/src/input/archive.rs b/crates/john-slop-core/src/input/archive.rs similarity index 100% rename from src/input/archive.rs rename to crates/john-slop-core/src/input/archive.rs diff --git a/src/input/file.rs b/crates/john-slop-core/src/input/file.rs similarity index 100% rename from src/input/file.rs rename to crates/john-slop-core/src/input/file.rs diff --git a/src/input/mod.rs b/crates/john-slop-core/src/input/mod.rs similarity index 100% rename from src/input/mod.rs rename to crates/john-slop-core/src/input/mod.rs diff --git a/src/input/tests.rs b/crates/john-slop-core/src/input/tests.rs similarity index 100% rename from src/input/tests.rs rename to crates/john-slop-core/src/input/tests.rs diff --git a/src/input/walk.rs b/crates/john-slop-core/src/input/walk.rs similarity index 100% rename from src/input/walk.rs rename to crates/john-slop-core/src/input/walk.rs diff --git a/crates/john-slop-core/src/lib.rs b/crates/john-slop-core/src/lib.rs new file mode 100644 index 0000000..4a60171 --- /dev/null +++ b/crates/john-slop-core/src/lib.rs @@ -0,0 +1,104 @@ +//! Shared analysis library for John Slop's command-line, formatter, and LSP +//! adapters. + +#![warn(missing_docs)] + +use std::path::Path; + +mod config; +mod detector; +mod diagnostic; +mod error; +mod input; +mod model; +mod replacement; + +pub use config::{ + Config, ConfigError, ConfigLayerError, ConfigLoader, ConfigName, ConfigNameError, + ConfigOverrides, ConfigPaths, ConfigValidationError, ContentKind, EvidenceThresholds, + HeuristicRule, InputLimits, InputOverrides, MatcherType, ReplacementRule, RuleScope, + ThresholdOverrides, +}; +pub use detector::{AnalysisContext, Detector, DetectorBuildError, Rule}; +pub use diagnostic::FindingDiagnostic; +pub use error::{InputError, InputResult}; +pub use input::{Document, Documents, read_stdin}; +pub use model::{ + Analysis, AnalysisSummary, Category, EvidenceBand, EvidenceScore, Finding, FindingSeverity, + RuleId, SeverityCounts, SourceId, TextEdit, +}; + +/// Deprecated name for [`EvidenceScore`]. +#[deprecated(since = "0.1.0", note = "use EvidenceScore")] +pub type Confidence = EvidenceScore; + +/// Deprecated name for [`EvidenceBand`]. +#[deprecated(since = "0.1.0", note = "use EvidenceBand")] +pub type ConfidenceBand = EvidenceBand; + +/// Deprecated name for [`EvidenceThresholds`]. +#[deprecated(since = "0.1.0", note = "use EvidenceThresholds")] +pub type ConfidenceThresholds = EvidenceThresholds; +pub use replacement::ReplacementError; + +/// Load embedded defaults plus optional platform user configuration. +pub fn load_config() -> Result { + ConfigLoader::discover(None, None)?.load() +} + +/// Analyze a UTF-8 source snapshot with the resolved configuration. +pub fn analyze_source(config: &Config, source: &str) -> Analysis { + Detector::new(config) + .expect("validated configuration must build a detector") + .analyze(source) +} + +/// Plan safe ASCII replacements without modifying the source. +pub fn plan_replacements(config: &Config, source: &str) -> Vec { + replacement::plan(config, source) +} + +/// Validate and apply text edits against their original UTF-8 source snapshot. +pub fn apply_replacements<'source>( + source: &'source str, + edits: &[TextEdit], +) -> Result, ReplacementError> { + replacement::apply(source, edits) +} + +/// Read documents from a file, directory, or supported ZIP-based container. +pub fn read_inputs(config: &Config, path: &Path) -> InputResult { + input::read(path, &config.input) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn facade_uses_one_configuration_for_analysis_and_edits() { + let config = load_config().unwrap(); + let source = "review—this"; + let analysis = analyze_source(&config, source); + let edits = plan_replacements(&config, source); + + assert_eq!(analysis.findings.len(), 1); + assert_eq!(analysis.findings[0].fix.as_ref(), edits.first()); + } + + #[test] + #[allow(deprecated)] + fn legacy_score_type_aliases_remain_source_compatible() { + let score: Confidence = EvidenceScore::from_score(42); + let band: ConfidenceBand = EvidenceBand::Medium; + let thresholds: ConfidenceThresholds = EvidenceThresholds { + low: 20, + medium: 50, + high: 75, + }; + + assert_eq!(score.score(), 42); + assert_eq!(band, EvidenceBand::Medium); + assert_eq!(thresholds.low, 20); + } +} diff --git a/src/model.rs b/crates/john-slop-core/src/model.rs similarity index 100% rename from src/model.rs rename to crates/john-slop-core/src/model.rs diff --git a/src/replacement.rs b/crates/john-slop-core/src/replacement.rs similarity index 100% rename from src/replacement.rs rename to crates/john-slop-core/src/replacement.rs diff --git a/crates/john-slop-lsp/Cargo.toml b/crates/john-slop-lsp/Cargo.toml new file mode 100644 index 0000000..710b0bb --- /dev/null +++ b/crates/john-slop-lsp/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "john-slop-lsp" +version = "0.1.0" +edition = "2024" +rust-version = "1.98.1" +license = "MIT" +repository = "https://git.dandaedre.com/idle-org/john_slop" + +[lib] +name = "john_slop_lsp_engine" + +[dependencies] +john-slop-core = { path = "../john-slop-core" } +serde_json = "1.0.151" +thiserror = "2.0.21" +tokio = { version = "1.53.1", default-features = false, features = ["sync"] } +tower-lsp-server = { version = "0.23.0", default-features = false, features = ["runtime-tokio"] } diff --git a/src/lsp.rs b/crates/john-slop-lsp/src/lib.rs similarity index 99% rename from src/lsp.rs rename to crates/john-slop-lsp/src/lib.rs index 61d1a84..6cf8565 100644 --- a/src/lsp.rs +++ b/crates/john-slop-lsp/src/lib.rs @@ -18,7 +18,7 @@ use tower_lsp_server::{ }, }; -use crate::{Config, ConfigLoader, plan_replacements}; +use john_slop_core::{Config, ConfigLoader, plan_replacements}; /// Failure to convert a canonical UTF-8 byte offset into an LSP position. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] diff --git a/docs/architecture.md b/docs/architecture.md index 440afc8..f1b8ebb 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,8 +1,8 @@ # Architecture -John Slop keeps detection and replacement behavior in a library. Executables are -thin adapters responsible for argument parsing, presentation, and process exit -statuses. +John Slop is a workspace with a compatibility facade and two reusable libraries. +Executables remain thin adapters responsible for argument parsing, presentation, +and process exit statuses. The current flow is: @@ -14,7 +14,16 @@ path -> input stream -> Document -> analysis -> Analysis + Finding -> LSP UTF-16 edit ``` -The main modules have deliberately narrow responsibilities: +The crate boundary separates dependency families: + +- `john-slop-core` owns configuration, input, detection, diagnostics, + replacement planning, and presentation-neutral domain values; +- `john-slop-lsp` owns asynchronous editor state and protocol conversion while + depending only on `john-slop-core`; +- the root `john_slop` package re-exports both libraries to preserve its public + API and distributes the detector, formatter, and LSP binaries. + +The modules within those crates retain deliberately narrow responsibilities: - `config` owns the validated embedded replacement catalog and input limits; - `input` streams files, directory entries, and archive members as documents; @@ -39,7 +48,8 @@ so language and path filters behave the same inside and outside archives. ## Public facade -The crate root exposes core entry points and presentation-neutral types: +The root compatibility facade and `john-slop-core` expose the same core entry +points and presentation-neutral types: - `load_config` resolves the configuration embedded in the current build; - `read_inputs` returns a streaming iterator of per-document results; diff --git a/src/lib.rs b/src/lib.rs index b114981..23c9480 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,105 +1,6 @@ -//! Shared analysis library for John Slop's command-line, formatter, and LSP -//! adapters. +//! Compatibility facade for John Slop's reusable analysis and LSP crates. #![warn(missing_docs)] -use std::path::Path; - -mod config; -mod detector; -mod diagnostic; -mod error; -mod input; -pub mod lsp; -mod model; -mod replacement; - -pub use config::{ - Config, ConfigError, ConfigLayerError, ConfigLoader, ConfigName, ConfigNameError, - ConfigOverrides, ConfigPaths, ConfigValidationError, ContentKind, EvidenceThresholds, - HeuristicRule, InputLimits, InputOverrides, MatcherType, ReplacementRule, RuleScope, - ThresholdOverrides, -}; -pub use detector::{AnalysisContext, Detector, DetectorBuildError, Rule}; -pub use diagnostic::FindingDiagnostic; -pub use error::{InputError, InputResult}; -pub use input::{Document, Documents, read_stdin}; -pub use model::{ - Analysis, AnalysisSummary, Category, EvidenceBand, EvidenceScore, Finding, FindingSeverity, - RuleId, SeverityCounts, SourceId, TextEdit, -}; - -/// Deprecated name for [`EvidenceScore`]. -#[deprecated(since = "0.1.0", note = "use EvidenceScore")] -pub type Confidence = EvidenceScore; - -/// Deprecated name for [`EvidenceBand`]. -#[deprecated(since = "0.1.0", note = "use EvidenceBand")] -pub type ConfidenceBand = EvidenceBand; - -/// Deprecated name for [`EvidenceThresholds`]. -#[deprecated(since = "0.1.0", note = "use EvidenceThresholds")] -pub type ConfidenceThresholds = EvidenceThresholds; -pub use replacement::ReplacementError; - -/// Load embedded defaults plus optional platform user configuration. -pub fn load_config() -> Result { - ConfigLoader::discover(None, None)?.load() -} - -/// Analyze a UTF-8 source snapshot with the resolved configuration. -pub fn analyze_source(config: &Config, source: &str) -> Analysis { - Detector::new(config) - .expect("validated configuration must build a detector") - .analyze(source) -} - -/// Plan safe ASCII replacements without modifying the source. -pub fn plan_replacements(config: &Config, source: &str) -> Vec { - replacement::plan(config, source) -} - -/// Validate and apply text edits against their original UTF-8 source snapshot. -pub fn apply_replacements<'source>( - source: &'source str, - edits: &[TextEdit], -) -> Result, ReplacementError> { - replacement::apply(source, edits) -} - -/// Read documents from a file, directory, or supported ZIP-based container. -pub fn read_inputs(config: &Config, path: &Path) -> InputResult { - input::read(path, &config.input) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn facade_uses_one_configuration_for_analysis_and_edits() { - let config = load_config().unwrap(); - let source = "review—this"; - let analysis = analyze_source(&config, source); - let edits = plan_replacements(&config, source); - - assert_eq!(analysis.findings.len(), 1); - assert_eq!(analysis.findings[0].fix.as_ref(), edits.first()); - } - - #[test] - #[allow(deprecated)] - fn legacy_score_type_aliases_remain_source_compatible() { - let score: Confidence = EvidenceScore::from_score(42); - let band: ConfidenceBand = EvidenceBand::Medium; - let thresholds: ConfidenceThresholds = EvidenceThresholds { - low: 20, - medium: 50, - high: 75, - }; - - assert_eq!(score.score(), 42); - assert_eq!(band, EvidenceBand::Medium); - assert_eq!(thresholds.low, 20); - } -} +pub use john_slop_core::*; +pub use john_slop_lsp_engine as lsp; diff --git a/tasks.md b/tasks.md index 5e2e306..67c24ca 100644 --- a/tasks.md +++ b/tasks.md @@ -320,7 +320,7 @@ floor; and the generated badges are current. - [x] Capture reproducible baseline cold and incremental check times from `milestone-11`, identify dependency ownership, and choose the smallest crate boundary capable of excluding async/LSP dependencies from core builds. -- [ ] Extract the reusable detector/configuration/input implementation into a +- [x] Extract the reusable detector/configuration/input implementation into a `john-slop-core` workspace crate and the protocol engine into a `john-slop-lsp` workspace crate. Keep the root package as the compatibility facade and binary distribution, preserving public behavior and all three