rust-meth-lib 0.4.0

Core library for rust-meth: discover methods on Rust types via rust-analyzer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! A utility for generating ephemeral, minimal Cargo projects ("probes") used to query
//! Language Server Protocol (LSP) intelligence like autocompletions or go-to-definitions.
//!
//! A `Probe` creates a temporary directory containing a valid Cargo package with a single source file.
//! The source file declares an isolated variable statement `let _x: TYPE = todo!();` followed by a target
//! interaction point (such as `_x.` or `_x.method()`).

pub(crate) mod cache;

use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{
    Arc,
    atomic::{AtomicU64, Ordering},
};

pub use cache::{
    CacheEntry, PersistentCacheEntry, cache_entries, cache_key_hash, clear_persistent_cache,
    clear_probe_cache, persistent_cache_dir, persistent_cache_entries, ra_version,
};
use cache::{CacheKey, CachedProbe, global_cache, load_persistent_probe, save_persistent_probe};

// -- Counter --

/// Global atomic counter ensuring that concurrently generated probe projects
/// receive unique names within the OS temporary directory.
static PROBE_COUNTER: AtomicU64 = AtomicU64::new(0);

// Preamble added to every probe file so common std types resolve without
// the user needing to fully qualify them (e.g. `HashMap` not `std::collections::HashMap`).
const PREAMBLE: &str = "\
#![allow(unused_imports)]
use std::collections::*;
use std::sync::*;
use std::cell::*;
use std::rc::Rc;
use std::io::{self, Read, Write, BufRead};
use std::fmt;
use std::ops::*;
use std::path::{Path, PathBuf};
";

/// Represents an ephemeral Cargo project written to disk for LSP interrogation.
///
/// Backed by a cache [`Arc<CachedProbe>`] the directory is only deleted when
/// both the cache entry and this `Probe` are dropped.
pub struct Probe {
    /// Shared ownership of the underlying cached probe data.
    #[allow(dead_code)]
    inner: Arc<CachedProbe>,
    /// The 0-indexed line number in `src/main.rs` pointing to the target interaction point (the dot trigger).
    pub dot_line: u32,
    /// The 0-indexed character/column offset pointing exactly after the dot (`_x.`) in `src/main.rs`.
    pub dot_col: u32,
    /// The absolute path to the root directory of the temporary Cargo project.
    pub dir: PathBuf,
    /// The absolute path to the generated `src/main.rs` file.
    pub src_path: PathBuf,
}

impl Probe {
    /// Creates a new probe project without dependencies (for stdlib types).
    ///
    /// # Errors
    ///
    /// Returns an [`std::io::Error`] if creating the underlying probe project directory
    /// or writing its files fails.
    pub fn new(type_name: &str) -> std::io::Result<Self> {
        Self::create_probe(type_name, None, None, None)
    }

    /// Creates a new probe project with optional dependencies (for 3rd party crates).
    ///
    /// # Arguments
    /// * `type_name` - The Rust type to query (e.g., "`Vec<u8>`", "`serde_json::Value`")
    /// * `deps` - Optional TOML dependencies section (e.g., "`serde_json` = \"1.0\"")
    ///
    /// # Errors
    ///
    /// Returns an [`std::io::Error`] if generating the probe files or writing the dependency
    /// configuration fails.
    pub fn new_with_deps(type_name: &str, deps: Option<&str>) -> std::io::Result<Self> {
        Self::create_probe(type_name, None, deps, None)
    }

    /// Creates a probe project with optional dependencies and persistent caching.
    ///
    /// Passing `ra_path` enables the persistent cache, the probe directory is saved
    /// to `$XDG_CACHE_HOME/rust-meth/probes/` and reused across process restarts,
    /// skipping Cargo resolution on subsequent calls.
    ///
    /// ## Errors
    ///
    /// Errors on I/O failures, Cache/File System errors
    pub fn new_with_deps_cached(
        type_name: &str,
        deps: Option<&str>,
        ra_path: &Path,
    ) -> std::io::Result<Self> {
        Self::create_probe(type_name, None, deps, Some(ra_path))
    }

    /// Creates a probe file with `_x.METHOD_NAME()` for go-to-definition queries.
    /// The cursor position points at the start of the method name.
    ///
    /// # Errors
    ///
    /// Returns an [`std::io::Error`] if the workspace initialization or file creation fails
    /// on disk.
    pub fn for_definition(type_name: &str, method_name: &str) -> std::io::Result<Self> {
        Self::create_probe(type_name, Some(method_name), None, None)
    }

