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
//  LOCATIONS.rs
//    by Lut99
//
//  Created:
//    26 Aug 2022, 15:44:19
//  Last edited:
//    14 Nov 2022, 10:00:31
//  Auto updated?
//    Yes
//
//  Description:
//!   Defines helpful enums and functions for location analysis.
//

use std::collections::HashSet;
use std::mem;


/***** LIBRARY *****/
/// Defines a special type that presents a location.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Location(pub String);

impl From<String> for Location {
    #[inline]
    fn from(value: String) -> Self { Self(value) }
}

impl From<&String> for Location {
    #[inline]
    fn from(value: &String) -> Self { Self(value.clone()) }
}

impl From<&str> for Location {
    #[inline]
    fn from(value: &str) -> Self { Self(value.into()) }
}

impl From<Location> for String {
    #[inline]
    fn from(value: Location) -> Self { value.0 }
}

impl From<&Location> for String {
    #[inline]
    fn from(value: &Location) -> Self { value.0.clone() }
}



/// Defines an enum that says something about the range of the location.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AllowedLocations {
    /// Everything is allowed (policy-wise).
    All,
    /// There is a restricted set of locations.
    Exclusive(HashSet<Location>),
}

impl AllowedLocations {
    /// Computes the intersection between this AllowedLocations and the given one.
    ///
    /// In other words, after running, this AllowedLocation contains only locations allowed by both.
    ///
    /// # Arguments
    /// - `other`: The other AllowedLocations to take the intersection with.
    #[inline]
    pub fn intersection(&mut self, other: &mut AllowedLocations) {
        use AllowedLocations::*;
        match self {
            All => {
                mem::swap(self, other);
            },
            Exclusive(self_locs) => {
                match other {
                    All => {},
                    Exclusive(other_locs) => {
                        // Take the self_locs
                        let old_locs: HashSet<Location> = mem::take(self_locs);

                        // Add those only present in both
                        let mut res_locs: HashSet<Location> = HashSet::with_capacity(old_locs.len());
                        for l in old_locs {
                            if other_locs.contains(&l) {
                                res_locs.insert(l);
                            }
                        }
                        res_locs.shrink_to_fit();

                        // Set 'em
                        *self_locs = res_locs;
                    },
                }
            },
        }
    }

    /// Returns whether all locations are allowed right now.
    #[inline]
    pub fn is_all(&self) -> bool { matches!(self, AllowedLocations::All) }

    /// Returns whether only specific locations are allowed right now.
    #[inline]
    pub fn is_exclusive(&self) -> bool { matches!(self, AllowedLocations::Exclusive(_)) }

    /// Returns whether _no_ location is still allowed right now.
    #[inline]
    pub fn is_empty(&self) -> bool {
        match self {
            AllowedLocations::All => false,
            AllowedLocations::Exclusive(locs) => locs.is_empty(),
        }
    }
}

impl AsRef<AllowedLocations> for AllowedLocations {
    #[inline]
    fn as_ref(&self) -> &AllowedLocations { self }
}

impl From<Location> for AllowedLocations {
    #[inline]
    fn from(value: Location) -> Self { Self::Exclusive(HashSet::from([value])) }
}