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
//! The `app` module.
//!
//! This module contains the core application logic for the Rust Assistant.
//! It typically includes structures and functions responsible for initializing
//! and running the application, handling high-level operations, and coordinating
//! between other modules.
//!
use crate::cache::{Crate, CrateCache, CrateTar, FileContent};
use crate::download::CrateDownloader;
use crate::github::{GithubClient, Issue, IssueEvent, Repository};
use crate::{
CrateVersion, CrateVersionPath, Directory, FileLineRange, Item, ItemQuery, Line, LineQuery,
};
/// The `RustAssistant` struct, providing functionalities to interact with crates and their contents.
///
/// This struct encapsulates methods for downloading crates, reading their content,
/// and performing searches within them.
#[derive(Clone)]
pub struct RustAssistant {
downloader: CrateDownloader,
cache: CrateCache,
github: GithubClient,
}
impl From<(CrateDownloader, CrateCache, GithubClient)> for RustAssistant {
/// Creates a new `RustAssistant` instance from a tuple of dependencies.
fn from((downloader, cache, github): (CrateDownloader, CrateCache, GithubClient)) -> Self {
Self {
downloader,
cache,
github,
}
}
}
impl RustAssistant {
/// Retrieves a crate from the cache or downloads it if not already cached.
///
/// # Arguments
/// * `crate_version` - A reference to `CrateVersion` specifying the crate to retrieve.
///
/// # Returns
/// A `Result` wrapping the `Crate`, or an error if the operation fails.
pub async fn get_crate(&self, crate_version: &CrateVersion) -> anyhow::Result<Crate> {
Ok(match self.cache.get_crate(crate_version) {
None => {
let data = self.downloader.download_crate_file(crate_version).await?;
let crate_tar = CrateTar::from((crate_version.clone(), data));
let krate =
tokio::task::spawn_blocking(move || Crate::try_from(crate_tar)).await??;
self.cache.set_crate(crate_version.clone(), krate);
self.cache
.get_crate(crate_version)
.ok_or_else(|| anyhow::anyhow!("Failed to get crate: {}", crate_version))?
}
Some(crate_tar) => crate_tar,
})
}
/// Retrieves the content of a file within a specified crate and range.
///
/// # Arguments
/// * `crate_version_path` - A reference to `CrateVersionPath` specifying the crate and file path.
/// * `file_line_range` - A `FileLineRange` specifying the range of lines to retrieve.
///
/// # Returns
/// A `Result` wrapping an `Option<CrateFileContent>`, or an error if the operation fails.
pub async fn get_file_content(
&self,
crate_version_path: &CrateVersionPath,
file_line_range: FileLineRange,
) -> anyhow::Result<Option<FileContent>> {
let krate = self.get_crate(&crate_version_path.crate_version).await?;
let path = crate_version_path.path.clone();
tokio::task::spawn_blocking(move || {
krate.get_file_by_file_line_range(path.as_ref(), file_line_range)
})
.await?
}
/// Reads the content of a directory within a specified crate.
///
/// # Arguments
/// * `crate_version_path` - A `CrateVersionPath` specifying the crate and directory path.
///
/// # Returns
/// A `Result` wrapping an `Option<Directory>`, or an error if the operation fails.
pub async fn read_directory(
&self,
crate_version_path: CrateVersionPath,
) -> anyhow::Result<Option<Directory>> {
let krate = self.get_crate(&crate_version_path.crate_version).await?;
Ok(krate
.read_directory(crate_version_path.path.as_ref())
.cloned())
}
/// Searches for items in a crate based on a query.
///
/// # Arguments
/// * `crate_version` - A reference to `CrateVersion` specifying the crate to search in.
/// * `query` - An `ItemQuery` specifying the search criteria.
///
/// # Returns
/// A `Result` wrapping a `Vec<Item>`, or an error if the operation fails.
pub async fn search_item(
&self,
crate_version: &CrateVersion,
query: impl Into<ItemQuery>,
) -> anyhow::Result<Vec<Item>> {
let krate = self.get_crate(crate_version).await?;
let query = query.into();
Ok(tokio::task::spawn_blocking(move || krate.search_item(&query)).await?)
}
/// Searches for lines in a crate's files based on a query.
///
/// # Arguments
/// * `crate_version` - A reference to `CrateVersion` specifying the crate to search in.
/// * `query` - A `LineQuery` specifying the search criteria.
///
/// # Returns
/// A `Result` wrapping a `Vec<Line>`, or an error if the operation fails.
pub async fn search_line(
&self,
crate_version: &CrateVersion,
query: impl Into<LineQuery>,
) -> anyhow::Result<Vec<Line>> {
let krate = self.get_crate(crate_version).await?;
let query = query.into();
tokio::task::spawn_blocking(move || krate.search_line(&query)).await?
}
/// Reads the content of a file within a specified GitHub repository.
///
/// # Arguments
/// * `repo` - A reference to `Repository` specifying the GitHub repository.
/// * `path` - A `&str` specifying the file path.
///
/// # Returns
/// A `Result` wrapping a `FileContent`, or an error if the operation fails.
///
pub async fn read_github_repository_file(
&self,
repo: &Repository,
path: &str,
) -> anyhow::Result<Option<FileContent>> {
self.github.get_file(repo, path).await
}
/// Reads the content of a directory within a specified GitHub repository.
///
/// # Arguments
/// * `repo` - A reference to `Repository` specifying the GitHub repository.
/// * `path` - A `&str` specifying the directory path.
///
/// # Returns
/// A `Result` wrapping a `Directory`, or an error if the operation fails.
///
pub async fn read_github_repository_directory(
&self,
repo: &Repository,
path: &str,
) -> anyhow::Result<Option<Directory>> {
self.github.read_dir(repo, path).await
}
/// Searches for issues in a specified GitHub repository based on a query.
///
/// # Arguments
/// * `repo` - A reference to `Repository` specifying the GitHub repository.
/// * `query` - A `&str` specifying the query string.
///
/// # Returns
/// A `Result` wrapping a `Vec<Issue>`, or an error if the operation fails.
///
pub async fn search_github_repository_for_issues(
&self,
repo: &Repository,
query: &str,
) -> anyhow::Result<Vec<Issue>> {
self.github.search_for_issues(repo, query).await
}
/// Retrieves the timeline of an issue in a specified GitHub repository.
///
/// # Arguments
/// * `repo` - A reference to `Repository` specifying the GitHub repository.
/// * `issue_number` - A `u64` specifying the issue number.
///
/// # Returns
/// A `Result` wrapping a `Vec<IssueEvent>`, or an error if the operation fails.
///
pub async fn get_github_repository_issue_timeline(
&self,
repo: &Repository,
issue_number: u64,
) -> anyhow::Result<Vec<IssueEvent>> {
self.github.get_issue_timeline(repo, issue_number).await
}
}