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+
131use fst:: Set ;
232use memmap2:: Mmap ;
333use 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) ]
0 commit comments