foundry_compilers/compilers/
restrictions.rs
1use std::{
2 fmt::Debug,
3 ops::{Deref, DerefMut},
4};
5
6use semver::VersionReq;
7
8pub trait CompilerSettingsRestrictions: Copy + Debug + Sync + Send + Clone + Default {
10 #[must_use]
12 fn merge(self, other: Self) -> Option<Self>;
13}
14
15#[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 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}