summaryrefslogtreecommitdiff
path: root/src/data.rs
blob: 697635258d6ce168f76479b865e532cfcd099583 (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
32
33
34
35
36
37
38
39
40
41
42
pub static MESSAGE: &'static str = "Hello";

#[derive(Debug)]
pub enum SimpleFsData {
    File(u64, String),
    Directory(u64, Vec<(String, SimpleFsData)>)
}

// pub static HELLO: &'static SimpleFsData =
//     SimpleFsData:File("hello.txt", "Hello World");

pub fn get_data1() -> SimpleFsData {
    SimpleFsData::Directory(
        1, vec!(("hello".to_string(),
                 SimpleFsData::File(2, "Hello, World!".to_string()))))
}

pub fn traverse_fs<F>(file: SimpleFsData, f: &mut F)
    where F: FnMut(& SimpleFsData) {
    f(&file);
    match file {
        SimpleFsData::Directory(_, children) => for (_, file) in children {
           traverse_fs(file, f);
        },
        _ => (),
    }
}

#[test]
fn traverse() {
    let mut result = String::new();
    {
        let mut f = |ref file: &SimpleFsData| {
            match *file {
                &SimpleFsData::File(_, ref s) => result += &s,
                _ => (),
            };
        };
        traverse_fs(get_data1(), &mut f);
    }
    assert_eq!("Hello, World!", result);
}