Skip to main content

deps_lsp/
lib.rs

1pub mod config;
2pub mod document;
3pub mod file_watcher;
4pub mod handlers;
5pub mod progress;
6pub mod server;
7
8#[cfg(test)]
9mod test_utils;
10
11use std::sync::Arc;
12
13pub use deps_core::{DepsError, EcosystemRegistry, HttpCache, Result};
14pub use server::Backend;
15
16/// Declares an ecosystem: re-exports types and registers at runtime.
17macro_rules! ecosystem {
18    ($feature:literal, $crate_name:ident, $ecosystem:ident, [$($types:ident),* $(,)?]) => {
19        #[cfg(feature = $feature)]
20        pub use $crate_name::{$ecosystem, $($types),*};
21    };
22}
23
24/// Registers ecosystem if feature is enabled.
25macro_rules! register {
26    ($feature:literal, $ecosystem:ident, $registry:expr, $cache:expr) => {
27        #[cfg(feature = $feature)]
28        $registry.register(Arc::new($ecosystem::new(Arc::clone($cache))));
29    };
30}
31
32// =============================================================================
33// Ecosystems — to add new: 1) feature in Cargo.toml  2) add ecosystem!() + register!()
34// =============================================================================
35
36ecosystem!(
37    "cargo",
38    deps_cargo,
39    CargoEcosystem,
40    [
41        CargoParser,
42        CargoVersion,
43        CrateInfo,
44        CratesIoRegistry,
45        DependencySection,
46        DependencySource,
47        ParseResult,
48        ParsedDependency,
49        parse_cargo_toml,
50    ]
51);
52
53ecosystem!(
54    "npm",
55    deps_npm,
56    NpmEcosystem,
57    [
58        NpmDependency,
59        NpmDependencySection,
60        NpmPackage,
61        NpmParseResult,
62        NpmRegistry,
63        NpmVersion,
64        parse_package_json,
65    ]
66);
67
68ecosystem!(
69    "pypi",
70    deps_pypi,
71    PypiEcosystem,
72    [
73        PypiDependency,
74        PypiDependencySection,
75        PypiParser,
76        PypiRegistry,
77        PypiVersion,
78    ]
79);
80
81ecosystem!(
82    "go",
83    deps_go,
84    GoEcosystem,
85    [
86        GoDependency,
87        GoDirective,
88        GoParseResult,
89        GoRegistry,
90        GoVersion,
91        parse_go_mod,
92    ]
93);
94
95ecosystem!(
96    "bundler",
97    deps_bundler,
98    BundlerEcosystem,
99    [
100        BundlerDependency,
101        BundlerParseResult,
102        BundlerVersion,
103        DependencyGroup,
104        GemInfo,
105        GemfileLockParser,
106        RubyGemsRegistry,
107        parse_gemfile,
108    ]
109);
110
111/// Registers all enabled ecosystems.
112pub fn register_ecosystems(registry: &EcosystemRegistry, cache: Arc<HttpCache>) {
113    register!("cargo", CargoEcosystem, registry, &cache);
114    register!("npm", NpmEcosystem, registry, &cache);
115    register!("pypi", PypiEcosystem, registry, &cache);
116    register!("go", GoEcosystem, registry, &cache);
117    register!("bundler", BundlerEcosystem, registry, &cache);
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[test]
125    fn test_register_ecosystems() {
126        let registry = Arc::new(EcosystemRegistry::new());
127        let cache = Arc::new(HttpCache::new());
128        register_ecosystems(&registry, Arc::clone(&cache));
129
130        #[cfg(feature = "cargo")]
131        assert!(registry.get("cargo").is_some());
132        #[cfg(feature = "npm")]
133        assert!(registry.get("npm").is_some());
134        #[cfg(feature = "pypi")]
135        assert!(registry.get("pypi").is_some());
136        #[cfg(feature = "go")]
137        assert!(registry.get("go").is_some());
138        #[cfg(feature = "bundler")]
139        assert!(registry.get("bundler").is_some());
140    }
141}