-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from MathiasPius/sparse-index
Use crates.io sparse index
- Loading branch information
Showing
10 changed files
with
521 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Change Log | ||
|
||
## 0.0.4 | ||
|
||
### Added | ||
* Implemented CrateCache which can be used with either of the crates.io backends. | ||
* Implemented crates.io sparse index backend and set it as the default. | ||
|
||
### Fixed | ||
* Left-over test code would create file CRATE_CACHE_DIR/test. | ||
|
||
## 0.0.3 | ||
|
||
### Fixed | ||
* Fix vulnerability in [rustls-webpki](https://github.com/briansmith/webpki/issues/69) | ||
* Check crate versions immediately on open, instead of only on change. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use async_trait::async_trait; | ||
use hyper::{client::HttpConnector, Body, Request}; | ||
use hyper_rustls::HttpsConnector; | ||
use semver::Version; | ||
use serde::Deserialize; | ||
|
||
use super::{CrateError, CrateLookup}; | ||
|
||
type HyperClient = hyper::Client<HttpsConnector<HttpConnector>>; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct CrateApi { | ||
client: HyperClient, | ||
} | ||
|
||
#[async_trait] | ||
impl CrateLookup for CrateApi { | ||
async fn get_latest_version(self, crate_name: String) -> Result<Version, CrateError> { | ||
let response = self | ||
.client | ||
.request( | ||
Request::builder() | ||
.uri(&format!("https://crates.io/api/v1/crates/{crate_name}")) | ||
.header( | ||
"User-Agent", | ||
"crates-lsp (github.com/MathiasPius/crates-lsp)", | ||
) | ||
.header("Accept", "application/json") | ||
.body(Body::empty()) | ||
.map_err(CrateError::transport)?, | ||
) | ||
.await | ||
.map_err(CrateError::transport)?; | ||
|
||
let body = hyper::body::to_bytes(response.into_body()) | ||
.await | ||
.map_err(CrateError::transport)?; | ||
|
||
let stringified = String::from_utf8_lossy(&body); | ||
|
||
#[derive(Deserialize)] | ||
struct CrateInner { | ||
pub max_stable_version: Version, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Crate { | ||
#[serde(rename = "crate")] | ||
pub inner: CrateInner, | ||
} | ||
|
||
let details: Crate = | ||
serde_json::from_str(&stringified).map_err(CrateError::Deserialization)?; | ||
|
||
Ok(details.inner.max_stable_version) | ||
} | ||
} | ||
|
||
impl Default for CrateApi { | ||
fn default() -> Self { | ||
let https = hyper_rustls::HttpsConnectorBuilder::new() | ||
.with_native_roots() | ||
.https_only() | ||
.enable_http1() | ||
.build(); | ||
let client = hyper::Client::builder().build(https); | ||
|
||
CrateApi { client } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::crates::{api::CrateApi, cache::CrateCache, CrateLookup}; | ||
|
||
#[tokio::test] | ||
async fn get_common_crates() { | ||
let api = CrateApi::default(); | ||
|
||
let cache = CrateCache::default(); | ||
|
||
let versions = api | ||
.fetch_versions(cache, &["serde", "log", "tracing", "crate-does-not-exist"]) | ||
.await; | ||
|
||
println!("{versions:#?}"); | ||
} | ||
} |
Oops, something went wrong.