3 pub enum PasswordValidation
{
8 pub fn validate_password(password
: &str) -> PasswordValidation
{
9 if password
.len() < consts
::MIN_PASSWORD_SIZE
{
10 PasswordValidation
::TooShort
12 PasswordValidation
::Ok
16 pub fn substitute(str: &str, pattern
: &str, replacements
: &[&str]) -> String
{
17 let mut result
= String
::with_capacity(
18 (str.len() + replacements
.iter().map(|s
| s
.len()).sum
::<usize>())
19 .saturating_sub(pattern
.len() * replacements
.len()),
23 for s
in str.split(pattern
) {
25 if i
< replacements
.len() {
26 result
.push_str(replacements
[i
]);
32 return str.to_string();
38 /// Example: substitute_with_names("{hello}, {world}!", &["{hello}", "{world"], &["Hello", "World"])
39 pub fn substitute_with_names(str: &str, names
: &[&str], replacements
: &[&str]) -> String
{
40 let mut result
= str.to_string();
41 for (i
, name
) in names
.iter().enumerate() {
42 if i
>= replacements
.len() {
45 result
= result
.replace(name
, replacements
[i
]);
55 fn test_substitute() {
56 assert_eq!(substitute("", "", &[]), "");
57 assert_eq!(substitute("", "", &[""]), "");
58 assert_eq!(substitute("", "{}", &["a"]), "");
59 assert_eq!(substitute("a", "{}", &["b"]), "a");
60 assert_eq!(substitute("a{}", "{}", &["b"]), "ab");
61 assert_eq!(substitute("{}c", "{}", &["b"]), "bc");
62 assert_eq!(substitute("a{}c", "{}", &["b"]), "abc");
63 assert_eq!(substitute("{}b{}", "{}", &["a", "c"]), "abc");
64 assert_eq!(substitute("{}{}{}", "{}", &["a", "bc", "def"]), "abcdef");
65 assert_eq!(substitute("{}{}{}", "{}", &["a"]), "a");
69 fn test_substitute_with_names() {
71 substitute_with_names("{hello}, {world}!", &["{hello}"], &["Hello", "World"]),
76 substitute_with_names("{hello}, {world}!", &[], &["Hello", "World"]),
81 substitute_with_names("{hello}, {world}!", &["{hello}", "{world}"], &["Hello"]),
86 substitute_with_names("{hello}, {world}!", &["{hello}", "{world}"], &[]),
91 substitute_with_names(
93 &["{hello}", "{world}"],