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
//! # Rust Assistant Library
//!
//! `rust_assistant` is a comprehensive library designed to enhance the Rust development experience,
//! offering a suite of tools and functionalities for downloading, caching, searching, and analyzing Rust crates.
//!
//! This library encapsulates a range of modules, each specializing in different aspects of crate management
//! and code analysis. It aims to streamline the process of working with Rust crates, providing developers
//! with efficient access to crate data, advanced search capabilities, and more.
//!
//! ## Features
//!
//! - **Crate Downloading**: Facilitates the downloading of crates from sources like crates.io,
//!   handling network requests and data processing.
//!
//! - **Crate Caching**: Implements caching mechanisms to store downloaded crates, optimizing
//!   performance and reducing redundant operations.
//!
//! - **Search Functionality**: Provides advanced search functionalities within crate contents,
//!   including source code, documentation, and other relevant data.
//!
//! ## Modules
//!
//! - `app`: Contains the core application logic for the Rust Assistant.
//! - `cache`: Provides caching functionalities for crates.
//! - `download`: Handles the downloading of crates and their contents.
//! - `search`: Implements search algorithms and data structures for efficient crate content search.
//!
pub mod app;

#[cfg(feature = "axum")]
pub mod axum;
pub mod cache;
pub mod download;
pub mod github;
pub mod search;

use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::fmt::{Display, Formatter};
use std::num::NonZeroUsize;
use std::ops::{Range, RangeInclusive};
use std::path::{Path, PathBuf};
use std::sync::Arc;

#[cfg(feature = "utoipa")]
use utoipa::ToSchema;

pub use app::*;
pub use search::*;

/// Represents the name and version of a crate.
///
/// This struct is used to uniquely identify a crate with its name and version number.
#[derive(Debug, Deserialize, Serialize, Hash, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct CrateVersion {
    /// The exact name of the crate
    #[serde(rename = "crate")]
    pub krate: Arc<str>,
    /// The semantic version number of the specified crate, following the Semantic versioning specification.
    pub version: Arc<str>,
}

impl CrateVersion {
    /// Computes the root directory for the specified crate version.
    ///
    /// This method concatenates the crate name and version number to form a path-like string,
    /// which can be used as a directory name to store crate-related data.
    ///
    pub fn root_dir(&self) -> PathBuf {
        PathBuf::from(format!("{}-{}", self.krate, self.version))
    }
}

impl<C, V> From<(C, V)> for CrateVersion
where
    C: AsRef<str>,
    V: AsRef<str>,
{
    /// Creates a `CrateVersion` instance from a tuple of crate name and version.
    ///
    /// This method allows for a convenient way to construct a `CrateVersion` from separate
    /// name and version strings.
    fn from(value: (C, V)) -> Self {
        Self {
            krate: Arc::from(value.0.as_ref()),
            version: Arc::from(value.1.as_ref()),
        }
    }
}

impl Display for CrateVersion {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}-{}", self.krate, self.version)
    }
}

/// Represents a path within a specific crate's directory structure.
///
/// It combines the crate version information with the relative path within the crate.
#[derive(Debug, Deserialize, Serialize)]
pub struct CrateVersionPath {
    /// The name and version of a crate.
    #[serde(flatten)]
    pub crate_version: CrateVersion,
    /// The path.
    pub path: Arc<str>,
}

/// Represents a range of lines in a file.
///
/// This struct is used to specify a start and end line for operations that work with line ranges.
#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct FileLineRange {
    /// The start line number.
    pub start: Option<NonZeroUsize>,
    /// The end line number.
    pub end: Option<NonZeroUsize>,
}

/// Represents the contents of a directory, including files and subdirectories.
///
/// This is used to provide a snapshot of a directory's contents, listing all files and directories.
#[derive(Debug, Deserialize, Serialize, Clone)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct Directory {
    /// Files in the directory.
    #[cfg_attr(feature = "utoipa", schema(value_type = BTreeSet<String>))]
    pub files: Arc<BTreeSet<PathBuf>>,
    /// Subdirectories in the directory.
    #[cfg_attr(feature = "utoipa", schema(value_type = BTreeSet<String>))]
    pub directories: Arc<BTreeSet<PathBuf>>,
}

impl Directory {
    /// Checks whether the directory is empty.
    ///
    /// This method returns `true` if both the `files` and `directories` sets are empty,
    /// indicating that the directory has no contents.
    pub fn is_empty(&self) -> bool {
        self.files.is_empty() && self.directories.is_empty()
    }
}

#[derive(Debug, Default)]
pub struct DirectoryMut {
    pub files: BTreeSet<PathBuf>,
    pub directories: BTreeSet<PathBuf>,
}

impl DirectoryMut {
    /// Checks whether the mutable directory is empty.
    ///
    /// Similar to `Directory::is_empty`, but for the mutable version of the directory.
    /// It's useful for scenarios where directory contents are being modified.
    pub fn is_empty(&self) -> bool {
        self.files.is_empty() && self.directories.is_empty()
    }

    /// Freezes the directory, converting it into an immutable `Directory`.
    ///
    /// This method converts `DirectoryMut` into `Directory` by wrapping its contents
    /// in `Arc`, thus allowing for safe shared access.
    ///
    pub fn freeze(self) -> Directory {
        Directory {
            files: Arc::new(self.files),
            directories: Arc::new(self.directories),
        }
    }
}

/// Represents a query for searching items in a crate.
///
/// This struct is used to specify the criteria for searching items like structs, enums, traits, etc., within a crate.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct ItemQuery {
    /// The type of item to search for.
    #[serde(rename = "type")]
    pub type_: ItemType,
    /// The query string used for searching.
    pub query: String,
    /// Optional path within the crate to narrow down the search scope.
    pub path: Option<PathBuf>,
}

