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
|
use std::error::Error;
use std::fmt::Display;
use crate::version::{Argument, CompatibilityRule, CompleteVersion, FeatureMatcher, Library, OSRestriction, RuleAction};
use super::SystemInfo;
#[derive(Debug)]
pub struct IncompatibleError {
what: &'static str,
reason: Option<String>
}
impl Display for IncompatibleError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(reason) = self.reason.as_ref() {
write!(f, "{} incompatible: {}", self.what, reason)
} else {
write!(f, "{} incompatible", self.what)
}
}
}
impl Error for IncompatibleError {}
mod seal {
pub trait CompatCheckInner {
const WHAT: &'static str;
fn get_rules(&self) -> Option<impl IntoIterator<Item = &super::CompatibilityRule>>;
fn get_incompatibility_reason(&self) -> Option<&str>;
}
}
pub trait CompatCheck: seal::CompatCheckInner {
fn rules_apply<'a>(&'a self, system: &SystemInfo, feature_matcher: impl FeatureMatcher<'a>) -> Result<(), IncompatibleError> {
let Some(rules) = self.get_rules() else { return Ok(()) };
let mut action = RuleAction::Disallow;
fn match_os(os: &OSRestriction, system: &SystemInfo) -> bool {
os.os.is_none_or(|o| system.is_our_os(o))
&& os.version.as_ref().is_none_or(|v| v.is_match(system.os_version.as_str()))
&& os.arch.as_ref().is_none_or(|a| a.is_match(system.arch.as_str()))
}
for rule in rules {
if rule.os.as_ref().is_none_or(|o| match_os(o, system))
&& rule.features_match(feature_matcher) {
action = rule.action;
}
}
if action == RuleAction::Disallow {
Err(IncompatibleError {
what: Self::WHAT,
reason: self.get_incompatibility_reason().map(|s| s.to_owned())
})
} else {
Ok(())
}
}
}
// trivial
impl seal::CompatCheckInner for CompatibilityRule {
const WHAT: &'static str = "rule";
fn get_rules(&self) -> Option<impl IntoIterator<Item = &CompatibilityRule>> {
Some(Some(self))
}
fn get_incompatibility_reason(&self) -> Option<&str> {
None
}
}
impl seal::CompatCheckInner for CompleteVersion {
const WHAT: &'static str = "version";
fn get_rules(&self) -> Option<impl IntoIterator<Item = &CompatibilityRule>> {
self.compatibility_rules.as_ref()
}
fn get_incompatibility_reason(&self) -> Option<&str> {
self.incompatibility_reason.as_ref().map(|s| s.as_str())
}
}
impl seal::CompatCheckInner for Library {
const WHAT: &'static str = "library";
fn get_rules(&self) -> Option<impl IntoIterator<Item = &CompatibilityRule>> {
self.rules.as_ref()
}
fn get_incompatibility_reason(&self) -> Option<&str> {
None
}
}
impl seal::CompatCheckInner for Argument {
const WHAT: &'static str = "argument";
fn get_rules(&self) -> Option<impl IntoIterator<Item = &CompatibilityRule>> {
self.rules.as_ref()
}
fn get_incompatibility_reason(&self) -> Option<&str> {
None
}
}
impl CompatCheck for CompatibilityRule {}
impl CompatCheck for CompleteVersion {}
impl CompatCheck for Library {}
impl CompatCheck for Argument {}
|