deps_core/parser.rs
1use crate::error::Result;
2use tower_lsp::lsp_types::{Range, Url};
3
4/// Generic manifest parser interface.
5///
6/// Implementors parse ecosystem-specific manifest files (Cargo.toml, package.json, etc.)
7/// and extract dependency information with precise LSP positions.
8pub trait ManifestParser: Send + Sync {
9 /// Parsed dependency type for this ecosystem.
10 type Dependency: DependencyInfo + Clone + Send + Sync;
11
12 /// Parse result containing dependencies and optional workspace information.
13 type ParseResult: ParseResultInfo<Dependency = Self::Dependency> + Send;
14
15 /// Parses a manifest file and extracts all dependencies with positions.
16 ///
17 /// # Errors
18 ///
19 /// Returns error if:
20 /// - Manifest syntax is invalid
21 /// - File path cannot be determined from URL
22 fn parse(&self, content: &str, doc_uri: &Url) -> Result<Self::ParseResult>;
23}
24
25/// Dependency information trait.
26///
27/// All parsed dependencies must implement this for generic handler access.
28pub trait DependencyInfo {
29 /// Dependency name (package/crate name).
30 fn name(&self) -> &str;
31
32 /// LSP range of the dependency name in the source file.
33 fn name_range(&self) -> Range;
34
35 /// Version requirement string (e.g., "^1.0", "~2.3.4").
36 fn version_requirement(&self) -> Option<&str>;
37
38 /// LSP range of the version string (for inlay hints positioning).
39 fn version_range(&self) -> Option<Range>;
40
41 /// Dependency source (registry, git, path).
42 fn source(&self) -> DependencySource;
43
44 /// Feature flags requested (Cargo-specific, empty for npm).
45 fn features(&self) -> &[String] {
46 &[]
47 }
48}
49
50/// Parse result information trait.
51pub trait ParseResultInfo {
52 type Dependency: DependencyInfo;
53
54 /// All dependencies found in the manifest.
55 fn dependencies(&self) -> &[Self::Dependency];
56
57 /// Workspace root path (for monorepo support).
58 fn workspace_root(&self) -> Option<&std::path::Path>;
59}
60
61/// Dependency source (shared across ecosystems).
62#[derive(Debug, Clone, PartialEq)]
63pub enum DependencySource {
64 /// Dependency from default registry (crates.io, npm, PyPI).
65 Registry,
66 /// Dependency from Git repository.
67 Git { url: String, rev: Option<String> },
68 /// Dependency from local filesystem path.
69 Path { path: String },
70}