1mod item;
2mod to_markdown;
3
4pub use item::*;
5pub use to_markdown::convert_rustdoc_to_markdown;
6
7use std::path::PathBuf;
8use std::sync::Arc;
9
10use anyhow::{bail, Context, Result};
11use async_trait::async_trait;
12use collections::{HashSet, VecDeque};
13use fs::Fs;
14use futures::AsyncReadExt;
15use http::{AsyncBody, HttpClient, HttpClientWithUrl};
16
17use crate::{IndexedDocsDatabase, IndexedDocsProvider, PackageName, ProviderId};
18
19#[derive(Debug, Clone, Copy)]
20pub enum RustdocSource {
21 /// The docs were sourced from Zed's rustdoc index.
22 Index,
23 /// The docs were sourced from local `cargo doc` output.
24 Local,
25 /// The docs were sourced from `docs.rs`.
26 DocsDotRs,
27}
28
29#[derive(Debug)]
30struct RustdocItemWithHistory {
31 pub item: RustdocItem,
32 #[cfg(debug_assertions)]
33 pub history: Vec<String>,
34}
35
36#[async_trait]
37pub trait RustdocProvider {
38 async fn fetch_page(
39 &self,
40 package: &PackageName,
41 item: Option<&RustdocItem>,
42 ) -> Result<Option<String>>;
43}
44
45pub struct RustdocIndexer {
46 provider: Box<dyn RustdocProvider + Send + Sync + 'static>,
47}
48
49impl RustdocIndexer {
50 pub fn new(provider: Box<dyn RustdocProvider + Send + Sync + 'static>) -> Self {
51 Self { provider }
52 }
53}
54
55#[async_trait]
56impl IndexedDocsProvider for RustdocIndexer {
57 fn id(&self) -> ProviderId {
58 ProviderId::rustdoc()
59 }
60
61 fn database_path(&self) -> PathBuf {
62 paths::support_dir().join("docs/rust/rustdoc-db.1.mdb")
63 }
64
65 async fn index(&self, package: PackageName, database: Arc<IndexedDocsDatabase>) -> Result<()> {
66 let Some(package_root_content) = self.provider.fetch_page(&package, None).await? else {
67 return Ok(());
68 };
69
70 let (crate_root_markdown, items) =
71 convert_rustdoc_to_markdown(package_root_content.as_bytes())?;
72
73 database
74 .insert(package.to_string(), crate_root_markdown)
75 .await?;
76
77 let mut seen_items = HashSet::from_iter(items.clone());
78 let mut items_to_visit: VecDeque<RustdocItemWithHistory> =
79 VecDeque::from_iter(items.into_iter().map(|item| RustdocItemWithHistory {
80 item,
81 #[cfg(debug_assertions)]
82 history: Vec::new(),
83 }));
84
85 while let Some(item_with_history) = items_to_visit.pop_front() {
86 let item = &item_with_history.item;
87
88 let Some(result) = self
89 .provider
90 .fetch_page(&package, Some(&item))
91 .await
92 .with_context(|| {
93 #[cfg(debug_assertions)]
94 {
95 format!(
96 "failed to fetch {item:?}: {history:?}",
97 history = item_with_history.history
98 )
99 }
100
101 #[cfg(not(debug_assertions))]
102 {
103 format!("failed to fetch {item:?}")
104 }
105 })?
106 else {
107 continue;
108 };
109
110 let (markdown, referenced_items) = convert_rustdoc_to_markdown(result.as_bytes())?;
111
112 database
113 .insert(format!("{package}::{}", item.display()), markdown)
114 .await?;
115
116 let parent_item = item;
117 for mut item in referenced_items {
118 if seen_items.contains(&item) {
119 continue;
120 }
121
122 seen_items.insert(item.clone());
123
124 item.path.extend(parent_item.path.clone());
125 match parent_item.kind {
126 RustdocItemKind::Mod => {
127 item.path.push(parent_item.name.clone());
128 }
129 _ => {}
130 }
131
132 items_to_visit.push_back(RustdocItemWithHistory {
133 #[cfg(debug_assertions)]
134 history: {
135 let mut history = item_with_history.history.clone();
136 history.push(item.url_path());
137 history
138 },
139 item,
140 });
141 }
142 }
143
144 Ok(())
145 }
146}
147
148pub struct LocalProvider {
149 fs: Arc<dyn Fs>,
150 cargo_workspace_root: PathBuf,
151}
152
153impl LocalProvider {
154 pub fn new(fs: Arc<dyn Fs>, cargo_workspace_root: PathBuf) -> Self {
155 Self {
156 fs,
157 cargo_workspace_root,
158 }
159 }
160}
161
162#[async_trait]
163impl RustdocProvider for LocalProvider {
164 async fn fetch_page(
165 &self,
166 crate_name: &PackageName,
167 item: Option<&RustdocItem>,
168 ) -> Result<Option<String>> {
169 let mut local_cargo_doc_path = self.cargo_workspace_root.join("target/doc");
170 local_cargo_doc_path.push(crate_name.as_ref());
171 if let Some(item) = item {
172 local_cargo_doc_path.push(item.url_path());
173 } else {
174 local_cargo_doc_path.push("index.html");
175 }
176
177 let Ok(contents) = self.fs.load(&local_cargo_doc_path).await else {
178 return Ok(None);
179 };
180
181 Ok(Some(contents))
182 }
183}
184
185pub struct DocsDotRsProvider {
186 http_client: Arc<HttpClientWithUrl>,
187}
188
189impl DocsDotRsProvider {
190 pub fn new(http_client: Arc<HttpClientWithUrl>) -> Self {
191 Self { http_client }
192 }
193}
194
195#[async_trait]
196impl RustdocProvider for DocsDotRsProvider {
197 async fn fetch_page(
198 &self,
199 crate_name: &PackageName,
200 item: Option<&RustdocItem>,
201 ) -> Result<Option<String>> {
202 let version = "latest";
203 let path = format!(
204 "{crate_name}/{version}/{crate_name}{item_path}",
205 item_path = item
206 .map(|item| format!("/{}", item.url_path()))
207 .unwrap_or_default()
208 );
209
210 let mut response = self
211 .http_client
212 .get(
213 &format!("https://docs.rs/{path}"),
214 AsyncBody::default(),
215 true,
216 )
217 .await?;
218
219 let mut body = Vec::new();
220 response
221 .body_mut()
222 .read_to_end(&mut body)
223 .await
224 .context("error reading docs.rs response body")?;
225
226 if response.status().is_client_error() {
227 let text = String::from_utf8_lossy(body.as_slice());
228 bail!(
229 "status error {}, response: {text:?}",
230 response.status().as_u16()
231 );
232 }
233
234 Ok(Some(String::from_utf8(body)?))
235 }
236}