-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #12939 - Alexendoo:lintcheck-popular-crates, r=xFrednet
Merge lintcheck popular-crates bin as a subcommand Also rewrites it to use `ureq` to drop some heavy dependencies as `crates_io_api` brings in `reqwest` r? `@xFrednet` changelog: none
- Loading branch information
Showing
6 changed files
with
77 additions
and
88 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
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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use serde::Deserialize; | ||
use std::error::Error; | ||
use std::fmt::Write; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct Page { | ||
crates: Vec<Crate>, | ||
meta: Meta, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct Crate { | ||
name: String, | ||
max_version: String, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct Meta { | ||
next_page: String, | ||
} | ||
|
||
pub(crate) fn fetch(output: PathBuf, number: usize) -> Result<(), Box<dyn Error>> { | ||
let agent = ureq::builder() | ||
.user_agent("clippy/lintcheck (github.com/rust-lang/rust-clippy/)") | ||
.build(); | ||
|
||
let mut crates = Vec::with_capacity(number); | ||
let mut query = "?sort=recent-downloads&per_page=100".to_string(); | ||
while crates.len() < number { | ||
let page: Page = agent | ||
.get(&format!("https://crates.io/api/v1/crates{query}")) | ||
.call()? | ||
.into_json()?; | ||
|
||
query = page.meta.next_page; | ||
crates.extend(page.crates); | ||
crates.truncate(number); | ||
|
||
let width = number.ilog10() as usize + 1; | ||
println!("Fetched {:>width$}/{number} crates", crates.len()); | ||
} | ||
|
||
let mut out = "[crates]\n".to_string(); | ||
for Crate { name, max_version } in crates { | ||
writeln!(out, "{name} = {{ name = '{name}', versions = ['{max_version}'] }}").unwrap(); | ||
} | ||
fs::write(output, out)?; | ||
|
||
Ok(()) | ||
} |