-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathlib.rs
290 lines (266 loc) Β· 8.26 KB
/
lib.rs
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#![warn(clippy::all)]
#![warn(rust_2018_idioms)]
#![cfg_attr(docsrs, feature(external_doc))]
//! # β cargo-deny
//!
//! [](https://github.com/EmbarkStudios/cargo-deny/actions?workflow=CI)
//! [](https://crates.io/crates/cargo-deny)
//! [](https://embarkstudios.github.io/cargo-deny/)
//! [](https://docs.rs/cargo-deny)
//! [](https://spdx.org/licenses/)
//! [](CODE_OF_CONDUCT.md)
//! [](http://embark.dev)
//!
//! `cargo-deny` is a cargo plugin for linting your dependencies. See the [book π](https://embarkstudios.github.io/cargo-deny/) for in-depth documentation.
//!
//! ## [Quickstart](https://embarkstudios.github.io/cargo-deny/)
//!
//! ```bash
//! cargo install cargo-deny && cargo deny init && cargo deny check
//! ```
//!
//! ## Usage
//!
//! ### [Install](https://embarkstudios.github.io/cargo-deny/cli/index.html) cargo-deny
//!
//! ```bash
//! cargo install cargo-deny
//! ```
//!
//! ### [Initialize](https://embarkstudios.github.io/cargo-deny/cli/init.html) your project
//!
//! ```bash
//! cargo deny init
//! ```
//!
//! ### [Check](https://embarkstudios.github.io/cargo-deny/cli/check.html) your crates
//!
//! ```bash
//! cargo deny check
//! ```
//!
//! #### [Licenses](https://embarkstudios.github.io/cargo-deny/checks/licenses/index.html)
//!
//! The licenses check is used to verify that every crate you use has license terms you find acceptable.
//!
//! ```bash
//! cargo deny check licenses
//! ```
//!
//! #### [Bans](https://embarkstudios.github.io/cargo-deny/checks/bans/index.html)
//!
//! The bans check is used to deny (or allow) specific crates, as well as detect and handle multiple versions of the same crate.
//!
//! ```bash
//! cargo deny check bans
//! ```
//!
//! #### [Advisories](https://embarkstudios.github.io/cargo-deny/checks/advisories/index.html)
//!
//! The advisories check is used to detect issues for crates by looking in an advisory database.
//!
//! ```bash
//! cargo deny check advisories
//! ```
//!
//! #### [Sources](https://embarkstudios.github.io/cargo-deny/checks/sources/index.html)
//!
//! The sources check ensures crates only come from sources you trust.
//!
//! ```bash
//! cargo deny check sources
//! ```
pub use semver::Version;
use std::{cmp, collections::HashMap, path::PathBuf};
pub mod advisories;
pub mod bans;
mod cfg;
pub mod diag;
/// Configuration and logic for checking crate licenses
pub mod licenses;
pub mod sources;
pub use cfg::Spanned;
use krates::cm;
pub use krates::{DepKind, Kid};
/// The possible lint levels for the various lints. These function similarly
/// to the standard [Rust lint levels](https://doc.rust-lang.org/rustc/lints/levels.html)
#[derive(serde::Deserialize, PartialEq, Eq, Clone, Copy, Debug)]
#[serde(rename_all = "snake_case")]
pub enum LintLevel {
/// A debug or info diagnostic _may_ be emitted if the lint is violated
Allow,
/// A warning will be emitted if the lint is violated, but the command
/// will succeed
Warn,
/// An error will be emitted if the lint is violated, and the command
/// will fail with a non-zero exit code
Deny,
}
impl Default for LintLevel {
fn default() -> Self {
LintLevel::Warn
}
}
const fn lint_warn() -> LintLevel {
LintLevel::Warn
}
const fn lint_deny() -> LintLevel {
LintLevel::Deny
}
#[derive(Debug)]
pub struct Krate {
pub name: String,
pub id: Kid,
pub version: Version,
pub source: Option<cm::Source>,
pub authors: Vec<String>,
pub repository: Option<String>,
pub description: Option<String>,
pub manifest_path: PathBuf,
pub license: Option<String>,
pub license_file: Option<PathBuf>,
pub deps: Vec<cm::Dependency>,
pub features: HashMap<String, Vec<String>>,
pub targets: Vec<cm::Target>,
pub publish: Option<Vec<String>>,
}
#[cfg(test)]
impl Default for Krate {
fn default() -> Self {
Self {
name: "".to_owned(),
version: Version::new(0, 1, 0),
authors: Vec::new(),
id: Kid {
repr: "".to_owned(),
},
source: None,
description: None,
deps: Vec::new(),
license: None,
license_file: None,
targets: Vec::new(),
features: HashMap::new(),
manifest_path: PathBuf::new(),
repository: None,
publish: None,
}
}
}
impl PartialOrd for Krate {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Krate {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.id.cmp(&other.id)
}
}
impl PartialEq for Krate {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for Krate {}
impl krates::KrateDetails for Krate {
fn name(&self) -> &str {
&self.name
}
fn version(&self) -> &semver::Version {
&self.version
}
}
impl From<cm::Package> for Krate {
fn from(pkg: cm::Package) -> Self {
Self {
name: pkg.name,
id: pkg.id,
version: pkg.version,
authors: pkg.authors,
repository: pkg.repository,
source: pkg.source,
targets: pkg.targets,
license: pkg.license.map(|lf| {
// cargo used to allow / in place of OR which is not valid
// in SPDX expression, we force correct it here
if lf.contains('/') {
lf.replace('/', " OR ")
} else {
lf
}
}),
license_file: pkg.license_file,
description: pkg.description,
manifest_path: pkg.manifest_path,
deps: {
let mut deps = pkg.dependencies;
deps.sort_by(|a, b| a.name.cmp(&b.name));
deps
},
features: pkg.features,
publish: pkg.publish,
}
}
}
impl Krate {
/// Returns true if the crate is marked as `publish = false`, or
/// it is only published to the specified private registries
pub(crate) fn is_private(&self, private_registries: &[&str]) -> bool {
self.publish
.as_ref()
.map(|v| {
if v.is_empty() {
true
} else {
v.iter()
.all(|reg| private_registries.contains(®.as_str()))
}
})
.unwrap_or(false)
}
}
pub type Krates = krates::Krates<Krate>;
#[inline]
pub fn binary_search<T, Q>(s: &[T], query: &Q) -> Result<usize, usize>
where
T: std::borrow::Borrow<Q>,
Q: Ord + ?Sized,
{
s.binary_search_by(|i| i.borrow().cmp(query))
}
#[inline]
pub fn contains<T, Q>(s: &[T], query: &Q) -> bool
where
T: std::borrow::Borrow<Q>,
Q: Eq + ?Sized,
{
s.iter().any(|i| i.borrow() == query)
}
#[inline]
pub fn hash(data: &[u8]) -> u32 {
use std::hash::Hasher;
// We use the 32-bit hash instead of the 64 even though
// it is significantly slower due to the TOML limitation
// if only supporting i64
let mut xx = twox_hash::XxHash32::default();
xx.write(data);
xx.finish() as u32
}
/// Common context for the various checks. Some checks
/// require additional information though.
pub struct CheckCtx<'ctx, T> {
/// The configuration for the check
pub cfg: T,
/// The krates graph to check
pub krates: &'ctx Krates,
/// The spans for each unique crate in a synthesized "lock file"
pub krate_spans: &'ctx diag::KrateSpans,
/// The codespan file id for the synthesized krate_spans
pub spans_id: codespan::FileId,
}
impl<'ctx, T> CheckCtx<'ctx, T> {
pub(crate) fn label_for_span(&self, krate_index: usize, msg: impl Into<String>) -> diag::Label {
diag::Label::new(self.spans_id, self.krate_spans[krate_index].clone(), msg)
}
}