1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate failure_derive;

extern crate chrono;
extern crate failure;
extern crate fnv;
extern crate reqwest;
extern crate semver;
extern crate serde;
extern crate serde_json;
extern crate url;

use chrono::Utc;

use failure::Error;

use fnv::FnvHashMap as Map;

use semver::{Version, VersionReq};

use serde::de::{Deserialize, DeserializeOwned, Deserializer};
use serde_json::Value;

use url::Url;

use std::env;
use std::fmt::{self, Display};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

const PER_PAGE: usize = 100;
const RETRIES: usize = 32;

pub type DateTime = chrono::DateTime<Utc>;

#[derive(Deserialize, Debug)]
pub struct IndexPage {
    pub crates: Vec<IndexCrate>,
    pub meta: Meta,
}

#[derive(Deserialize, Debug)]
pub struct IndexCrate {
    pub name: String,
}

#[derive(Deserialize, Debug)]
pub struct Meta {
    pub total: usize,
}

#[derive(Deserialize, Debug)]
pub struct Crate {
    #[serde(rename = "crate")] pub index: IndexCrate,
    pub versions: Vec<CrateVersion>,
}

#[derive(Deserialize, Debug)]
pub struct Dependencies {
    pub dependencies: Vec<Dependency>,
}

#[derive(Deserialize, Clone, Debug)]
pub struct Dependency {
    #[serde(rename = "crate_id")] pub name: String,
    pub kind: DependencyKind,
    pub req: VersionReq,
    pub optional: bool,
    pub default_features: bool,
    pub features: Vec<String>,
}

#[derive(Deserialize, PartialEq, Clone, Copy, Debug)]
#[serde(rename_all = "lowercase")]
pub enum DependencyKind {
    Normal,
    Build,
    Dev,
}

#[derive(Deserialize, Debug)]
pub struct CrateVersion {
    pub num: Version,
    pub created_at: DateTime,
    pub features: Map<String, Vec<Feature>>,
}

#[derive(Clone, Debug)]
pub enum Feature {
    Current(String),
    Dependency(String, String),
}

impl<'de> Deserialize<'de> for Feature {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let mut s = String::deserialize(deserializer)?;
        Ok(match s.find('/') {
            Some(slash) => {
                let feature = s[slash + 1..].to_owned();
                s.truncate(slash);
                Feature::Dependency(s, feature)
            }
            None => Feature::Current(s),
        })
    }
}

pub fn cache_index(n: usize) -> Result<IndexPage, Error> {
    let endpoint = format!("/api/v1/crates?page={}", n);
    let location = format!("index/{}.json", n);
    cache(endpoint, location)
}

pub fn cache_crate(name: &str) -> Result<Crate, Error> {
    let endpoint = format!("/api/v1/crates/{}", name);
    let location = format!("crate/{}.json", name);
    cache(endpoint, location)
}

pub fn cache_dependencies(name: &str, num: &Version) -> Result<Dependencies, Error> {
    let endpoint = format!("/api/v1/crates/{}/{}/dependencies", name, num);
    let location = format!("dependencies/{}/{}.json", name, num);
    cache(endpoint, location)
}

pub fn num_pages() -> Result<usize, Error> {
    let total = cache_index(1)?.meta.total;
    Ok((total + PER_PAGE - 1) / PER_PAGE)
}

pub fn total_crates() -> Result<usize, Error> {
    Ok(cache_index(1)?.meta.total)
}

#[derive(Debug, Fail)]
#[fail(display = "download did not return success: {}", url)]
struct DownloadError {
    url: Url,
}

#[derive(Debug, Fail)]
struct FileNotFoundError {
    path: PathBuf,
}

impl Display for FileNotFoundError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(
            formatter,
            "file not found, run `cargo tally --init` to download: {}",
            self.path.display()
        )
    }
}

impl Display for DependencyKind {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            DependencyKind::Normal => write!(formatter, "normal"),
            DependencyKind::Build => write!(formatter, "build"),
            DependencyKind::Dev => write!(formatter, "dev"),
        }
    }
}

impl Display for Feature {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Feature::Current(ref feature) => write!(formatter, "{}", feature),
            Feature::Dependency(ref name, ref feature) => write!(formatter, "{}/{}", name, feature),
        }
    }
}

fn cache<U, P, T>(endpoint: U, location: P) -> Result<T, Error>
where
    U: AsRef<str>,
    P: AsRef<Path>,
    T: DeserializeOwned,
{
    let location = location.as_ref();
    assert!(location.is_relative());

    let mut path = PathBuf::from("tally");
    path.push(location);

    if path.exists() {
        let mut file = File::open(&path)?;
        let mut contents = String::new();
        file.read_to_string(&mut contents)?;
        if let Ok(de) = serde_json::from_str(&contents) {
            return Ok(de);
        }
    }

    if env::var("ALLOW_DOWNLOAD").is_err() {
        return Err(Error::from(FileNotFoundError { path }));
    }

    let parent = path.parent().unwrap();
    fs::create_dir_all(parent)?;

    let data: Value = download(endpoint.as_ref())?;
    let de = T::deserialize(&data)?;

    let j = serde_json::to_string_pretty(&data)?;
    let mut file = File::create(&path)?;
    file.write_all(j.as_bytes())?;

    Ok(de)
}

fn download<T>(endpoint: &str) -> Result<T, Error>
where
    T: DeserializeOwned,
{
    let mut url = Url::parse("https://crates.io").unwrap().join(endpoint)?;
    url.query_pairs_mut()
        .append_pair("per_page", &PER_PAGE.to_string());

    let mut resp = retry(|| {
        let resp = reqwest::get(url.clone())?;
        if !resp.status().is_success() {
            return Err(Error::from(DownloadError { url: url.clone() }));
        }
        Ok(resp)
    })?;

    let data = resp.json()?;
    Ok(data)
}

#[cfg(feature = "cargo-clippy")]
fn retry<F, R>(_f: F) -> R
where
    F: Fn() -> R,
{
    let _ = RETRIES;
    unimplemented!()
}

#[cfg(not(feature = "cargo-clippy"))]
fn retry<F, T, E>(f: F) -> Result<T, E>
where
    F: Fn() -> Result<T, E>,
{
    #[allow(dead_code)]
    enum StaticAssert {
        False = false as isize,
        True = (RETRIES > 0) as isize,
    }

    for i in 1usize.. {
        let result = f();
        if result.is_ok() || i == RETRIES {
            return result;
        }
    }

    unreachable!()
}