[![reqwest-badge]][reqwest] [![serde-badge]][serde] [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding]
在方便的 Rust 迭代器中,包装一个分页的 Web API。迭代器在到达每一页的末尾时,从远程服务器惰性地获取下一页的结果。
#[macro_use]
extern crate serde_derive;
extern crate reqwest;
use reqwest::Error;
#[derive(Deserialize)]
struct ApiResponse {
dependencies: Vec<Dependency>,
meta: Meta,
}
#[derive(Deserialize)]
struct Dependency {
crate_id: String,
}
#[derive(Deserialize)]
struct Meta {
total: u32,
}
struct ReverseDependencies {
crate_id: String,
dependencies: <Vec<Dependency> as IntoIterator>::IntoIter,
client: reqwest::Client,
page: u32,
per_page: u32,
total: u32,
}
impl ReverseDependencies {
fn of(crate_id: &str) -> Result<Self, Error> {
Ok(ReverseDependencies {
crate_id: crate_id.to_owned(),
dependencies: vec![].into_iter(),
client: reqwest::Client::new(),
page: 0,
per_page: 100,
total: 0,
})
}
fn try_next(&mut self) -> Result<Option<Dependency>, Error> {
if let Some(dep) = self.dependencies.next() {
return Ok(Some(dep));
}
if self.page > 0 && self.page * self.per_page >= self.total {
return Ok(None);
}
self.page += 1;
let url = format!("https://crates.io/api/v1/crates/{}/reverse_dependencies?page={}&per_page={}",
self.crate_id,
self.page,
self.per_page);
let response = self.client.get(&url).send()?.json::<ApiResponse>()?;
self.dependencies = response.dependencies.into_iter();
self.total = response.meta.total;
Ok(self.dependencies.next())
}
}
impl Iterator for ReverseDependencies {
type Item = Result<Dependency, Error>;
fn next(&mut self) -> Option<Self::Item> {
match self.try_next() {
Ok(Some(dep)) => Some(Ok(dep)),
Ok(None) => None,
Err(err) => Some(Err(err)),
}
}
}
fn main() -> Result<(), Error> {
for dep in ReverseDependencies::of("serde")? {
println!("reverse dependency: {}", dep?.crate_id);
}
Ok(())
}