-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprofile_name.rs
More file actions
373 lines (322 loc) · 11.4 KB
/
Copy pathprofile_name.rs
File metadata and controls
373 lines (322 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! LXD profile name validation and management
//!
//! This module provides the `ProfileName` type which ensures LXD profile names
//! follow the strict naming requirements imposed by LXD for security and
//! compatibility reasons.
//!
//! ## Naming Requirements
//!
//! - Length: 1-63 characters
//! - Characters: ASCII letters, numbers, and dashes only
//! - Cannot start with digit or dash
//! - Cannot end with dash
//!
//! These restrictions ensure compatibility with DNS records, file systems,
//! security profiles, and host names across different environments.
use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use thiserror::Error;
/// Errors that can occur during profile name validation
#[derive(Debug, Error, PartialEq)]
pub enum ProfileNameError {
#[error("Profile name cannot be empty")]
Empty,
#[error("Profile name must be 63 characters or less, got {length} characters")]
TooLong { length: usize },
#[error("Profile name must not start with a digit or dash")]
InvalidFirstCharacter,
#[error("Profile name must not end with a dash")]
InvalidLastCharacter,
#[error("Profile name must contain only ASCII letters, numbers, and dashes")]
InvalidCharacters,
}
/// A validated LXD profile name following LXD naming requirements.
///
/// Valid profile names must fulfill the following requirements:
/// - The name must be between 1 and 63 characters long
/// - The name must contain only letters, numbers and dashes from the ASCII table
/// - The name must not start with a digit or a dash
/// - The name must not end with a dash
///
/// These requirements ensure that the profile name can be used in DNS records,
/// on the file system, in various security profiles and as the host name.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::ProfileName;
///
/// // Valid profile names - accepts both &str and String
/// let profile1 = ProfileName::new("test-profile")?;
/// let profile2 = ProfileName::new("web-server-profile".to_string())?;
/// let profile3 = ProfileName::new(format!("app-{}-profile", "prod"))?;
///
/// // Invalid profile names
/// assert!(ProfileName::new("").is_err());
/// assert!(ProfileName::new("test-").is_err());
/// assert!(ProfileName::new("-test").is_err());
/// assert!(ProfileName::new("1test").is_err());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String")]
pub struct ProfileName(String);
impl ProfileName {
/// Creates a new `ProfileName` from a string if it's valid.
///
/// # Arguments
///
/// * `name` - The profile name to validate (accepts `&str`, `String`, or anything that implements `Into<String>`)
///
/// # Returns
///
/// * `Ok(ProfileName)` - If the name is valid
/// * `Err(ProfileNameError)` - If the name violates LXD naming requirements
///
/// # Errors
///
/// This function will return an error if the name violates any LXD naming requirements:
/// * Empty name
/// * Name longer than 63 characters
/// * Name contains non-ASCII letters, numbers, or dashes
/// * Name starts with a digit or dash
/// * Name ends with a dash
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::ProfileName;
///
/// // Valid names - accepts both &str and String
/// let name1 = ProfileName::new("test-profile")?;
/// let name2 = ProfileName::new("web-01-profile".to_string())?;
/// let name3 = ProfileName::new(format!("app-{}-profile", "prod"))?;
///
/// // Invalid names
/// assert!(ProfileName::new("").is_err());
/// assert!(ProfileName::new("test-").is_err());
/// assert!(ProfileName::new("-test").is_err());
/// assert!(ProfileName::new("1test").is_err());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn new<S: Into<String>>(name: S) -> Result<Self, ProfileNameError> {
let name = name.into();
Self::validate(&name)?;
Ok(Self(name))
}
/// Returns the profile name as a string slice.
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
/// Validates a profile name according to LXD requirements.
///
/// # Arguments
///
/// * `name` - The name to validate
///
/// # Returns
///
/// * `Ok(())` - If the name is valid
/// * `Err(ProfileNameError)` - If the name violates any requirement
fn validate(name: &str) -> Result<(), ProfileNameError> {
// Check length: must be between 1 and 63 characters
if name.is_empty() {
return Err(ProfileNameError::Empty);
}
if name.len() > 63 {
return Err(ProfileNameError::TooLong { length: name.len() });
}
// Check characters: only ASCII letters, numbers, and dashes
if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
return Err(ProfileNameError::InvalidCharacters);
}
// Check first character: must not be a digit or dash
if let Some(first_char) = name.chars().next() {
if first_char.is_ascii_digit() || first_char == '-' {
return Err(ProfileNameError::InvalidFirstCharacter);
}
}
// Check last character: must not be a dash
if let Some(last_char) = name.chars().last() {
if last_char == '-' {
return Err(ProfileNameError::InvalidLastCharacter);
}
}
Ok(())
}
}
impl fmt::Display for ProfileName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for ProfileName {
type Err = ProfileNameError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}
impl TryFrom<String> for ProfileName {
type Error = ProfileNameError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl From<ProfileName> for String {
fn from(profile_name: ProfileName) -> Self {
profile_name.0
}
}
impl AsRef<str> for ProfileName {
fn as_ref(&self) -> &str {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_create_valid_profile_name() {
let profile = ProfileName::new("torrust-profile").unwrap();
assert_eq!(profile.as_str(), "torrust-profile");
}
#[test]
fn it_should_accept_string_input() {
let profile_str = "test-profile".to_string();
let profile = ProfileName::new(profile_str).unwrap();
assert_eq!(profile.as_str(), "test-profile");
}
#[test]
fn it_should_accept_formatted_string() {
let env = "production";
let profile = ProfileName::new(format!("torrust-profile-{env}")).unwrap();
assert_eq!(profile.as_str(), "torrust-profile-production");
}
#[test]
fn it_should_create_profile_name_with_numbers() {
let profile = ProfileName::new("profile123").unwrap();
assert_eq!(profile.as_str(), "profile123");
}
#[test]
fn it_should_create_profile_name_with_dashes() {
let profile = ProfileName::new("test-profile-name").unwrap();
assert_eq!(profile.as_str(), "test-profile-name");
}
#[test]
fn it_should_reject_empty_profile_name() {
let result = ProfileName::new("");
assert!(matches!(result, Err(ProfileNameError::Empty)));
}
#[test]
fn it_should_reject_profile_name_starting_with_digit() {
let result = ProfileName::new("1profile");
assert!(matches!(
result,
Err(ProfileNameError::InvalidFirstCharacter)
));
}
#[test]
fn it_should_reject_profile_name_starting_with_dash() {
let result = ProfileName::new("-profile");
assert!(matches!(
result,
Err(ProfileNameError::InvalidFirstCharacter)
));
}
#[test]
fn it_should_reject_profile_name_ending_with_dash() {
let result = ProfileName::new("profile-");
assert!(matches!(
result,
Err(ProfileNameError::InvalidLastCharacter)
));
}
#[test]
fn it_should_reject_profile_name_with_invalid_characters() {
let result = ProfileName::new("test@profile");
assert!(matches!(result, Err(ProfileNameError::InvalidCharacters)));
let result = ProfileName::new("test_profile");
assert!(matches!(result, Err(ProfileNameError::InvalidCharacters)));
let result = ProfileName::new("test profile");
assert!(matches!(result, Err(ProfileNameError::InvalidCharacters)));
}
#[test]
fn it_should_reject_profile_name_too_long() {
let long_name = "a".repeat(64);
let result = ProfileName::new(long_name);
assert!(matches!(
result,
Err(ProfileNameError::TooLong { length: 64 })
));
}
#[test]
fn it_should_accept_max_length_profile_name() {
let max_length_name = "a".repeat(63);
let result = ProfileName::new(max_length_name);
assert!(result.is_ok());
}
#[test]
fn it_should_implement_from_str() {
let profile: ProfileName = "test-profile".parse().unwrap();
assert_eq!(profile.as_str(), "test-profile");
let invalid: Result<ProfileName, _> = "".parse();
assert!(invalid.is_err());
}
#[test]
fn it_should_implement_try_from_string() {
let profile = ProfileName::try_from("test-profile".to_string()).unwrap();
assert_eq!(profile.as_str(), "test-profile");
let invalid = ProfileName::try_from(String::new());
assert!(invalid.is_err());
}
#[test]
fn it_should_convert_to_string() {
let profile = ProfileName::new("test-profile").unwrap();
let string: String = profile.into();
assert_eq!(string, "test-profile");
}
#[test]
fn it_should_implement_as_ref() {
let profile = ProfileName::new("test-profile").unwrap();
let s: &str = profile.as_ref();
assert_eq!(s, "test-profile");
}
#[test]
fn it_should_display_profile_name() {
let profile = ProfileName::new("test-profile").unwrap();
assert_eq!(format!("{profile}"), "test-profile");
}
#[test]
fn it_should_be_cloneable() {
let profile = ProfileName::new("test-profile").unwrap();
let cloned = profile.clone();
assert_eq!(profile, cloned);
}
#[test]
fn it_should_be_hashable() {
use std::collections::HashMap;
let profile = ProfileName::new("test-profile").unwrap();
let mut map = HashMap::new();
map.insert(profile, "value");
assert_eq!(map.len(), 1);
}
#[test]
fn it_should_serialize_and_deserialize() {
let profile = ProfileName::new("test-profile").unwrap();
let json = serde_json::to_string(&profile).unwrap();
assert_eq!(json, "\"test-profile\"");
let deserialized: ProfileName = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, profile);
}
#[test]
fn it_should_fail_deserialization_for_invalid_names() {
let invalid_json = "\"\""; // Empty string
let result: Result<ProfileName, _> = serde_json::from_str(invalid_json);
assert!(result.is_err());
let invalid_json = "\"-invalid\""; // Starts with dash
let result: Result<ProfileName, _> = serde_json::from_str(invalid_json);
assert!(result.is_err());
}
}