@@ -46,7 +46,7 @@ const UPPER: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
46
46
const DIGIT : & str = "0123456789" ;
47
47
48
48
define_validated ! {
49
- Nickname ( ArrayString <15 > casefolded) {
49
+ Nickname ( ArrayString <{ Nickname :: LENGTH } > casefolded) {
50
50
check_allowed_chars( value, & [ LOWER , UPPER , DIGIT , "-_\\ |[]{}^`" ] ) ?;
51
51
if let Some ( first) = value. chars( ) . next( ) {
52
52
if DIGIT . contains( first) || first == '-' {
@@ -58,11 +58,11 @@ define_validated! {
58
58
Ok ( ( ) )
59
59
}
60
60
61
- Username ( ArrayString <10 >) {
61
+ Username ( ArrayString <{ Username :: LENGTH } >) {
62
62
Ok ( ( ) )
63
63
}
64
64
65
- Hostname ( ArrayString <64 >) {
65
+ Hostname ( ArrayString <{ Hostname :: LENGTH } >) {
66
66
Ok ( ( ) )
67
67
}
68
68
@@ -112,6 +112,9 @@ define_validated! {
112
112
}
113
113
114
114
impl Nickname {
115
+ /// Maximum length, in bytes
116
+ pub const LENGTH : usize = 15 ;
117
+
115
118
/// Create a new Nickname, bypassing normal validation. This is only for internal use, and only when created
116
119
/// nicknames for collided users
117
120
pub ( crate ) fn new_for_collision (
@@ -122,17 +125,25 @@ impl Nickname {
122
125
}
123
126
124
127
impl Username {
128
+ /// Maximum length, in bytes
129
+ pub const LENGTH : usize = 10 ;
130
+
125
131
/// Coerce the provided value into a valid `Username`, by truncating to the
126
132
/// permitted length and removing any invalid characters.
127
133
pub fn new_coerce ( s : & str ) -> Self {
128
134
let mut s = s. to_string ( ) ;
129
135
s. retain ( |c| c != '[' ) ;
130
- s. truncate ( s. floor_char_boundary ( 10 ) ) ;
136
+ s. truncate ( s. floor_char_boundary ( Self :: LENGTH ) ) ;
131
137
// expect() is safe here; we've already truncated to the max length
132
138
Self ( ArrayString :: try_from ( s. as_str ( ) ) . expect ( "Failed to convert string" ) )
133
139
}
134
140
}
135
141
142
+ impl Hostname {
143
+ /// Maximum length, in bytes
144
+ pub const LENGTH : usize = 64 ;
145
+ }
146
+
136
147
impl ChannelKey {
137
148
pub fn new_coerce ( s : & str ) -> <Self as Validated >:: Result {
138
149
let mut s = s. to_string ( ) ;
0 commit comments