Skip to content

Commit

Permalink
Use ResourceId name for the trait
Browse files Browse the repository at this point in the history
GitHub Actions
  • Loading branch information
kirillt authored and tareknaser committed May 24, 2024
1 parent de1b60f commit dfae6da
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 95 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ jobs:
run_benchmarks:
name: Run Benchmarks
runs-on: ubuntu-latest

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

- uses: boa-dev/criterion-compare-action@v3
- name: Run Criterion
uses: boa-dev/criterion-compare-action@v3
with:
branchName: ${{ github.base_ref }}
token: ${{ secrets.GITHUB_TOKEN }}
18 changes: 17 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: Verify build

on: [push]
on:
push:
branches:
- main
pull_request:
branches:
- main

env:
CARGO_TERM_COLOR: always
Expand All @@ -12,20 +18,26 @@ jobs:

steps:
- uses: actions/checkout@v4

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

- name: Check
run: cargo check

- name: Format
run: |
cargo fmt --all -- --check
cargo clippy
- name: Build Debug
run: cargo build --verbose

- name: Run tests
run: cargo test --verbose

- name: Build Release
run: cargo build --verbose --release

Expand All @@ -35,8 +47,10 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable

- name: Run tests
run: cargo test --workspace --verbose

Expand All @@ -46,7 +60,9 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable

- name: Run tests
run: cargo test --workspace --verbose
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Main Branch Workflow
name: Weekly testing

on:
push:
Expand All @@ -15,8 +15,10 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable

- name: Run tests
run: cargo test --workspace --verbose --release

Expand All @@ -26,8 +28,10 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable

- name: Run tests
run: cargo test --workspace --verbose --release

Expand All @@ -37,8 +41,10 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable

- name: Run tests
run: cargo test --workspace --verbose --release

Expand All @@ -48,7 +54,9 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable

- name: Run tests
run: cargo test --workspace --verbose --release
2 changes: 1 addition & 1 deletion ark-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use data_pdf::{render_preview_page, PDFQuality};
// Change this to use another type for the resource id if needed.
//
// We define it globally here so that it can be easily changed.
pub(crate) use dev_hash::Crc32ResourceId as ResourceId;
pub(crate) use dev_hash::Crc32 as ResourceId;

use fs_atomic_versions::app_id;
use fs_storage::ARK_FOLDER;
Expand Down
14 changes: 7 additions & 7 deletions data-link/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use data_error::Result;
use data_resource::ResourceIdTrait;
use data_resource::ResourceId;
use fs_atomic_versions::atomic::AtomicFile;
use fs_metadata::store_metadata;
use fs_properties::load_raw_properties;
Expand All @@ -17,7 +17,7 @@ use std::{io::Write, path::PathBuf};
use url::Url;