    /// Creates a probe file for go-to-definition with custom dependencies.
    ///
    /// # Errors
    ///
    /// Returns an [`std::io::Error`] if the underlying project boilerplate, file buffers,
    /// or custom dependency sections cannot be written.
    pub fn for_definition_with_deps(
        type_name: &str,
        method_name: &str,
        deps: Option<&str>,
    ) -> std::io::Result<Self> {
        Self::create_probe(type_name, Some(method_name), deps, None)
    }

    /// Creates a go-to-definition probe with custom dependencies and persistent caching.
    ///
    /// ## Errors
    ///
    /// On I/O failures, and Cache/File System errors
    pub fn for_definition_with_deps_cached(
        type_name: &str,
        method_name: &str,
        deps: Option<&str>,
        ra_path: &Path,
    ) -> std::io::Result<Self> {
        Self::create_probe(type_name, Some(method_name), deps, Some(ra_path))
    }

    /// Infers a minimal Cargo dependency string from a type path when no explicit
    /// `--deps` argument is provided.
    ///
    /// Returns `Some("crate_name = \"*\"")` if the leading path segment starts with
    /// a lowercase letter (indicating a third-party crate), or `None` for stdlib/
    /// primitive types whose leading segment is uppercase (e.g. `Vec`, `HashMap`).
    ///
    /// # Examples
    /// ```text
    /// assert_eq!(infer_dep("serde_json::Value"), Some(r#"serde_json = "*""#.into()));
    /// assert_eq!(infer_dep("Vec<u8>"),           None);
    /// assert_eq!(infer_dep("HashMap<K, V>"),     None);
    /// ```
    pub(crate) fn infer_dep(type_name: &str) -> Option<String> {
        if !type_name.contains("::") {
            return None; // primitives, String, Vec<u8>, etc. never need an inferred dep
        }
        let crate_name = type_name.split("::").next()?;
        if crate_name.chars().next()?.is_ascii_lowercase() {
            Some(format!(r#"{crate_name} = "*""#))
        } else {
            None
        }
    }

    /// Internal probe creation logic shared by all constructors.
    ///
    /// Checks the global cache first. On a hit, clones the [`Arc`] and returns
    /// immediately without touching the filesystem. On a miss, creates the probe
    /// directory, writes the files, inserts into the cache, and returns.
    ///
    /// # Arguments
    /// * `type_name` - The Rust type to query
    /// * `method_name` - If Some, creates a definition probe; if None, creates a completion probe
    /// * `deps` - Optional TOML dependencies to add to Cargo.toml
    fn create_probe(
        type_name: &str,
        method_name: Option<&str>,
        deps: Option<&str>,
        ra_path: Option<&Path>,
    ) -> std::io::Result<Self> {
        let effective_deps = deps
            .map(str::to_owned)
            .or_else(|| Self::infer_dep(type_name));

        let key = CacheKey {
            type_name: type_name.to_string(),
            effective_deps: effective_deps.clone(),
            method_name: method_name.map(str::to_owned),
        };

        // 1. In-process cache
        if let Some(probe) = Self::from_memory_cache(&key) {
            return Ok(probe);
        }

        // 2. Persistent cache
        if let Some(ra) = ra_path
            && let Some(probe) = Self::from_persistent_cache(&key, ra)
        {
            return Ok(probe);
        }

        // 3. Create on disk
        Self::create_on_disk(
            key,
            type_name,
            method_name,
            effective_deps.as_ref(),
            ra_path,
        )
    }

    /// Checks the in-process Arc cache. Returns `Some(Probe)` on a hit.
    fn from_memory_cache(key: &CacheKey) -> Option<Self> {
        let inner = {
            let cache = cache::global_cache()
                .lock()
                .expect("probe cache lock poisoned");
            let cached = cache.get(key)?;
            if !cached.dir.exists() {
                return None;
            }
            let inner = Arc::clone(cached);
            drop(cache);
            inner
        };
        Some(Self {
            dir: inner.dir.clone(),
            src_path: inner.src_path.clone(),
            dot_line: inner.dot_line,
            dot_col: inner.dot_col,
            inner,
        })
    }

    /// Checks the persistent disk cache. On a hit, also populates the in-process
    /// cache so subsequent calls in the same process are instant.
    fn from_persistent_cache(key: &CacheKey, ra_path: &Path) -> Option<Self> {
        let version = ra_version(ra_path);
        let (dir, src_path, dot_line, dot_col) = load_persistent_probe(
            &key.type_name,
            key.effective_deps.as_deref(),
            key.method_name.as_deref(),
            &version,
        )?;

        if std::env::var("RUST_METH_DEBUG").is_ok() {
            eprintln!("[debug] persistent cache hit: {}", dir.display());
        }

        let inner = Arc::new(CachedProbe {
            dir: dir.clone(),
            src_path: src_path.clone(),
            dot_line,
            dot_col,
            owned: false,
        });

        {
            let mut cache = global_cache().lock().expect("probe cache lock poisoned");
            cache.insert(key.clone(), Arc::clone(&inner));
        }

        Some(Self {
            inner,
            dot_line,
            dot_col,
            dir,
            src_path,
        })
    }

    /// Creates a new probe directory on disk, writes Cargo.toml and src/main.rs,
    /// saves to the persistent cache if `ra_path` is provided, and inserts into
    /// the in-process cache.
    fn create_on_disk(
        key: CacheKey,
        type_name: &str,
        method_name: Option<&str>,
        effective_deps: Option<&String>,
        ra_path: Option<&Path>,
    ) -> std::io::Result<Self> {
        let id = PROBE_COUNTER.fetch_add(1, Ordering::Relaxed);
        let suffix = method_name.map_or("probe", |_| "probe-def");
        let dir =
            std::env::temp_dir().join(format!("rust-meth-{suffix}-{}-{id}", std::process::id()));
        let src_dir = dir.join("src");
        fs::create_dir_all(&src_dir)?;

        let cargo_toml = effective_deps.map_or_else(
        || "[package]\nname = \"probe\"\nversion = \"0.1.0\"\nedition = \"2024\"\n".to_string(),
        |d| format!(
            "[package]\nname = \"probe\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n[dependencies]\n{d}\n"
        ),
    );
        fs::write(dir.join("Cargo.toml"), cargo_toml)?;

        let preamble_lines = u32::try_from(PREAMBLE.lines().count())
            .expect("PREAMBLE is a fixed compile-time constant");

        let source = method_name.map_or_else(
        || format!("{PREAMBLE}fn main() {{\n    let _x: {type_name} = todo!();\n    _x.\n}}\n"),
        |method| format!(
            "{PREAMBLE}fn main() {{\n    let _x: {type_name} = todo!();\n    _x.{method}();\n}}\n"
        ),
    );

        let src_path = src_dir.join("main.rs");
        fs::write(&src_path, &source)?;

        let dot_line = preamble_lines + 2;
        let dot_col =
            u32::try_from("    _x.".len()).expect("string literal length always fits in u32");

        if std::env::var("RUST_METH_DEBUG").is_ok() {
            eprintln!("=== probe source ===");
            for (i, line) in source.lines().enumerate() {
                eprintln!("{i:3}: {line}");
            }
            eprintln!("cursor → line={dot_line} col={dot_col}");
        }

        if let Some(ra) = ra_path {
            let version = ra_version(ra);
            save_persistent_probe(
                &dir,
                type_name,
                effective_deps.map(String::as_str),
                method_name,
                &version,
                dot_line,
                dot_col,
            );
        }

        let inner = Arc::new(CachedProbe {
            dir: dir.clone(),
            src_path: src_path.clone(),
            dot_line,
            dot_col,
            owned: true,
        });

        {
            let mut cache = global_cache().lock().expect("probe cache lock poisoned");
            cache.insert(key, Arc::clone(&inner));
        }

        Ok(Self {
            inner,
            dot_line,
            dot_col,
            dir,
            src_path,
        })
    }

    /// Converts the generated `src/main.rs` file path into a formatted `file://` URI string.
    ///
    /// Useful for protocols like LSP that require document paths formatted as URLs.
    #[must_use]
    pub fn src_uri(&self) -> String {
        path_to_uri(&self.src_path)
    }

    /// Converts the root workspace directory path into a formatted `file://` URI string.
    #[must_use]
    pub fn root_uri(&self) -> String {
        path_to_uri(&self.dir)
    }

    /// Reads and returns the contents of the source file as a string.
    ///
    /// # Errors
    ///
    /// This function will return an `Err` if the file cannot be read.
    /// Common reasons include:
    /// * The file at `src_path` does not exist.
    /// * The user lacks permissions to read the file.
    /// * The file contents are not valid UTF-8.
    pub fn source(&self) -> std::io::Result<String> {
        fs::read_to_string(&self.src_path)
    }
}

// `CachedProbe::drop` handles cleanup
// when the Arc refcount reaches zero. Nothing to do here.
impl Drop for Probe {
    fn drop(&mut self) {}
}

fn path_to_uri(path: &Path) -> String {
    let s = path.to_string_lossy();
    if s.starts_with('/') {
        format!("file://{s}")
    } else {
        format!("file:///{s}")
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    // -- helpers -------------------------------------------------------

    fn preamble_line_count() -> u32 {
        u32::try_from(PREAMBLE.lines().count()).unwrap()
    }

    // -- Cargo.toml generation ------------------------------------------

    #[test]
    fn no_deps_omits_dependencies_section() {
        let p = Probe::new_with_deps("Vec<u8>", None).unwrap();
        let cargo = fs::read_to_string(p.dir.join("Cargo.toml")).unwrap();
        assert!(
            !cargo.contains("[dependencies]"),
            "Cargo.toml should not have a [dependencies] section when deps is None"
        );
        assert!(cargo.contains("[package]"));
        assert!(cargo.contains(r#"name = "probe""#));
        assert!(cargo.contains(r#"edition = "2024""#));
    }

    #[test]
    fn with_deps_injects_dependencies_section() {
        let p = Probe::new_with_deps("serde_json::Value", Some(r#"serde_json = "1.0""#)).unwrap();
        let cargo = fs::read_to_string(p.dir.join("Cargo.toml")).unwrap();
        assert!(cargo.contains("[dependencies]"));
        assert!(cargo.contains(r#"serde_json = "1.0""#));
    }

    #[test]
    fn multiple_deps_all_appear_in_cargo_toml() {
        let deps = "serde = { version = \"1.0\", features = [\"derive\"] }\nserde_json = \"1.0\"";
        let p = Probe::new_with_deps("serde_json::Value", Some(deps)).unwrap();
        let cargo = fs::read_to_string(p.dir.join("Cargo.toml")).unwrap();
        assert!(cargo.contains("[dependencies]"));
        assert!(cargo.contains("serde ="));
        assert!(cargo.contains(r#"serde_json = "1.0""#));
    }

    // ── source content ───────────────────────────────────────────────────────

    #[test]
    fn completion_probe_source_has_dot_trigger() {
        let p = Probe::new_with_deps("Vec<u8>", None).unwrap();
        let src = p.source().unwrap();
        assert!(
            src.contains("let _x: Vec<u8> = todo!();"),
            "source should declare the type"
        );
        // ends with `_x.` NOT `_x.something()`
        assert!(
            src.contains("    _x.\n"),
            "completion probe should have bare dot trigger"
        );
    }

    #[test]
    fn definition_probe_source_has_method_call() {
        let p = Probe::for_definition_with_deps("Vec<u8>", "push", None).unwrap();
        let src = p.source().unwrap();
        assert!(src.contains("let _x: Vec<u8> = todo!();"));
        assert!(
            src.contains("_x.push();"),
            "definition probe should contain the method call"
        );
    }

    #[test]
    fn completion_probe_with_deps_type_in_source() {
        let p = Probe::new_with_deps("serde_json::Value", Some(r#"serde_json = "1.0""#)).unwrap();
        let src = p.source().unwrap();
        assert!(src.contains("let _x: serde_json::Value = todo!();"));
    }

    #[test]
    fn definition_probe_with_deps_cargo_and_source_correct() {
        let p = Probe::for_definition_with_deps(
            "serde_json::Value",
            "as_str",
            Some(r#"serde_json = "1.0""#),
        )
        .unwrap();
        let cargo = fs::read_to_string(p.dir.join("Cargo.toml")).unwrap();
        assert!(cargo.contains("[dependencies]"));
        let src = p.source().unwrap();
        assert!(src.contains("serde_json::Value"));
        assert!(src.contains("_x.as_str();"));
    }

    // ── dot position ─────────────────────────────────────────────────────────

    #[test]
    fn dot_col_is_seven() {
        // "    _x." is always 7 characters
        let p = Probe::new_with_deps("Vec<u8>", None).unwrap();
        assert_eq!(p.dot_col, 7, r#""    _x." should be 7 chars"#);
    }

    #[test]
    fn dot_line_is_preamble_plus_two() {
        // layout: preamble lines, fn main() {, let _x = …, _x.
        let p = Probe::new_with_deps("Vec<u8>", None).unwrap();
        assert_eq!(p.dot_line, preamble_line_count() + 2);
    }

    #[test]
    fn dot_line_same_for_definition_probe() {
        let p = Probe::for_definition_with_deps("Vec<u8>", "len", None).unwrap();
        assert_eq!(p.dot_line, preamble_line_count() + 2);
    }

    // ── URI helpers ──────────────────────────────────────────────────────────

    #[test]
    fn src_uri_is_file_uri_ending_in_main_rs() {
        let p = Probe::new_with_deps("Vec<u8>", None).unwrap();
        let uri = p.src_uri();
        assert!(
            uri.starts_with("file://"),
            "src_uri should be a file:// URI"
        );
        assert!(
            uri.ends_with("/src/main.rs"),
            "src_uri should end in /src/main.rs"
        );
    }

    #[test]
    fn root_uri_is_file_uri_not_ending_in_main_rs() {
        let p = Probe::new_with_deps("Vec<u8>", None).unwrap();
        let uri = p.root_uri();
        assert!(uri.starts_with("file://"));
        assert!(!uri.ends_with("main.rs"));
    }
}