vizier-tui 0.2.0

Blazing-fast Rust Code Inspector for the Terminal
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
//! Installed crates registry inspector
//!
//! Scans ~/.cargo/registry to find and analyze installed crates

use crate::analyzer::{AnalyzedItem, RustAnalyzer};
use crate::error::Result;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

/// Parsed Cargo.toml package metadata: description, authors, license, repository, documentation, keywords, categories
type CargoTomlMeta = (
    Option<String>,
    Vec<String>,
    Option<String>,
    Option<String>,
    Option<String>,
    Vec<String>,
    Vec<String>,
);

/// Information about an installed crate
#[derive(Debug, Clone)]
pub struct InstalledCrate {
    pub name: String,
    pub version: String,
    pub path: PathBuf,
    pub readme: Option<String>,
    pub license: Option<String>,
    pub description: Option<String>,
    pub authors: Vec<String>,
    pub repository: Option<String>,
    pub documentation: Option<String>,
    pub keywords: Vec<String>,
    pub categories: Vec<String>,
}

/// Registry of installed crates
pub struct CrateRegistry {
    crates: HashMap<String, Vec<InstalledCrate>>,
    registry_path: PathBuf,
}

impl CrateRegistry {
    /// Create a new registry scanner
    pub fn new() -> Self {
        let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
        let registry_path = PathBuf::from(home).join(".cargo/registry/src");

        Self {
            crates: HashMap::new(),
            registry_path,
        }
    }

    /// Create with custom registry path
    pub fn with_path(path: PathBuf) -> Self {
        Self {
            crates: HashMap::new(),
            registry_path: path,
        }
    }

    /// Scan the registry for installed crates
    pub fn scan(&mut self) -> Result<()> {
        self.crates.clear();

        if !self.registry_path.exists() {
            return Ok(());
        }

        // Registry has subdirectories for different indices (e.g., index.crates.io-xxx)
        for index_entry in fs::read_dir(&self.registry_path)? {
            let index_entry = index_entry?;
            let index_path = index_entry.path();

            if index_path.is_dir() {
                self.scan_index_directory(&index_path)?;
            }
        }

        Ok(())
    }

    fn scan_index_directory(&mut self, index_path: &Path) -> Result<()> {
        for entry in fs::read_dir(index_path)? {
            let entry = entry?;
            let crate_path = entry.path();

            if crate_path.is_dir() {
                if let Some(crate_info) = self.parse_crate_directory(&crate_path) {
                    self.crates
                        .entry(crate_info.name.clone())
                        .or_default()
                        .push(crate_info);
                }
            }
        }

        Ok(())
    }

    fn parse_crate_directory(&self, path: &Path) -> Option<InstalledCrate> {
        let dir_name = path.file_name()?.to_str()?;

        // Parse name and version from directory name (e.g., "serde-1.0.193")
        let (name, version) = Self::parse_crate_name_version(dir_name)?;

        // Try to read Cargo.toml for metadata
        let cargo_toml_path = path.join("Cargo.toml");
        let (description, authors, license, repository, documentation, keywords, categories) =
            if cargo_toml_path.exists() {
                Self::parse_cargo_toml(&cargo_toml_path)
            } else {
                (None, vec![], None, None, None, vec![], vec![])
            };

        // Try to read README
        let readme = Self::find_and_read_readme(path);

        Some(InstalledCrate {
            name,
            version,
            path: path.to_path_buf(),
            readme,
            license,
            description,
            authors,
            repository,
            documentation,
            keywords,
            categories,
        })
    }

    fn parse_crate_name_version(dir_name: &str) -> Option<(String, String)> {
        // Find the last hyphen followed by a version number
        let mut last_version_start = None;
        let chars: Vec<char> = dir_name.chars().collect();

        for i in (0..chars.len()).rev() {
            if chars[i] == '-' && i + 1 < chars.len() {
                // Check if what follows looks like a version
                let rest = &dir_name[i + 1..];
                if rest
                    .chars()
                    .next()
                    .map(|c| c.is_ascii_digit())
                    .unwrap_or(false)
                {
                    last_version_start = Some(i);
                    break;
                }
            }
        }

        let i = last_version_start?;
        let name = dir_name[..i].to_string();
        let version = dir_name[i + 1..].to_string();

        Some((name, version))
    }

    fn parse_cargo_toml(path: &Path) -> CargoTomlMeta {
        let content = match fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => return (None, vec![], None, None, None, vec![], vec![]),
        };

        let toml: toml::Value = match content.parse() {
            Ok(t) => t,
            Err(_) => return (None, vec![], None, None, None, vec![], vec![]),
        };

        let package = toml.get("package");

        let description = package
            .and_then(|p| p.get("description"))
            .and_then(|d| d.as_str())
            .map(|s| s.to_string());