#[derive(Debug, Deserialize, Serialize)]
pub struct Link<Id: ResourceIdTrait> {
pub struct Link<Id: ResourceId> {
pub url: Url,
pub prop: Properties,
// We need `_marker` to indicate that `Link` is generic over Id
Expand All @@ -30,7 +30,7 @@ pub struct Properties {
pub desc: Option<String>,
}

impl<Id: ResourceIdTrait> Link<Id> {
impl<Id: ResourceId> Link<Id> {
pub fn new(url: Url, title: String, desc: Option<String>) -> Self {
Self {
url,
Expand Down Expand Up @@ -293,15 +293,15 @@ impl OpenGraphTag {
async fn test_create_link_file() {
fs_atomic_versions::initialize();

use dev_hash::Crc32ResourceId as ResourceId;
use dev_hash::Crc32;
use tempdir::TempDir;

let dir = TempDir::new("arklib_test").unwrap();

let root: &Path = dir.path();
println!("temporary root: {}", root.display());
let url = Url::parse("https://kaydee.net/blog/open-graph-image/").unwrap();
let link: Link<ResourceId> = Link::new(
let link: Link<Crc32> = Link::new(
url,
String::from("test_title"),
Some(String::from("test_desc")),
Expand All @@ -317,12 +317,12 @@ async fn test_create_link_file() {
Url::from_str(str::from_utf8(current_bytes.as_bytes()).unwrap())
.unwrap();
assert_eq!(url.as_str(), "https://kaydee.net/blog/open-graph-image/");
let link: Link<ResourceId> = Link::load(root, &path).unwrap();
let link: Link<Crc32> = Link::load(root, &path).unwrap();
assert_eq!(link.url.as_str(), url.as_str());
assert_eq!(link.prop.desc.unwrap(), "test_desc");
assert_eq!(link.prop.title, "test_title");

let id = ResourceId::from_bytes(current_bytes.as_bytes()).unwrap();
let id = Crc32::from_bytes(current_bytes.as_bytes()).unwrap();
let path = Path::new(&root)
.join(ARK_FOLDER)
.join(PREVIEWS_STORAGE_FOLDER)
Expand Down
4 changes: 2 additions & 2 deletions data-resource/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# `data-resource`

`data-resource` is a lightweight minimal dependency crate that defines the `ResourceIdTrait`. This trait specifies constraints for any type that represents a `ResourceId`. It is used by other `ark` crates to make the index generic over the resource ID type and hash function used.
`data-resource` is a lightweight minimal dependency crate that defines the `ResourceId` trait. This trait specifies constraints for any type that represents a `ResourceId`. It is used by other ARK crates to make the index generic over the resource ID type and hash function used.

## Example Implementations

To see example implementations of the `ResourceIdTrait`, please refer to the [`dev-hash`](../dev-hash) crate.
To see example implementations of the `ResourceId` trait, please refer to the [`dev-hash`](../dev-hash) crate.
2 changes: 1 addition & 1 deletion data-resource/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{fmt::Debug, hash::Hash, path::Path};
/// The hash value is used to uniquely identify the resource.
///
/// Implementors of this trait must provide a way to compute the hash value from the resource's data.
pub trait ResourceIdTrait:
pub trait ResourceId:
Debug
+ Display
+ FromStr
Expand Down
10 changes: 5 additions & 5 deletions dev-hash/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# `dev-hash`

`dev-hash` is a crate that provides example implementations of `ResourceIdTrait`. It can be extended to include additional hash function implementations. The crate also includes benchmarks for the defined `ResourceId` types.
`dev-hash` contains reference implementations of `ResourceId` trait and should be used only as an example, or as a dependency in test cases. It will be extended to include additional hash function implementations. The crate also includes benchmarks for the defined `ResourceId` types.

## Defined Types

| Type | Description |
| ------------------ | --------------------------------------------------- |
| `Blake3ResourceId` | Uses the Blake3 cryptographic hash function |
| `Crc32ResourceId` | Uses the CRC32 fast non-cryptographic hash function |
| Type | Description |
|----------|-----------------------------------------------------------------------------|
| `Blake3` | Impl of `ResourceId` that uses the Blake3 cryptographic hash function |
| `Crc32` | Impl of `ResourceId` that uses the CRC32 non-cryptographic hash function |
8 changes: 4 additions & 4 deletions dev-hash/benches/blake3.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use data_resource::ResourceIdTrait;
use data_resource::ResourceId;
use rand::prelude::*;
use std::path::Path;

use dev_hash::Blake3ResourceId as ResourceId;
use dev_hash::Blake3;

// Add files to benchmark here
const FILE_PATHS: [&str; 2] =
Expand Down Expand Up @@ -36,7 +36,7 @@ fn bench_resource_id_creation(c: &mut Criterion) {
let id = format!("compute_from_path:{}", file_path);
group.bench_function(id, move |b| {
b.iter(|| {
ResourceId::from_path(black_box(file_path))
<Blake3 as ResourceId>::from_path(black_box(file_path))
.expect("from_path returned an error")
});
});
Expand All @@ -51,7 +51,7 @@ fn bench_resource_id_creation(c: &mut Criterion) {
let id = format!("compute_from_bytes:{}", name);
group.bench_function(id, move |b| {
b.iter(|| {
ResourceId::from_bytes(black_box(&input_data))
<Blake3 as ResourceId>::from_bytes(black_box(&input_data))
.expect("from_bytes returned an error")
});
});
Expand Down
8 changes: 4 additions & 4 deletions dev-hash/benches/crc32.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use data_resource::ResourceIdTrait;
use data_resource::ResourceId;
use rand::prelude::*;
use std::path::Path;

use dev_hash::Crc32ResourceId as ResourceId;
use dev_hash::Crc32;

// Add files to benchmark here
const FILE_PATHS: [&str; 2] =
Expand Down Expand Up @@ -36,7 +36,7 @@ fn bench_resource_id_creation(c: &mut Criterion) {
let id = format!("compute_from_path:{}", file_path);
group.bench_function(id, move |b| {
b.iter(|| {
ResourceId::from_path(black_box(file_path))
<Crc32 as ResourceId>::from_path(black_box(file_path))
.expect("from_path returned an error")
});
});
Expand All @@ -51,7 +51,7 @@ fn bench_resource_id_creation(c: &mut Criterion) {
let id = format!("compute_from_bytes:{}", name);
group.bench_function(id, move |b| {
b.iter(|| {
ResourceId::from_bytes(black_box(&input_data))
<Crc32 as ResourceId>::from_bytes(black_box(&input_data))
.expect("from_bytes returned an error")
});
});
Expand Down
24 changes: 12 additions & 12 deletions dev-hash/src/blake3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ use hex::encode;
use serde::{Deserialize, Serialize};

use data_error::Result;
use data_resource::ResourceIdTrait;
use data_resource::ResourceId;

/// Represents a resource identifier using the BLAKE3 algorithm.
///
/// Uses [`blake3`] crate to compute the hash value.
#[derive(
Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize,
)]
pub struct ResourceId(pub String);
pub struct Blake3(pub String);

impl FromStr for ResourceId {
impl FromStr for Blake3 {
type Err = hex::FromHexError;

fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
Ok(ResourceId(s.to_string()))
Ok(Blake3(s.to_string()))
}
}

impl Display for ResourceId {
impl Display for Blake3 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}

impl ResourceIdTrait for ResourceId {
impl ResourceId for Blake3 {
fn from_path<P: AsRef<Path>>(file_path: P) -> Result<Self> {
log::debug!("Computing BLAKE3 hash for file: {:?}", file_path.as_ref());

Expand All @@ -51,7 +51,7 @@ impl ResourceIdTrait for ResourceId {
buffer.clear();
}
let hash = hasher.finalize();
Ok(ResourceId(encode(hash.as_bytes())))
Ok(Blake3(encode(hash.as_bytes())))
}

fn from_bytes(bytes: &[u8]) -> Result<Self> {
Expand All @@ -60,7 +60,7 @@ impl ResourceIdTrait for ResourceId {
let mut hasher = Hasher::new();
hasher.update(bytes);
let hash = hasher.finalize();
Ok(ResourceId(encode(hash.as_bytes())))
Ok(Blake3(encode(hash.as_bytes())))
}
}

Expand All @@ -71,19 +71,19 @@ mod tests {
#[test]
fn sanity_check() {
let file_path = Path::new("../test-assets/lena.jpg");
let id = ResourceId::from_path(file_path)
let id = Blake3::from_path(file_path)
.expect("Failed to compute resource identifier");
assert_eq!(
id,
ResourceId("172b4bf148e858b13dde0fc6613413bcb7552e5c4e5c45195ac6c80f20eb5ff5".to_string())
Blake3("172b4bf148e858b13dde0fc6613413bcb7552e5c4e5c45195ac6c80f20eb5ff5".to_string())
);

let raw_bytes = fs::read(file_path).expect("Failed to read file");
let id = ResourceId::from_bytes(&raw_bytes)
let id = <Blake3 as ResourceId>::from_bytes(&raw_bytes)
.expect("Failed to compute resource identifier");
assert_eq!(
id,
ResourceId("172b4bf148e858b13dde0fc6613413bcb7552e5c4e5c45195ac6c80f20eb5ff5".to_string())
Blake3("172b4bf148e858b13dde0fc6613413bcb7552e5c4e5c45195ac6c80f20eb5ff5".to_string())
);
}
}
Loading

0 comments on commit dfae6da

Please sign in to comment.