Hello rustacians
I need help regarding the api method used to get the user belonging crate info in cargo
Do you mean you want to read the authors
field in the manifest file Cargo.toml
?
If so, then you can read the CARGO_PKG_AUTHORS
environment variable set by cargo
at compile time. If you use clap
, you can also have a look at the crate_authors
macro that adds some formatting features.
Here's an example.
Cargo.toml
:
[package]
name = "authors"
version = "0.1.0"
edition = "2021"
authors = ["Anders Andersson"]
[dependencies]
src/main.rs
:
fn main() {
let authors = env!("CARGO_PKG_AUTHORS");
println!("Hello from {authors}!");
}
Output:
$ cargo run
Compiling authors v0.1.0 (/tmp/authors)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s
Running `target/debug/authors`
Hello from Anders Andersson!
yep
i need to get all the crates info belonging to author
but in api call like /v1/users/
API of what? Are you building a backend for a website or something?
api of author created cargo crate list
It's still not clear to me what you are asking. Are you perhaps conflating the words cargo
and crates.io?
Do you want to get a list of all the crates published on crates.io that are owned by some user? For example by dtolnay? [1]
I’m assuming what you want is to get a list of all the crates owned by a specific user on crates.io programmatically.
# Cargo.toml
[dependencies]
crates_io_api = "0.11"
// src/main.rs
use crates_io_api::{CratesQuery, SyncClient};
fn main() {
let username = "HashiramaSenjuhari";
// See https://crates.io/policies#crawlers
let client = SyncClient::new(
"bot_agent (some way to contact you, the bot runner)",
std::time::Duration::from_secs(1),
).unwrap();
let user = client.user(username).unwrap();
let query = CratesQuery::builder()
.sort(crates_io_api::Sort::RecentUpdates)
.user_id(user.id)
.build();
let crates = client.crates(query).unwrap();
let on_page = crates.meta.total;
println!("Found {on_page} crates by {username}:");
for krate in &crates.crates {
println!("- crate {}", krate.name);
}
}