        let authors = package
            .and_then(|p| p.get("authors"))
            .and_then(|a| a.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str())
                    .map(|s| s.to_string())
                    .collect()
            })
            .unwrap_or_default();

        let license = package
            .and_then(|p| p.get("license"))
            .and_then(|l| l.as_str())
            .map(|s| s.to_string());

        let repository = package
            .and_then(|p| p.get("repository"))
            .and_then(|r| r.as_str())
            .map(|s| s.to_string());

        let documentation = package
            .and_then(|p| p.get("documentation"))
            .and_then(|d| d.as_str())
            .map(|s| s.to_string());

        let keywords = package
            .and_then(|p| p.get("keywords"))
            .and_then(|k| k.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str())
                    .map(|s| s.to_string())
                    .collect()
            })
            .unwrap_or_default();

        let categories = package
            .and_then(|p| p.get("categories"))
            .and_then(|c| c.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str())
                    .map(|s| s.to_string())
                    .collect()
            })
            .unwrap_or_default();

        (
            description,
            authors,
            license,
            repository,
            documentation,
            keywords,
            categories,
        )
    }

    fn find_and_read_readme(path: &Path) -> Option<String> {
        let readme_names = [
            "README.md",
            "README",
            "Readme.md",
            "readme.md",
            "README.txt",
        ];

        for name in &readme_names {
            let readme_path = path.join(name);
            if readme_path.exists() {
                if let Ok(content) = fs::read_to_string(&readme_path) {
                    // Truncate if too long, ensuring we don't split UTF-8 chars
                    let max_len = 10000;
                    if content.len() > max_len {
                        // Find valid UTF-8 boundary
                        let truncate_at = content
                            .char_indices()
                            .take_while(|(i, _)| *i < max_len)
                            .last()
                            .map(|(i, c)| i + c.len_utf8())
                            .unwrap_or(0);
                        return Some(content[..truncate_at].to_string() + "\n...[truncated]");
                    }
                    return Some(content);
                }
            }
        }
        None
    }

    /// Get all installed crate names
    pub fn crate_names(&self) -> Vec<&str> {
        let mut names: Vec<_> = self.crates.keys().map(|s| s.as_str()).collect();
        names.sort();
        names
    }

    /// Get all versions of a crate
    pub fn versions(&self, name: &str) -> Vec<&InstalledCrate> {
        self.crates
            .get(name)
            .map(|v| {
                let mut versions: Vec<_> = v.iter().collect();
                versions.sort_by(|a, b| {
                    // Sort by version descending (newest first)
                    b.version.cmp(&a.version)
                });
                versions
            })
            .unwrap_or_default()
    }

    /// Get the latest version of a crate
    pub fn latest(&self, name: &str) -> Option<&InstalledCrate> {
        self.versions(name).into_iter().next()
    }

    /// Get a specific version of a crate
    pub fn get(&self, name: &str, version: &str) -> Option<&InstalledCrate> {
        self.crates.get(name)?.iter().find(|c| c.version == version)
    }

    /// Check if a crate is installed
    pub fn is_installed(&self, name: &str) -> bool {
        self.crates.contains_key(name)
    }

    /// Get total number of installed crates
    pub fn count(&self) -> usize {
        self.crates.len()
    }

    /// Analyze a specific installed crate
    pub fn analyze_crate(&self, name: &str, version: Option<&str>) -> Result<Vec<AnalyzedItem>> {
        let crate_info = match version {
            Some(v) => self.get(name, v),
            None => self.latest(name),
        };

        let crate_info = match crate_info {
            Some(c) => c,
            None => return Ok(vec![]),
        };

        let analyzer = RustAnalyzer::new();
        let src_path = crate_info.path.join("src");

        let mut items = Vec::new();
        // Use crate name (with underscores instead of hyphens) as base module path
        let crate_module_name = name.replace('-', "_");

        if src_path.exists() {
            Self::analyze_directory(&analyzer, &src_path, &mut items, &crate_module_name)?;
        }

        Ok(items)
    }

    fn analyze_directory(
        analyzer: &RustAnalyzer,
        dir: &Path,
        items: &mut Vec<AnalyzedItem>,
        crate_name: &str,
    ) -> Result<()> {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_dir() {
                Self::analyze_directory(analyzer, &path, items, crate_name)?;
            } else if path.extension().is_some_and(|ext| ext == "rs") {
                // Build module path: crate_name + path from src/
                let module_path = Self::build_module_path(&path, crate_name);
                if let Ok(file_items) = analyzer.analyze_file_with_module(&path, module_path) {
                    items.extend(file_items);
                }
            }
        }

        Ok(())
    }

    /// Build module path from file path relative to src/
    /// Returns clean path like ["serde", "de", "value"] not registry paths
    fn build_module_path(file_path: &Path, crate_name: &str) -> Vec<String> {
        let mut result = vec![crate_name.to_string()];

        // Convert to string and find src/
        let path_str = file_path.to_string_lossy();

        // Find the last "src/" in the path
        if let Some(src_idx) = path_str.rfind("/src/") {
            let after_src = &path_str[src_idx + 5..]; // Skip "/src/"

            for part in after_src.split('/') {
                if part.ends_with(".rs") {
                    let module = part.trim_end_matches(".rs");
                    // Skip lib.rs, main.rs, mod.rs
                    if module != "lib" && module != "main" && module != "mod" {
                        result.push(module.to_string());
                    }
                } else if !part.is_empty() {
                    result.push(part.to_string());
                }
            }
        }

        result
    }

    /// Search for crates by name
    pub fn search(&self, query: &str) -> Vec<&InstalledCrate> {
        let query_lower = query.to_lowercase();

        self.crates
            .values()
            .flatten()
            .filter(|c| c.name.to_lowercase().contains(&query_lower))
            .collect()
    }
}

impl Default for CrateRegistry {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_parse_crate_name_version() {
        let cases = vec![
            (
                "serde-1.0.193",
                Some(("serde".to_string(), "1.0.193".to_string())),
            ),
            (
                "serde_json-1.0.108",
                Some(("serde_json".to_string(), "1.0.108".to_string())),
            ),
            (
                "tokio-1.35.0",
                Some(("tokio".to_string(), "1.35.0".to_string())),
            ),
            (
                "my-crate-name-0.1.0",
                Some(("my-crate-name".to_string(), "0.1.0".to_string())),
            ),
        ];

        for (input, expected) in cases {
            assert_eq!(
                CrateRegistry::parse_crate_name_version(input),
                expected,
                "Failed for: {}",
                input
            );
        }
    }
}