foundry_compilers/compilers/
restrictions.rs

1use std::{
2    fmt::Debug,
3    ops::{Deref, DerefMut},
4};
5
6use semver::VersionReq;
7
8/// Abstraction over set of restrictions for given [`crate::Compiler::Settings`].
9pub trait CompilerSettingsRestrictions: Copy + Debug + Sync + Send + Clone + Default {
10    /// Combines this restriction with another one. Returns `None` if restrictions are incompatible.
11    #[must_use]
12    fn merge(self, other: Self) -> Option<Self>;
13}
14
15/// Combines [CompilerSettingsRestrictions] with a restrictions on compiler versions for a given
16/// source file.
17#[derive(Debug, Clone, Default)]
18pub struct RestrictionsWithVersion<T> {
19    pub version: Option<VersionReq>,
20    pub restrictions: T,
21}
22
23impl<T: CompilerSettingsRestrictions> RestrictionsWithVersion<T> {
24    /// Tries to merge the given restrictions with the other [`RestrictionsWithVersion`]. Returns
25    /// `None` if restrictions are incompatible.
26    pub fn merge(mut self, other: Self) -> Option<Self> {
27        if let Some(version) = other.version {
28            if let Some(self_version) = self.version.as_mut() {
29                self_version.comparators.extend(version.comparators);
30            } else {
31                self.version = Some(version);
32            }
33        }
34        self.restrictions = self.restrictions.merge(other.restrictions)?;
35        Some(self)
36    }
37}
38
39impl<T> Deref for RestrictionsWithVersion<T> {
40    type Target = T;
41
42    fn deref(&self) -> &Self::Target {
43        &self.restrictions
44    }
45}
46
47impl<T> DerefMut for RestrictionsWithVersion<T> {
48    fn deref_mut(&mut self) -> &mut Self::Target {
49        &mut self.restrictions
50    }
51}