Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Rust CI

on:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
build-test:
name: Run test and build
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu

- os: ubuntu-24.04-arm
target: aarch64-unknown-linux-gnu

- os: macos-14
target: aarch64-apple-darwin

- os: macos-15
target: aarch64-apple-darwin

- os: macos-15-intel
target: x86_64-apple-darwin

- os: windows-latest
target: x86_64-pc-windows-msvc

runs-on: ${{ matrix.os }}

steps:
- name: Checkout source
uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy

- name: Check fmt and linting
run: make check

- name: Run tests
run: cargo test --target ${{ matrix.target }} --verbose

- name: Build
run: cargo build --target ${{ matrix.target }} --verbose
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ crate-type = ["rlib"]

[dependencies]
fst = "0.4.7"
memmap2 = "0.9.9"
once_cell = "1.21"

[[bin]]
Expand Down
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
build-fst:
cargo run --bin fst_builder


clean:
cargo clean
rm -f purls.fst
rm -rf target

.PHONY: build-fst clean
test:
cargo test

valid:
cargo fmt --all
cargo clippy --all-targets --all-features

check:
cargo fmt --all -- --check
cargo clippy --all-targets --all-features -- -D warnings

.PHONY: build-fst clean test valid check
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![License](https://img.shields.io/badge/License-Apache--2.0-blue.svg?style=for-the-badge)](https://opensource.org/licenses/Apache-2.0)
[![Version](https://img.shields.io/github/v/release/aboutcode-org/purl-validator-rust?style=for-the-badge)](https://github.com/aboutcode-org/purl-validator-rust/releases)
[![Test](https://img.shields.io/github/actions/workflow/status/aboutcode-org/purl-validator-rust/run-test.yml?style=for-the-badge&logo=github)](https://github.com/aboutcode-org/purl-validator-rust/actions)
[![Test](https://img.shields.io/github/actions/workflow/status/aboutcode-org/purl-validator-rust/ci.yml?style=for-the-badge&logo=github)](https://github.com/aboutcode-org/purl-validator-rust/actions)

**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?**

Expand Down Expand Up @@ -53,6 +53,12 @@ Run tests:
make test
```

Fix formatting and linting:

```bash
make valid
```

## License

SPDX-License-Identifier: Apache-2.0
Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
use fst::Set;
use memmap2::Mmap;
use once_cell::sync::Lazy;
use std::env;
use std::fs::File;
use std::path::Path;

static FST_BYTES: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/purls.fst"));
static VALIDATOR: Lazy<Set<Mmap>> = Lazy::new(|| {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("purls.fst");
load_fst(&path)
});

static VALIDATOR: Lazy<Set<&[u8]>> =
Lazy::new(|| Set::new(FST_BYTES).expect("Failed to load FST from embedded bytes"));
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")
}

pub fn validate(packageurl: &str) -> bool {
let trimmed_packageurl = packageurl.trim_end_matches("/");
VALIDATOR.contains(trimmed_packageurl)
}

#[cfg(test)]
mod validate_tests;
11 changes: 11 additions & 0 deletions src/validate_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::*;
use std::path::Path;

#[test]
fn test_validate_with_custom_file() {
let test_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/test_purls.fst");
let validator = load_fst(&test_path);

assert!(validator.contains("pkg:nuget/FluentUtils.EnumExtensions"));
assert!(!validator.contains("pkg:example/nonexistent"));
}
Binary file added tests/data/test_purls.fst
Binary file not shown.