-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.rs
More file actions
66 lines (57 loc) · 1.97 KB
/
lib.rs
File metadata and controls
66 lines (57 loc) · 1.97 KB
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
/*
Copyright (c) nexB Inc. and others. All rights reserved.
ScanCode is a trademark of nexB Inc.
SPDX-License-Identifier: Apache-2.0
See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
See https://github.com/aboutcode-org/purl-validator-rust for support or download.
See https://aboutcode.org for more information about nexB OSS projects.
*/
//! A library to validate whether a PURL actually exists.
//!
//! **purl-validator** is a Rust library for validating
//! [`Package URLs` (PURLs)](https://github.com/package-url/purl-spec).
//! It works fully offline, including in **air-gapped** or **restricted environments**,
//! and answers one key question: **Does the package this PURL represents actually exist?**
//!
//!
//! # Examples
//!
//! Simplest way to use `validate` is as follows:
//!
//! ```
//! use purl_validator::validate;
//!
//! let result: bool = validate("pkg:nuget/FluentValidation");
//! ```
//!
use fst::Set;
use memmap2::Mmap;
use once_cell::sync::Lazy;
use std::env;
use std::fs::File;
use std::path::Path;
static VALIDATOR: Lazy<Set<Mmap>> = Lazy::new(|| {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("purls.fst");
load_fst(&path)
});
fn load_fst(path: &Path) -> Set<Mmap> {
let file = File::open(path).expect("Failed to open FST file");
let mmap = unsafe { Mmap::map(&file).expect("Failed to mmap FST file") };
Set::new(mmap).expect("Failed to load FST from mmap")
}
fn strip_and_check_purl(packageurl: &str, fst_map: &Set<Mmap>) -> bool {
let trimmed_packageurl = packageurl.trim_end_matches("/");
fst_map.contains(trimmed_packageurl)
}
/// Validate a Package URL (PURL)
///
/// Returns `true` if the given base PURL represents an existing package,
/// otherwise returns `false`.
///
/// Use pre-built FST (Finite State Transducer) to perform lookups and confirm whether
/// the **base PURL** exists.
pub fn validate(packageurl: &str) -> bool {
strip_and_check_purl(packageurl, &VALIDATOR)
}
#[cfg(test)]
mod validate_tests;