(Query(query): Query<FindTextQuery>)
| 3073 | } |
| 3074 | |
| 3075 | async fn find_text(Query(query): Query<FindTextQuery>) -> Result<Json<Vec<SearchResult>>> { |
| 3076 | let root = project_root()?; |
| 3077 | let base_input = query |
| 3078 | .path |
| 3079 | .unwrap_or_else(|| root.to_string_lossy().to_string()); |
| 3080 | let base_path = resolve_input_path(&base_input, &root)?; |
| 3081 | let mut results = Vec::new(); |
| 3082 | |
| 3083 | fn search_in_file(path: &std::path::Path, pattern: &str, results: &mut Vec<SearchResult>) { |
| 3084 | if let Ok(content) = std::fs::read_to_string(path) { |
| 3085 | for (line_num, line) in content.lines().enumerate() { |
| 3086 | if let Some(col) = line.find(pattern) { |
| 3087 | results.push(SearchResult { |
| 3088 | path: path.to_string_lossy().to_string(), |
| 3089 | line: line_num + 1, |
| 3090 | column: col + 1, |
| 3091 | match_text: line.to_string(), |
| 3092 | }); |
| 3093 | } |
| 3094 | } |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | fn search_recursive( |
| 3099 | path: &FsPath, |
| 3100 | root: &FsPath, |
| 3101 | pattern: &str, |
| 3102 | results: &mut Vec<SearchResult>, |
| 3103 | ) { |
| 3104 | if path.is_dir() { |
| 3105 | if let Ok(entries) = std::fs::read_dir(path) { |
| 3106 | for entry in entries.flatten() { |
| 3107 | let path_buf = entry.path(); |
| 3108 | if !is_within_root(&path_buf, root) { |
| 3109 | continue; |
| 3110 | } |
| 3111 | if path_buf.is_dir() { |
| 3112 | search_recursive(&path_buf, root, pattern, results); |
| 3113 | } else if path_buf.is_file() { |
| 3114 | search_in_file(&path_buf, pattern, results); |
| 3115 | } |
| 3116 | } |
| 3117 | } |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | search_recursive(&base_path, &root, &query.pattern, &mut results); |
| 3122 | Ok(Json(results)) |
| 3123 | } |
| 3124 | |
| 3125 | #[derive(Debug, Deserialize)] |
| 3126 | pub struct FindFilesQuery { |
nothing calls this directly
no test coverage detected