Skip to content

Commit 9e14fa3

Browse files
committed
encode_fingerprint
1 parent 06ce69e commit 9e14fa3

File tree

7 files changed

+27
-20
lines changed

7 files changed

+27
-20
lines changed

lib/bencher_context/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ license-file.workspace = true
77
publish = false
88

99
[features]
10-
client = ["dep:gix", "dep:uuid", "dep:windows"]
11-
server = ["dep:uuid", "bencher_valid/server", "uuid/v4"]
10+
client = ["dep:gix", "dep:uuid", "dep:windows", "bencher_valid/client"]
11+
server = ["bencher_valid/server"]
1212
schema = ["dep:schemars"]
1313

1414
[dependencies]

lib/bencher_context/src/server/base36.rs renamed to lib/bencher_context/src/client/platform/base36.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@ use bencher_valid::BASE_36;
22
use uuid::Uuid;
33

44
pub fn encode_uuid(uuid: Uuid) -> String {
5+
const ENCODED_LEN: usize = 26;
6+
57
let base = BASE_36.len() as u128;
68
let chars = BASE_36.chars().collect::<Vec<_>>();
79

8-
let mut num = uuid.as_u128();
10+
// Treat the bytes as a single large number to preserve entropy
11+
let bytes = uuid.as_bytes();
12+
let mut num = 0u128;
13+
for &byte in bytes {
14+
num = (num << 8) | u128::from(byte);
15+
}
16+
917
let mut result = String::new();
18+
#[allow(clippy::cast_possible_truncation)]
1019
while num > 0 {
1120
let remainder = (num % base) as usize;
1221
if let Some(c) = chars.get(remainder) {
@@ -15,7 +24,11 @@ pub fn encode_uuid(uuid: Uuid) -> String {
1524
num /= base;
1625
}
1726

18-
result.chars().rev().collect()
27+
result
28+
.chars()
29+
.chain(std::iter::repeat('0'))
30+
.take(ENCODED_LEN)
31+
.collect()
1932
}
2033

2134
#[cfg(test)]
@@ -26,12 +39,12 @@ mod tests {
2639
#[test]
2740
fn test_encode_uuid() {
2841
let uuid = Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap();
29-
assert_eq!(encode_uuid(uuid), "");
42+
assert_eq!(encode_uuid(uuid), "00000000000000000000000000");
3043

3144
let uuid = Uuid::parse_str("ffffffff-ffff-ffff-ffff-ffffffffffff").unwrap();
32-
assert_eq!(encode_uuid(uuid), "f5lxx1zz5pnorynqglhzmsp33");
45+
assert_eq!(encode_uuid(uuid), "33psmzhlgqnyronp5zz1xxl5f0");
3346

3447
let uuid = Uuid::parse_str("12345678-1234-5678-1234-567812345678").unwrap();
35-
assert_eq!(encode_uuid(uuid), "12srddy53kndus0lmbgjgy7i0");
48+
assert_eq!(encode_uuid(uuid), "0i7ygjgbml0sudnk35yddrs210");
3649
}
3750
}

lib/bencher_context/src/client/platform/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use std::fmt;
22

33
use uuid::Uuid;
44

5+
mod base36;
56
mod target_os;
67

78
#[derive(Debug, Clone, Copy)]
89
pub struct Fingerprint(Uuid);
910

1011
impl fmt::Display for Fingerprint {
1112
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12-
write!(f, "{}", self.0)
13+
write!(f, "{}", base36::encode_uuid(self.0))
1314
}
1415
}
1516

lib/bencher_context/src/client/platform/target_os/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn digital_product_id() -> Option<Uuid> {
5757
let digital_product_id = data
5858
.into_iter()
5959
.take(data_size as usize)
60-
.fold(0u128, |acc, byte| acc.overflowing_add(byte.into()).0);
60+
.fold(0u128, |acc, byte| (acc << 8) | u128::from(byte));
6161
Some(Uuid::from_u128(digital_product_id))
6262
}
6363

lib/bencher_context/src/server/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use bencher_valid::{ResourceName, Slug};
2-
use uuid::Uuid;
32

43
use crate::{ContextPath, RunContext};
54

6-
mod base36;
7-
85
#[allow(clippy::multiple_inherent_impl)]
96
impl RunContext {
107
fn get(&self, path: &str) -> Option<&String> {
@@ -36,8 +33,9 @@ impl RunContext {
3633
self.get(ContextPath::TESTBED_OS).map(String::as_str)
3734
}
3835

39-
pub fn testbed_fingerprint(&self) -> Option<Uuid> {
40-
self.get(ContextPath::TESTBED_FINGERPRINT)?.parse().ok()
36+
pub fn testbed_fingerprint(&self) -> Option<&str> {
37+
self.get(ContextPath::TESTBED_FINGERPRINT)
38+
.map(String::as_str)
4139
}
4240

4341
pub fn name(&self) -> Option<ResourceName> {
@@ -52,8 +50,6 @@ impl RunContext {
5250
let hash = self.repo_hash().map(short_hash).unwrap_or_default();
5351
let fingerprint = self
5452
.testbed_fingerprint()
55-
.map(base36::encode_uuid)
56-
.as_deref()
5753
.map(short_fingerprint)
5854
.unwrap_or_default();
5955
// The spaces here are important,

lib/bencher_valid/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ mod url;
2727
mod user_name;
2828

2929
pub use crate::git_hash::GitHash;
30-
pub use crate::slug::Slug;
31-
#[cfg(feature = "server")]
32-
pub use crate::slug::BASE_36;
30+
pub use crate::slug::{Slug, BASE_36};
3331
pub use crate::url::Url;
3432
pub use benchmark_name::BenchmarkName;
3533
pub use branch_name::BranchName;

lib/bencher_valid/src/slug.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use serde::{
1212

1313
use crate::{is_valid_len, ValidError, MAX_LEN};
1414

15-
#[cfg(feature = "server")]
1615
pub const BASE_36: &str = "0123456789abcdefghijklmnopqrstuvwxyz";
1716

1817
#[typeshare::typeshare]

0 commit comments

Comments
 (0)