/// Represents an item found in a crate.
///
/// This struct describes an item, such as a struct or function, including its location within the crate.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct Item {
    /// The name of the item.
    pub name: String,
    /// The type of the item.
    #[serde(rename = "type")]
    pub type_: ItemType,
    /// The file path where the item is located.
    #[cfg_attr(feature = "utoipa", schema(value_type = String))]
    pub file: Arc<Path>,
    /// The range of lines in the file where the item is defined.
    #[cfg_attr(feature = "utoipa", schema(value_type = RangeSchema))]
    pub line_range: RangeInclusive<NonZeroUsize>,
}

/// Defines various types of items that can be searched for in a crate.
///
/// This enum lists different types of code constructs like structs, enums, traits, etc.
#[derive(Debug, Default, Serialize, Deserialize, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
#[serde(rename_all = "kebab-case")]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub enum ItemType {
    /// Represents all item types.
    #[default]
    All,
    /// A struct definition.
    Struct,
    /// An enum definition.
    Enum,
    /// A trait definition.
    Trait,
    /// Type implementation.
    ImplType,
    /// Trait implementation for a type.
    ImplTraitForType,
    /// A macro definition.
    Macro,
    /// An attribute macro.
    AttributeMacro,
    /// A standalone function.
    Function,
    /// A type alias.
    TypeAlias,
}

/// Represents a query for searching lines within files in a crate.
///
/// This struct is used for specifying criteria for line-based searches, such as finding specific text within files.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct LineQuery {
    /// The text or pattern to search for.
    pub query: String,
    /// The search mode (e.g., plain text or regular expression).
    pub mode: SearchMode,
    /// Indicates if the search should be case-sensitive.
    #[serde(default)]
    pub case_sensitive: bool,
    /// Indicates if the search should match whole words only.
    #[serde(default)]
    pub whole_word: bool,
    /// The maximum number of results to return.
    #[cfg_attr(feature = "utoipa", schema(value_type = usize))]
    pub max_results: Option<NonZeroUsize>,
    /// A comma-separated string specifying file extensions to include in the search.
    /// Each segment represents a file extension, e.g., "rs,txt" for Rust and text files.
    #[serde(default)]
    pub file_ext: String,
    /// Optional path within the crate to limit the search scope.
    #[cfg_attr(feature = "utoipa", schema(value_type = Option<String>))]
    pub path: Option<PathBuf>,
}

/// Defines different modes for searching text.
///
/// This enum distinguishes between plain text searches and regular expression searches.
#[derive(Debug, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
#[serde(rename_all = "kebab-case")]
pub enum SearchMode {
    /// A plain text search.
    PlainText,
    /// A regular expression search.
    Regex,
}

/// Represents a specific line found in a search operation.
///
/// This struct contains details about a line of text found in a file, including its content and location.
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
pub struct Line {
    /// The content of the line.
    pub line: String,
    /// The file path where the line is located.
    #[cfg_attr(feature = "utoipa", schema(value_type = String))]
    pub file: PathBuf,
    /// The line number within the file.
    #[cfg_attr(feature = "utoipa", schema(value_type = usize))]
    pub line_number: NonZeroUsize,
    /// The range of columns in the line where the text was found.
    #[cfg_attr(feature = "utoipa", schema(value_type = RangeSchema))]
    pub column_range: Range<NonZeroUsize>,
}

/// Schema for representing a range, used in other structs to describe line and column ranges.
#[cfg(feature = "utoipa")]
#[derive(ToSchema)]
pub struct RangeSchema {
    /// The start line number.
    pub start: usize,
    /// The end line number.
    pub end: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cache::{Crate, CrateCache, CrateTar};
    use crate::download::CrateDownloader;
    use std::num::NonZeroUsize;

    #[tokio::test]
    async fn download_and_read() -> anyhow::Result<()> {
        // let start = Instant::now();
        let crate_version = CrateVersion::from(("tokio", "1.35.1"));
        let downloader = CrateDownloader::default();
        let tar_data = downloader.download_crate_file(&crate_version).await?;
        let cache = CrateCache::new(NonZeroUsize::new(1024).unwrap());
        let crate_tar = CrateTar::from((crate_version.clone(), tar_data));
        let krate = Crate::try_from(crate_tar)?;
        let old = cache.set_crate(crate_version.clone(), krate);
        assert!(old.is_none());

        let crate_ = cache.get_crate(&crate_version).expect("get crate");

        let files = crate_.read_directory("").expect("read directory");
        assert!(!files.is_empty());
        println!("{:#?}", files);

        let lib_rs_content = crate_.get_file_by_line_range("src/lib.rs", ..)?;
        assert!(lib_rs_content.is_some());
        let lib_rs_range_content =
            crate_.get_file_by_line_range("src/lib.rs", ..NonZeroUsize::new(27).unwrap())?;
        assert!(lib_rs_range_content.is_some());
        // println!("{}", lib_rs_range_content.expect("lib.rs"));
        // println!("Elapsed: {}µs", start.elapsed().as_micros());

        let file = crate_
            .get_file_by_line_range("src/lib.rs", ..=NonZeroUsize::new(3).unwrap())?
            .unwrap();
        println!("[{}]", std::str::from_utf8(file.data.as_ref()).unwrap());

        let lines = crate_.search_line(&LineQuery {
            query: "Sleep".to_string(),
            mode: SearchMode::PlainText,
            case_sensitive: true,
            whole_word: true,
            max_results: Some(6.try_into().expect("6")),
            file_ext: "rs".into(),
            path: Some(PathBuf::from("src")),
        })?;
        println!("{:#?}", lines);
        Ok(())
    }
}