Skip to main content

mx_core/
mnemonic.rs

1//! Mnemonic normalization utilities.
2
3/// Normalizes a mnemonic string by replacing commas or multiple whitespace with single spaces.
4///
5/// This handles various input formats:
6/// - Comma-separated words: "word1,word2,word3"
7/// - Whitespace-separated words: "word1  word2\tword3"
8/// - Mixed separators: "word1, word2  word3"
9///
10/// # Examples
11/// ```
12/// use mx_core::normalize_mnemonic;
13///
14/// let mnemonic = "abandon,abandon abandon  about";
15/// assert_eq!(normalize_mnemonic(mnemonic), "abandon abandon abandon about");
16/// ```
17pub fn normalize_mnemonic(input: &str) -> String {
18    input
19        .split(|c: char| c.is_whitespace() || c == ',')
20        .filter(|part| !part.is_empty())
21        .collect::<Vec<_>>()
22        .join(" ")
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    fn test_normalize_mnemonic_spaces() {
31        let input = "word1  word2   word3";
32        assert_eq!(normalize_mnemonic(input), "word1 word2 word3");
33    }
34
35    #[test]
36    fn test_normalize_mnemonic_commas() {
37        let input = "word1,word2,word3";
38        assert_eq!(normalize_mnemonic(input), "word1 word2 word3");
39    }
40
41    #[test]
42    fn test_normalize_mnemonic_mixed() {
43        let input = "word1, word2  word3\tword4";
44        assert_eq!(normalize_mnemonic(input), "word1 word2 word3 word4");
45    }
46
47    #[test]
48    fn test_normalize_mnemonic_already_normalized() {
49        let input = "word1 word2 word3";
50        assert_eq!(normalize_mnemonic(input), "word1 word2 word3");
51    }
52}