Extract ExtensionIndexedDocsProvider to indexed_docs crate (#20607)

This PR extracts the `ExtensionIndexedDocsProvider` implementation to
the `indexed_docs` crate.

To achieve this, we introduce a new `Extension` trait that provides an
abstracted interface for calling an extension. This trait resides in the
`extension` crate, which has minimal dependencies and can be depended on
by other crates, like `indexed_docs`.

We're then able to implement the `ExtensionIndexedDocsProvider` without
having any knowledge of the Wasm-specific internals of the extension
system.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-11-13 11:19:55 -05:00 committed by GitHub
parent 7832883c74
commit b084d53f8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 177 additions and 127 deletions

View file

@ -17,6 +17,7 @@ async-trait.workspace = true
cargo_metadata.workspace = true
collections.workspace = true
derive_more.workspace = true
extension.workspace = true
fs.workspace = true
futures.workspace = true
fuzzy.workspace = true
@ -30,7 +31,6 @@ paths.workspace = true
serde.workspace = true
strum.workspace = true
util.workspace = true
extension_host.workspace = true
[dev-dependencies]
indoc.workspace = true

View file

@ -0,0 +1,53 @@
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;
use extension::Extension;
use crate::{IndexedDocsDatabase, IndexedDocsProvider, PackageName, ProviderId};
pub struct ExtensionIndexedDocsProvider {
extension: Arc<dyn Extension>,
id: ProviderId,
}
impl ExtensionIndexedDocsProvider {
pub fn new(extension: Arc<dyn Extension>, id: ProviderId) -> Self {
Self { extension, id }
}
}
#[async_trait]
impl IndexedDocsProvider for ExtensionIndexedDocsProvider {
fn id(&self) -> ProviderId {
self.id.clone()
}
fn database_path(&self) -> PathBuf {
let mut database_path = PathBuf::from(self.extension.work_dir().as_ref());
database_path.push(self.extension.manifest().id.as_ref());
database_path.push("docs");
database_path.push(format!("{}.0.mdb", self.id));
database_path
}
async fn suggest_packages(&self) -> Result<Vec<PackageName>> {
let packages = self
.extension
.suggest_docs_packages(self.id.0.clone())
.await?;
Ok(packages
.into_iter()
.map(|package| PackageName::from(package.as_str()))
.collect())
}
async fn index(&self, package: PackageName, database: Arc<IndexedDocsDatabase>) -> Result<()> {
self.extension
.index_docs(self.id.0.clone(), package.as_ref().into(), database)
.await
}
}

View file

@ -1,7 +1,9 @@
mod extension_indexed_docs_provider;
mod providers;
mod registry;
mod store;
pub use crate::extension_indexed_docs_provider::ExtensionIndexedDocsProvider;
pub use crate::providers::rustdoc::*;
pub use crate::registry::*;
pub use crate::store::*;

View file

@ -2,7 +2,6 @@ mod item;
mod to_markdown;
use cargo_metadata::MetadataCommand;
use extension_host::DocsDatabase;
use futures::future::BoxFuture;
pub use item::*;
use parking_lot::RwLock;
@ -209,7 +208,7 @@ impl IndexedDocsProvider for DocsDotRsProvider {
async fn index_rustdoc(
package: PackageName,
database: Arc<dyn DocsDatabase>,
database: Arc<IndexedDocsDatabase>,
fetch_page: impl Fn(&PackageName, Option<&RustdocItem>) -> BoxFuture<'static, Result<Option<String>>>
+ Send
+ Sync,

View file

@ -324,10 +324,8 @@ impl IndexedDocsDatabase {
Ok(any)
})
}
}
impl extension_host::DocsDatabase for IndexedDocsDatabase {
fn insert(&self, key: String, docs: String) -> Task<Result<()>> {
pub fn insert(&self, key: String, docs: String) -> Task<Result<()>> {
let env = self.env.clone();
let entries = self.entries;
@ -339,3 +337,9 @@ impl extension_host::DocsDatabase for IndexedDocsDatabase {
})
}
}
impl extension::KeyValueStoreDelegate for IndexedDocsDatabase {
fn insert(&self, key: String, docs: String) -> Task<Result<()>> {
IndexedDocsDatabase::insert(&self, key, docs)
}
}