Skip to content

Commit e56ef1f

Browse files
committed
MAX_LEN
1 parent 7379665 commit e56ef1f

File tree

9 files changed

+47
-22
lines changed

9 files changed

+47
-22
lines changed

lib/bencher_valid/src/benchmark_name.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use serde::{
1313
use crate::ValidError;
1414

1515
// In practice, this may need to be raised all the way up to 4096.
16-
pub(crate) const MAX_BENCHMARK_NAME_LEN: usize = 1024;
16+
const MAX_BENCHMARK_NAME_LEN: usize = 1024;
1717

1818
const BENCHER_IGNORE_SNAKE_CASE: &str = "_bencher_ignore";
1919
const BENCHER_IGNORE_PASCAL_CASE: &str = "BencherIgnore";
@@ -30,18 +30,18 @@ pub struct BenchmarkName(String);
3030
crate::typed_string!(BenchmarkName);
3131

3232
impl BenchmarkName {
33+
pub const MAX_LEN: usize = MAX_BENCHMARK_NAME_LEN;
34+
3335
pub fn try_push(&mut self, separator: char, other: &Self) -> Result<(), ValidError> {
34-
let remaining_capacity = MAX_BENCHMARK_NAME_LEN
35-
.checked_sub(self.0.len())
36-
.unwrap_or_default();
36+
let remaining_capacity = Self::MAX_LEN.checked_sub(self.0.len()).unwrap_or_default();
3737
if other.0.len() < remaining_capacity {
3838
self.0.push(separator);
3939
self.0.push_str(other.as_ref());
4040
debug_assert!(
41-
self.0.len() <= MAX_BENCHMARK_NAME_LEN,
41+
self.0.len() <= Self::MAX_LEN,
4242
"Benchmark name length is {} but should be <= {}",
4343
self.0.len(),
44-
MAX_BENCHMARK_NAME_LEN
44+
Self::MAX_LEN
4545
);
4646
Ok(())
4747
} else {
@@ -122,7 +122,7 @@ mod test {
122122

123123
use crate::BenchmarkName;
124124

125-
use super::{is_valid_benchmark_name, MAX_BENCHMARK_NAME_LEN};
125+
use super::is_valid_benchmark_name;
126126
use pretty_assertions::assert_eq;
127127

128128
#[test]
@@ -142,8 +142,8 @@ mod test {
142142
let benchmark_name_len = benchmark_name.0.len();
143143
assert_eq!(benchmark_name_len, 10);
144144

145-
let other_benchmark_name_bytes: [u8; MAX_BENCHMARK_NAME_LEN - 11] =
146-
[0; MAX_BENCHMARK_NAME_LEN - 11];
145+
let other_benchmark_name_bytes: [u8; BenchmarkName::MAX_LEN - 11] =
146+
[0; BenchmarkName::MAX_LEN - 11];
147147
let other_benchmark_name: BenchmarkName = std::str::from_utf8(&other_benchmark_name_bytes)
148148
.unwrap()
149149
.parse()
@@ -153,11 +153,11 @@ mod test {
153153
// 10 + 1 + 1013 = 1024
154154
assert_eq!(
155155
benchmark_name_len + 1 + other_benchmark_name_len,
156-
MAX_BENCHMARK_NAME_LEN
156+
BenchmarkName::MAX_LEN
157157
);
158158

159159
benchmark_name.try_push('.', &other_benchmark_name).unwrap();
160-
assert_eq!(benchmark_name.0.len(), MAX_BENCHMARK_NAME_LEN);
160+
assert_eq!(benchmark_name.0.len(), BenchmarkName::MAX_LEN);
161161
is_valid_benchmark_name(&benchmark_name.0);
162162
assert_eq!(other_benchmark_name_len, other_benchmark_name.0.len());
163163
}
@@ -168,8 +168,8 @@ mod test {
168168
let benchmark_name_len = benchmark_name.0.len();
169169
assert_eq!(benchmark_name_len, 10);
170170

171-
let other_benchmark_name_bytes: [u8; MAX_BENCHMARK_NAME_LEN - 10] =
172-
[0; MAX_BENCHMARK_NAME_LEN - 10];
171+
let other_benchmark_name_bytes: [u8; BenchmarkName::MAX_LEN - 10] =
172+
[0; BenchmarkName::MAX_LEN - 10];
173173
let other_benchmark_name: BenchmarkName = std::str::from_utf8(&other_benchmark_name_bytes)
174174
.unwrap()
175175
.parse()
@@ -179,7 +179,7 @@ mod test {
179179
// 10 + 1 + 1014 = 1025
180180
assert_eq!(
181181
benchmark_name_len + 1 + other_benchmark_name_len,
182-
MAX_BENCHMARK_NAME_LEN + 1
182+
BenchmarkName::MAX_LEN + 1
183183
);
184184

185185
assert!(benchmark_name.try_push('.', &other_benchmark_name).is_err());

lib/bencher_valid/src/branch_name.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use serde::{
1414
use crate::{Slug, ValidError};
1515

1616
// https://stackoverflow.com/questions/60045157/what-is-the-maximum-length-of-a-github-branch-name
17-
pub(crate) const MAX_BRANCH_LEN: usize = 256;
17+
const MAX_BRANCH_LEN: usize = 256;
1818

1919
#[typeshare::typeshare]
2020
#[derive(Debug, Display, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
@@ -82,6 +82,10 @@ impl Visitor<'_> for BranchNameVisitor {
8282
}
8383
}
8484

85+
impl BranchName {
86+
pub const MAX_LEN: usize = MAX_BRANCH_LEN;
87+
}
88+
8589
#[cfg_attr(feature = "wasm", wasm_bindgen)]
8690
pub fn is_valid_branch_name(branch_name: &str) -> bool {
8791
branch_name.len() <= MAX_BRANCH_LEN && name_partial(branch_name.into()).is_ok()

lib/bencher_valid/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub use secret::Secret;
6060
pub use units::{Units, BYTES, NANOSECONDS, SECONDS};
6161
pub use user_name::UserName;
6262

63-
pub const MAX_LEN: usize = 64;
63+
const MAX_LEN: usize = 64;
6464

6565
#[cfg(feature = "wasm")]
6666
#[wasm_bindgen(start)]

lib/bencher_valid/src/name_id.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,7 @@ impl Visitor<'_> for NameIdVisitor {
126126
NameId::from_str(v).map_err(|_e| E::invalid_value(Unexpected::Str(v), &self))
127127
}
128128
}
129+
130+
impl NameId {
131+
pub const MAX_LEN: usize = crate::MAX_LEN;
132+
}

lib/bencher_valid/src/non_empty.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ impl Visitor<'_> for NonEmptyVisitor {
7878
}
7979
}
8080

81+
impl NonEmpty {
82+
pub const MAX_LEN: usize = crate::MAX_LEN;
83+
}
84+
8185
#[cfg_attr(feature = "wasm", wasm_bindgen)]
8286
pub fn is_valid_non_empty(non_empty: &str) -> bool {
8387
crate::is_valid_non_empty(non_empty)

lib/bencher_valid/src/resource_id.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ impl Visitor<'_> for ResourceIdVisitor {
9898
ResourceId::from_str(v).map_err(|_e| E::invalid_value(Unexpected::Str(v), &self))
9999
}
100100
}
101+
102+
impl ResourceId {
103+
pub const MAX_LEN: usize = crate::MAX_LEN;
104+
}

lib/bencher_valid/src/resource_name.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ impl Visitor<'_> for ResourceNameVisitor {
8484
}
8585
}
8686

87+
impl ResourceName {
88+
pub const MAX_LEN: usize = crate::MAX_LEN;
89+
}
90+
8791
#[cfg_attr(feature = "wasm", wasm_bindgen)]
8892
pub fn is_valid_resource_name(resource_name: &str) -> bool {
8993
is_valid_len(resource_name)

lib/bencher_valid/src/slug.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ pub fn new_slug(slug: &str) -> String {
8787
}
8888

8989
impl Slug {
90-
pub const MAX: usize = MAX_LEN;
90+
pub const MAX_LEN: usize = MAX_LEN;
91+
9192
#[cfg(feature = "server")]
9293
const RAND_LEN: usize = 8;
9394

@@ -96,8 +97,8 @@ impl Slug {
9697
S: AsRef<str>,
9798
{
9899
let new_slug = slug::slugify(&slug);
99-
Self(if new_slug.len() > Self::MAX {
100-
slug::slugify(new_slug.split_at(Slug::MAX).0)
100+
Self(if new_slug.len() > Self::MAX_LEN {
101+
slug::slugify(new_slug.chars().take(Self::MAX_LEN).collect::<String>())
101102
} else {
102103
new_slug
103104
})
@@ -136,9 +137,9 @@ impl Slug {
136137
#[cfg(feature = "server")]
137138
#[must_use]
138139
pub fn with_rand_suffix(self) -> Self {
139-
let truncated = if self.as_ref().len() + 1 + Self::RAND_LEN > Self::MAX {
140-
let mid = Self::MAX - (1 + Self::RAND_LEN);
141-
slug::slugify(self.as_ref().split_at(mid).0)
140+
let truncated = if self.as_ref().len() + 1 + Self::RAND_LEN > Self::MAX_LEN {
141+
let mid = Self::MAX_LEN - (1 + Self::RAND_LEN);
142+
slug::slugify(self.as_ref().chars().take(mid).collect::<String>())
142143
} else {
143144
self.0
144145
};

lib/bencher_valid/src/user_name.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ impl Visitor<'_> for UserNameVisitor {
8282
}
8383
}
8484

85+
impl UserName {
86+
pub const MAX_LEN: usize = crate::MAX_LEN;
87+
}
88+
8589
#[cfg_attr(feature = "wasm", wasm_bindgen)]
8690
pub fn is_valid_user_name(name: &str) -> bool {
8791
if !is_valid_len(name) {

0 commit comments

Comments
 (0)