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
use std::{collections::HashSet, path::PathBuf};

use anyhow::Result;
use regex::Regex;

use crate::validate::ValidateParserError;

pub fn validate_continuous_assets(paths: &[PathBuf]) -> Result<()> {
    // Checking the assets are a proper series starting at 0 and ending at n-1
    let num_re = Regex::new(r"^(\d+).json$").unwrap();
    let collection_re = Regex::new(r"^collection.json$").unwrap();
    let mut collection_found = false;

    let num_series = paths
        .iter()
        .filter_map(|path| {
            let name = path.file_name().unwrap().to_str().unwrap();
            if collection_re.is_match(name) {
                collection_found = true;
                return None;
            }
            num_re
                .captures(name)
                .map(|number| number[1].parse::<usize>().unwrap())
        })
        .collect::<Vec<usize>>();

    if collection_found && num_series.len() != paths.len() - 1 {
        return Err(ValidateParserError::UnexpectedFilesFound.into());
    }
    if !collection_found && num_series.len() != paths.len() {
        return Err(ValidateParserError::UnexpectedFilesFound.into());
    }

    if num_series.is_empty() {
        return Err(ValidateParserError::NoAssetsFound.into());
    }

    // Sum of series given we expect:
    // a_0 = 0 , a_n = num_series.size() - 1 , n = num_series.size() => n * (a_0 + a_n) / 2
    // https://en.wikipedia.org/wiki/Arithmetic_progression

    let target_sum = num_series.len() * (num_series.len() - 1) / 2;
    let mut sum: usize = 0;
    let mut redundant: HashSet<usize> = HashSet::new();
    for num in &num_series {
        if redundant.contains(num) {
            return Err(ValidateParserError::RedundantFile(*num).into());
        } else if num >= &num_series.len() {
            return Err(ValidateParserError::FileOutOfRange(*num).into());
        } else {
            redundant.insert(*num);
            sum += num;
        }
    }

    if sum != target_sum {
        return Err(ValidateParserError::NonContinuousSeries.into());
    }

    Ok(())
}

#[test]
fn test_validate_continuous_assets_success() {
    let paths = vec![
        PathBuf::from("assets/0.json"),
        PathBuf::from("assets/1.json"),
        PathBuf::from("assets/2.json"),
        PathBuf::from("assets/3.json"),
        PathBuf::from("assets/4.json"),
    ];
    assert!(validate_continuous_assets(&paths).is_ok());
}

#[test]
fn test_validate_continuous_assets_with_collection_success() {
    let paths = vec![
        PathBuf::from("assets/0.json"),
        PathBuf::from("assets/1.json"),
        PathBuf::from("assets/2.json"),
        PathBuf::from("assets/3.json"),
        PathBuf::from("assets/4.json"),
        PathBuf::from("assets/collection.json"),
    ];
    assert!(validate_continuous_assets(&paths).is_ok());
}

#[test]
fn test_validate_continuous_assets_fail_out_of_range() {
    let paths = vec![
        PathBuf::from("assets/0.json"),
        PathBuf::from("assets/1.json"),
        PathBuf::from("assets/2.json"),
        PathBuf::from("assets/9.json"),
        PathBuf::from("assets/collection.json"),
    ];
    let result = validate_continuous_assets(&paths);
    assert!(result.is_err());
    assert_eq!(
        result.unwrap_err().to_string(),
        "File 9.json is out of expected range"
    );
}

#[test]
fn test_validate_continuous_assets_fail_redundant_file() {
    let paths = vec![
        PathBuf::from("assets/0.json"),
        PathBuf::from("assets/1.json"),
        PathBuf::from("assets/2.json"),
        PathBuf::from("assets/2.json"),
        PathBuf::from("assets/collection.json"),
    ];
    let result = validate_continuous_assets(&paths);
    assert!(result.is_err());
    assert_eq!(result.unwrap_err().to_string(), "Redundant file 2.json");
}

#[test]
fn test_validate_continuous_assets_fail_bad_naming() {
    let paths = vec![
        PathBuf::from("assets/0.json"),
        PathBuf::from("assets/xyz1.json"),
        PathBuf::from("assets/-2.json"),
        PathBuf::from("assets/collection.json"),
    ];
    let result = validate_continuous_assets(&paths);
    assert!(result.is_err());
    assert_eq!(
        result.unwrap_err().to_string(),
        "Unexpected files found in assets directory"
    );
}

#[test]
fn test_validate_continuous_assets_fail_no_assets_found_with_collection() {
    let paths = vec![
        PathBuf::from("assets/hello_world.json"),
        PathBuf::from("assets/collection.json"),
    ];
    let result = validate_continuous_assets(&paths);
    assert!(result.is_err());
    assert_eq!(
        result.unwrap_err().to_string(),
        "Unexpected files found in assets directory"
    );
}

#[test]
fn test_validate_continuous_assets_fail_no_assets_found() {
    let paths = vec![PathBuf::from("assets/hello_world.json")];
    let result = validate_continuous_assets(&paths);
    assert!(result.is_err());
    assert_eq!(
        result.unwrap_err().to_string(),
        "Unexpected files found in assets directory"
    );
}