crates-index 3.3.0

Library for retrieving and interacting with the crates.io index
Documentation
use crates_index::SparseIndex;
use std::io;

///
/// **important**:<br>
/// dont forget to enable the **http-interop** feature of **ureq**
///
/// command to run:<br>
/// cargo run --example sparse_http_ureq -F sparse
///

const CRATE_TO_FETCH: &str = "inferno";

fn main() {
    let mut index = SparseIndex::new_cargo_default().unwrap();

    print_crate(&mut index);
    update(&mut index);
    print_crate(&mut index);
}

fn print_crate(index: &mut SparseIndex) {
    match index.crate_from_cache(CRATE_TO_FETCH) {
        Ok(krate) => {
            println!("{:?}", krate.highest_normal_version().unwrap().version());
        }
        Err(_err) => {
            println!("could not find crate {}", CRATE_TO_FETCH)
        }
    }
}

fn update(index: &mut SparseIndex) {
    let request: ureq::Request = index.make_cache_request(CRATE_TO_FETCH).unwrap().try_into().unwrap();

    let response = request
        .call()
        .map_err(|_e| io::Error::new(io::ErrorKind::InvalidInput, "connection error"))
        .unwrap();

    index.parse_cache_response(CRATE_TO_FETCH, response.into(), true).unwrap();
}