aboutsummaryrefslogtreecommitdiff
path: root/src/util/serialization.rs
blob: a8b5438d65fe0eaf588888f0ab4aec3cc081dc80 (plain)
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
pub fn line_to_words(line: &str) -> Vec<String> {
    line.split('|')
        .map(|w| w.trim().to_string())
        .filter(|w| !w.is_empty())
        .collect()
}

pub fn words_to_line(words: &[String]) -> String {
    words.join(" | ")
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_line_to_words() {
        assert_eq!(line_to_words("a"), vec!("a"));
        assert_eq!(line_to_words("a | b | c"), vec!("a", "b", "c"));
    }

    #[test]
    fn test_words_to_line() {
        assert_eq!(words_to_line(&["a".to_string()]), "a");
        assert_eq!(
            words_to_line(&["a".to_string(), "b".to_string(), "c".to_string()]),
            "a | b | c"
        );
    }
}