lean_ctx_ext/core/import/
claude_code.rs1use std::path::{Path, PathBuf};
5
6use super::{ImportResult, ImportSource, home_dir, push_error, read_jsonl_files};
7
8#[must_use]
10pub fn import() -> ImportResult {
11 let Some(home) = home_dir() else {
12 return ImportResult::default();
13 };
14 import_from_root(&home.join(".claude/projects"))
15}
16
17#[must_use]
21pub fn import_from_root(root: &Path) -> ImportResult {
22 let projects = match std::fs::read_dir(root) {
23 Ok(projects) => projects,
24 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
25 return ImportResult::default();
26 }
27 Err(error) => {
28 let mut result = ImportResult::default();
29 push_error(&mut result, format!("{}: {error}", root.display()));
30 return result;
31 }
32 };
33
34 let mut files = Vec::new();
35 for project in projects {
36 let project = match project {
37 Ok(project) => project,
38 Err(error) => {
39 let mut result = ImportResult::default();
40 push_error(&mut result, format!("{}: {error}", root.display()));
41 return result;
42 }
43 };
44 let path = project.path();
45 if !path.is_dir() {
46 continue;
47 }
48 match std::fs::read_dir(&path) {
49 Ok(entries) => collect_jsonl_files(entries, &mut files),
50 Err(error) => {
51 let mut result = read_jsonl_files(ImportSource::ClaudeCode, root, files);
52 push_error(&mut result, format!("{}: {error}", path.display()));
53 return result;
54 }
55 }
56 }
57 files.sort();
58 read_jsonl_files(ImportSource::ClaudeCode, root, files)
59}
60
61fn collect_jsonl_files(entries: std::fs::ReadDir, files: &mut Vec<PathBuf>) {
62 for entry in entries.flatten() {
63 let path = entry.path();
64 if path.is_file()
65 && path
66 .extension()
67 .is_some_and(|extension| extension == "jsonl")
68 {
69 files.push(path);
70 }
71 }
72}