@@ -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}
0 commit comments