foundry_compilers/compilers/
restrictions.rs

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
use std::{
    fmt::Debug,
    ops::{Deref, DerefMut},
};

use semver::VersionReq;

/// Abstraction over set of restrictions for given [`crate::Compiler::Settings`].
pub trait CompilerSettingsRestrictions: Copy + Debug + Sync + Send + Clone + Default {
    /// Combines this restriction with another one. Returns `None` if restrictions are incompatible.
    #[must_use]
    fn merge(self, other: Self) -> Option<Self>;
}

/// Combines [CompilerSettingsRestrictions] with a restrictions on compiler versions for a given
/// source file.
#[derive(Debug, Clone, Default)]
pub struct RestrictionsWithVersion<T> {
    pub version: Option<VersionReq>,
    pub restrictions: T,
}

impl<T: CompilerSettingsRestrictions> RestrictionsWithVersion<T> {
    /// Tries to merge the given restrictions with the other [`RestrictionsWithVersion`]. Returns
    /// `None` if restrictions are incompatible.
    pub fn merge(mut self, other: Self) -> Option<Self> {
        if let Some(version) = other.version {
            if let Some(self_version) = self.version.as_mut() {
                self_version.comparators.extend(version.comparators);
            } else {
                self.version = Some(version);
            }
        }
        self.restrictions = self.restrictions.merge(other.restrictions)?;
        Some(self)
    }
}

impl<T> Deref for RestrictionsWithVersion<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.restrictions
    }
}

impl<T> DerefMut for RestrictionsWithVersion<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.restrictions
    }
}