Skip to content

Commit 4bce1f0

Browse files
authored
Merge pull request #3 from aboutcode-org/docs
Add documentation for docs.rs
2 parents 1d3df4f + 0e1d252 commit 4bce1f0

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ clean:
77
rm -rf target
88

99
test:
10-
cargo test
10+
cargo test -- --nocapture
1111

1212
valid:
1313
cargo fmt --all
@@ -17,4 +17,7 @@ check:
1717
cargo fmt --all -- --check
1818
cargo clippy --all-targets --all-features -- -D warnings
1919

20-
.PHONY: build-fst clean test valid check
20+
doc:
21+
cargo doc --open
22+
23+
.PHONY: build-fst clean test valid check doc

fst_builder/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
3+
Copyright (c) nexB Inc. and others. All rights reserved.
4+
ScanCode is a trademark of nexB Inc.
5+
SPDX-License-Identifier: Apache-2.0
6+
See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
7+
See https://github.com/aboutcode-org/purl-validator-rust for support or download.
8+
See https://aboutcode.org for more information about nexB OSS projects.
9+
10+
*/
11+
112
use fst::SetBuilder;
213
use std::fs::File;
314
use std::io::{BufRead, BufReader};

src/lib.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/*
2+
3+
Copyright (c) nexB Inc. and others. All rights reserved.
4+
ScanCode is a trademark of nexB Inc.
5+
SPDX-License-Identifier: Apache-2.0
6+
See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
7+
See https://github.com/aboutcode-org/purl-validator-rust for support or download.
8+
See https://aboutcode.org for more information about nexB OSS projects.
9+
10+
*/
11+
12+
//! A library to validate whether a PURL actually exists.
13+
//!
14+
//! **purl-validator** is a Rust library for validating
15+
//! [`Package URLs` (PURLs)](https://github.com/package-url/purl-spec).
16+
//! It works fully offline, including in **air-gapped** or **restricted environments**,
17+
//! and answers one key question: **Does the package this PURL represents actually exist?**
18+
//!
19+
//!
20+
//! # Examples
21+
//!
22+
//! Simplest way to use `validate` is as follows:
23+
//!
24+
//! ```
25+
//! use purl_validator::validate;
26+
//!
27+
//! let result: bool = validate("pkg:nuget/FluentValidation");
28+
//! ```
29+
//!
30+
131
use fst::Set;
232
use memmap2::Mmap;
333
use once_cell::sync::Lazy;
@@ -16,9 +46,20 @@ fn load_fst(path: &Path) -> Set<Mmap> {
1646
Set::new(mmap).expect("Failed to load FST from mmap")
1747
}
1848

19-
pub fn validate(packageurl: &str) -> bool {
49+
fn strip_and_check_purl(packageurl: &str, fst_map: &Set<Mmap>) -> bool {
2050
let trimmed_packageurl = packageurl.trim_end_matches("/");
21-
VALIDATOR.contains(trimmed_packageurl)
51+
fst_map.contains(trimmed_packageurl)
52+
}
53+
54+
/// Validate a Package URL (PURL)
55+
///
56+
/// Returns `true` if the given base PURL represents an existing package,
57+
/// otherwise returns `false`.
58+
///
59+
/// Use pre-built FST (Finite State Transducer) to perform lookups and confirm whether
60+
/// the **base PURL** exists.
61+
pub fn validate(packageurl: &str) -> bool {
62+
strip_and_check_purl(packageurl, &VALIDATOR)
2263
}
2364

2465
#[cfg(test)]

src/validate_tests.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
3+
Copyright (c) nexB Inc. and others. All rights reserved.
4+
ScanCode is a trademark of nexB Inc.
5+
SPDX-License-Identifier: Apache-2.0
6+
See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
7+
See https://github.com/aboutcode-org/purl-validator-rust for support or download.
8+
See https://aboutcode.org for more information about nexB OSS projects.
9+
10+
*/
11+
112
use super::*;
213
use std::path::Path;
314

@@ -6,6 +17,21 @@ fn test_validate_with_custom_file() {
617
let test_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/test_purls.fst");
718
let validator = load_fst(&test_path);
819

20+
assert!(strip_and_check_purl(
21+
"pkg:nuget/FluentUtils.EnumExtensions",
22+
&validator
23+
));
24+
assert!(!strip_and_check_purl("pkg:example/nonexistent", &validator));
25+
}
26+
27+
#[test]
28+
fn test_validate_with_packageurl_trailing_slash() {
29+
let test_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/test_purls.fst");
30+
let validator = load_fst(&test_path);
31+
932
assert!(validator.contains("pkg:nuget/FluentUtils.EnumExtensions"));
10-
assert!(!validator.contains("pkg:example/nonexistent"));
33+
assert!(strip_and_check_purl(
34+
"pkg:nuget/FluentUtils.EnumExtensions/",
35+
&validator
36+
));
1137
}

0 commit comments

Comments
 (0)