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
use crate::traits::IsUnique;

/// Uniqueness of the array items validation.
///
/// See <https://json-schema.org/understanding-json-schema/reference/array.html#unique_items>
pub trait ValidateUniqueItems {
    fn validate_unique_items(&self) -> Result<(), crate::UniqueItemsErrorParams>;
}

impl<T> ValidateUniqueItems for Vec<T>
where
    T: std::cmp::Eq + std::hash::Hash + std::fmt::Debug,
{
    fn validate_unique_items(&self) -> Result<(), crate::UniqueItemsErrorParams> {
        if self.is_unique() {
            Ok(())
        } else {
            Err(crate::UniqueItemsErrorParams::new())
        }
    }
}

impl<T, const N: usize> ValidateUniqueItems for [T; N]
where
    T: std::cmp::Eq + std::hash::Hash + std::fmt::Debug,
{
    fn validate_unique_items(&self) -> Result<(), crate::UniqueItemsErrorParams> {
        if self.is_unique() {
            Ok(())
        } else {
            Err(crate::UniqueItemsErrorParams::new())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_array_unique_items_array_type_is_true() {
        assert!(ValidateUniqueItems::validate_unique_items(&[1, 2, 3, 4]).is_ok());
    }

    #[test]
    fn test_validate_array_unique_items_vec_type_is_true() {
        assert!(ValidateUniqueItems::validate_unique_items(&vec![1, 2, 3, 4]).is_ok());
    }

    #[test]
    fn test_validate_array_unique_items_is_false() {
        assert!(ValidateUniqueItems::validate_unique_items(&[1, 2, 3, 3]).is_err());
    }
}