Skip to content

Commit 0c8b4fc

Browse files
committed
Rename fn to ensure_max_string_length
1 parent a9cbd52 commit 0c8b4fc

4 files changed

Lines changed: 31 additions & 25 deletions

File tree

crates/stackable-operator/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file.
77
### Added
88

99
- Add the Cargo feature `kube-cel` that enables the `cel` feature on the `kube` crate ([#1259]).
10-
- Add `length_enforcement::ensure_max_length` and `Key::shortened_to_valid_length` helper functions ([#1260]).
10+
- Add `length_enforcement::ensure_max_string_length` and `Key::shortened_to_valid_length` helper functions ([#1260]).
1111

1212
[#1259]: https://github.com/stackabletech/operator-rs/pull/1259
1313
[#1260]: https://github.com/stackabletech/operator-rs/pull/1260

crates/stackable-operator/src/kvp/key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fmt::Display, ops::Deref, str::FromStr, sync::LazyLock};
33
use regex::Regex;
44
use snafu::{ResultExt, Snafu, ensure};
55

6-
use crate::utils::length_enforcement::ensure_max_length;
6+
use crate::utils::length_enforcement::ensure_max_string_length;
77

88
const KEY_PREFIX_MAX_LEN: usize = 253;
99
const KEY_NAME_MAX_LEN: usize = 63;
@@ -139,7 +139,7 @@ impl Deref for Key {
139139
impl Key {
140140
/// (Optionally) shortens the `prefix` and `name` to make sure they produce a valid [`Key`].
141141
///
142-
/// See [`ensure_max_length`] for details on the shortening algorithm.
142+
/// See [`ensure_max_string_length`] for details on the shortening algorithm.
143143
pub fn shortened_to_valid_length(
144144
prefix: Option<&str>,
145145
name: impl Into<String>,
@@ -148,7 +148,7 @@ impl Key {
148148
// name, shortening won't make it one. In particular, a prefix must end in a letters-only
149149
// TLD, but the appended hash adds a hyphen and probably digits, very likely being an
150150
// invalid result.
151-
let name = ensure_max_length(name, KEY_NAME_MAX_LEN, 8);
151+
let name = ensure_max_string_length(name, KEY_NAME_MAX_LEN, 8);
152152

153153
let key = match prefix {
154154
Some(prefix) => format!("{prefix}/{name}"),

crates/stackable-operator/src/utils/length_enforcement.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use sha2::{Digest, Sha256};
1111
/// # Panics
1212
///
1313
/// Panics if `max_length_bytes < 1 /* character */ + 1 /* dash */ + hash_length`.
14-
pub fn ensure_max_length(
14+
pub fn ensure_max_string_length(
1515
original: impl Into<String>,
1616
max_length_bytes: usize,
1717
hash_length: usize,
@@ -63,102 +63,108 @@ mod test {
6363
use super::*;
6464

6565
#[test]
66-
fn test_ensure_max_length() {
66+
fn ensure_max_string_length_ascii() {
6767
// empty resource name, no hash length
68-
assert_eq!(String::new(), ensure_max_length(String::new(), 2, 0));
68+
assert_eq!(String::new(), ensure_max_string_length(String::new(), 2, 0));
6969

7070
// resource_name.len() <= max_length
7171
assert_eq!(
7272
"abcdef".to_owned(),
73-
ensure_max_length("abcdef".to_owned(), 6, 4)
73+
ensure_max_string_length("abcdef".to_owned(), 6, 4)
7474
);
7575

7676
// hash_length == 0
7777
assert_eq!(
7878
"abcdef".to_owned(),
79-
ensure_max_length("abcdefg".to_owned(), 6, 0)
79+
ensure_max_string_length("abcdefg".to_owned(), 6, 0)
8080
);
8181

8282
// hash appended with dash
8383
assert_eq!(
8484
"a-7d1a".to_owned(),
85-
ensure_max_length("abcdefg".to_owned(), 6, 4)
85+
ensure_max_string_length("abcdefg".to_owned(), 6, 4)
8686
);
8787

8888
// hash appended without an extra dash
8989
assert_eq!(
9090
"ab-a1b1".to_owned(),
91-
ensure_max_length("ab-defgh".to_owned(), 7, 4)
91+
ensure_max_string_length("ab-defgh".to_owned(), 7, 4)
9292
);
9393

9494
// hash appended without an extra dash
9595
// In this case, the result is one character shorter than the maximum length.
9696
assert_eq!(
9797
"a-3951".to_owned(),
98-
ensure_max_length("a-cdefgh".to_owned(), 7, 4)
98+
ensure_max_string_length("a-cdefgh".to_owned(), 7, 4)
9999
);
100100

101101
// hash appended without an extra dash
102102
// The two dashes in the given resource name are intentionally kept.
103103
assert_eq!(
104104
"a--f7a0".to_owned(),
105-
ensure_max_length("a--defgh".to_owned(), 7, 4)
105+
ensure_max_string_length("a--defgh".to_owned(), 7, 4)
106106
);
107107

108108
// A hash_length longer than the produced hash string may not produce the desired result.
109109
// Just use sensible values!
110110
assert_eq!(
111111
"aaaaaaaaa-d476ce01c3787bcab054a2cf48d6af6dd303a0eb549e21a74125132f79d90c36".to_owned(),
112-
ensure_max_length("a".repeat(1011), 1010, 1000)
112+
ensure_max_string_length("a".repeat(1011), 1010, 1000)
113113
);
114114
}
115115

116116
/// The maximum length is measured in bytes, so multi-byte characters must not be split up by
117117
/// the truncation. This can make the result shorter than the maximum length.
118118
#[test]
119-
fn test_ensure_max_length_with_multi_byte_characters() {
119+
fn ensure_max_string_length_with_multi_byte_characters() {
120120
// The two byte characters fit exactly into the maximum length.
121-
assert_eq!("äöü".to_owned(), ensure_max_length("äöü".to_owned(), 6, 4));
121+
assert_eq!(
122+
"äöü".to_owned(),
123+
ensure_max_string_length("äöü".to_owned(), 6, 4)
124+
);
122125

123126
// Truncating after 5 bytes would split up the "ü", so it is dropped entirely.
124-
assert_eq!("äö".to_owned(), ensure_max_length("äöü".to_owned(), 5, 0));
127+
assert_eq!(
128+
"äö".to_owned(),
129+
ensure_max_string_length("äöü".to_owned(), 5, 0)
130+
);
125131

126132
// The 5 bytes reserved for the name only fit "äö", of which the "ö" is then replaced by
127133
// the dash, so the result is two bytes shorter than the maximum length.
128134
assert_eq!(
129135
"ä-e109".to_owned(),
130-
ensure_max_length("äöüäöü".to_owned(), 9, 4)
136+
ensure_max_string_length("äöüäöü".to_owned(), 9, 4)
131137
);
132138

133139
// hash appended with dash, three byte characters
134140
assert_eq!(
135141
"日-9efa".to_owned(),
136-
ensure_max_length("日本語日本語".to_owned(), 10, 4)
142+
ensure_max_string_length("日本語日本語".to_owned(), 10, 4)
137143
);
138144

139145
// hash appended with dash, four byte characters
140146
assert_eq!(
141147
"🚀-a13c".to_owned(),
142-
ensure_max_length("🚀🚀🚀🚀".to_owned(), 13, 4)
148+
ensure_max_string_length("🚀🚀🚀🚀".to_owned(), 13, 4)
143149
);
144150

145151
// The trailing dash of the truncated name is replaced by the dash which separates the
146152
// hash.
147153
assert_eq!(
148154
"aä-f726".to_owned(),
149-
ensure_max_length("aä-öüb".to_owned(), 8, 4)
155+
ensure_max_string_length("aä-öüb".to_owned(), 8, 4)
150156
);
151157

152158
// The truncated name is "aä-ö", so the "ö" is dropped and the existing dash is reused.
153159
assert_eq!(
154160
"aä-ae0c".to_owned(),
155-
ensure_max_length("aä-öüäöü".to_owned(), 10, 4)
161+
ensure_max_string_length("aä-öüäöü".to_owned(), 10, 4)
156162
);
157163

158164
// The truncation does not leave any character, so only the hash is returned.
159165
assert_eq!(
160166
"d24d".to_owned(),
161-
ensure_max_length("🚀🚀🚀".to_owned(), 6, 4)
167+
ensure_max_string_length("🚀🚀🚀".to_owned(), 6, 4)
162168
);
163169
}
164170
}

crates/stackable-operator/src/v2/role_group_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::types::{
66
},
77
operator::{ClusterName, RoleGroupName, RoleName},
88
};
9-
use crate::{attributed_string_type, utils::length_enforcement::ensure_max_length};
9+
use crate::{attributed_string_type, utils::length_enforcement::ensure_max_string_length};
1010

1111
attributed_string_type! {
1212
QualifiedRoleGroupName,
@@ -79,7 +79,7 @@ impl ResourceNames {
7979
);
8080
// `concatenated_name` contains only ASCII characters.
8181
assert!(concatenated_name.is_ascii());
82-
let sanitized_name = ensure_max_length(
82+
let sanitized_name = ensure_max_string_length(
8383
concatenated_name,
8484
QualifiedRoleGroupName::MAX_LENGTH,
8585
HASH_LENGTH,

0 commit comments

Comments
 (0)