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
use std::ascii::AsciiExt;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
extern crate git2;
extern crate glob;
extern crate rustc_serialize;
static INDEX_GIT_URL: &'static str = "https://github.com/rust-lang/crates.io-index";
#[derive(RustcDecodable, Clone)]
pub struct Version {
name: String,
vers: String,
deps: Vec<Dependency>,
cksum: String,
features: HashMap<String, Vec<String>>,
yanked: bool,
}
impl Version {
pub fn name(&self) -> &str {
&self.name
}
pub fn version(&self) -> &str {
&self.vers
}
pub fn dependencies(&self) -> &[Dependency] {
&self.deps
}
pub fn checksum(&self) -> &str {
&self.cksum
}
pub fn features(&self) -> &HashMap<String, Vec<String>> {
&self.features
}
pub fn is_yanked(&self) -> bool {
self.yanked
}
}
#[derive(RustcDecodable, Clone)]
pub struct Dependency {
name: String,
req: String,
features: Vec<String>,
optional: bool,
default_features: bool,
target: Option<String>,
kind: Option<String>,
}
impl Dependency {
pub fn name(&self) -> &str {
&self.name
}
pub fn requirement(&self) -> &str {
&self.req
}
pub fn features(&self) -> &[String] {
&self.features
}
pub fn is_optional(&self) -> bool {
self.optional
}
pub fn has_default_features(&self) -> bool {
self.default_features
}
pub fn target(&self) -> Option<&str> {
match self.target {
Some(ref s) => Some(s),
None => None,
}
}
pub fn kind(&self) -> Option<&str> {
match self.kind {
Some(ref s) => Some(s),
None => None,
}
}
}
pub struct Crates(CrateIndexPaths);
impl Iterator for Crates {
type Item = Crate;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|p| Crate::new(&p))
}
}
pub struct CrateIndexPaths(std::iter::Chain<glob::Paths, glob::Paths>);
impl CrateIndexPaths {
fn new<P: AsRef<Path>>(path: P) -> CrateIndexPaths {
let mut match_options = glob::MatchOptions::new();
match_options.require_literal_leading_dot = true;
let path = path.as_ref();
let glob_pattern = format!("{}/*/*/*", path.to_str().unwrap());
let index_paths1 = glob::glob_with(&glob_pattern, &match_options).unwrap();
let glob_pattern = format!("{}/[12]/*", path.to_str().unwrap());
let index_paths2 = glob::glob_with(&glob_pattern, &match_options).unwrap();
CrateIndexPaths(index_paths1.chain(index_paths2))
}
}
impl Iterator for CrateIndexPaths {
type Item = PathBuf;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|glob_result| glob_result.unwrap())
}
}
pub struct Index {
path: PathBuf,
}
impl Index {
pub fn new(path: PathBuf) -> Index {
Index { path: path }
}
pub fn exists(&self) -> bool {
fs::metadata(&self.path).is_ok()
}
pub fn fetch(&self) -> Result<(), git2::Error> {
let _ = try!(git2::Repository::clone(INDEX_GIT_URL, &self.path));
Ok(())
}
pub fn crate_(&self, crate_name: &str) -> Option<Crate> {
self.crate_index_paths()
.find(|path| {
path.file_name()
.and_then(OsStr::to_str)
.map(|file_name| file_name.eq_ignore_ascii_case(crate_name))
.unwrap_or(false)
})
.map(|p| Crate::new(&p))
}
pub fn crates(&self) -> Crates {
Crates(self.crate_index_paths())
}
pub fn crate_index_paths(&self) -> CrateIndexPaths {
CrateIndexPaths::new(self.path.clone())
}
}
pub struct Crate {
versions: Vec<Version>,
}
impl Crate {
pub fn new<P: AsRef<Path>>(index_path: P) -> Crate {
let index_path = index_path.as_ref();
let mut versions = vec![];
let file = fs::File::open(&index_path).unwrap();
for line in BufReader::new(file).lines() {
let version: Version = rustc_serialize::json::decode(&line.unwrap()).unwrap();
versions.push(version);
}
Crate { versions: versions }
}
pub fn versions(&self) -> &[Version] {
&self.versions
}
pub fn earliest_version(&self) -> &Version {
&self.versions[0]
}
pub fn latest_version(&self) -> &Version {
&self.versions[self.versions.len() - 1]
}
}
#[test]
fn test_dependencies() {
let index = Index::new("_test".into());
if !index.exists() {
index.fetch().unwrap();
}
let crate_ = index.crates().nth(0).unwrap();
let version = crate_.latest_version();
let _ = version.deps;
}