aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / semver / src / eval.rs
| 1 | use crate::{Comparator, Op, Version, VersionReq}; |
| 2 | |
| 3 | pub(crate) fn matches_req(req: &VersionReq, ver: &Version) -> bool { |
| 4 | for cmp in &req.comparators { |
| 5 | if !matches_impl(cmp, ver) { |
| 6 | return false; |
| 7 | } |
| 8 | } |
| 9 | |
| 10 | if ver.pre.is_empty() { |
| 11 | return true; |
| 12 | } |
| 13 | |
| 14 | // If a version has a prerelease tag (for example, 1.2.3-alpha.3) then it |
| 15 | // will only be allowed to satisfy req if at least one comparator with the |
| 16 | // same major.minor.patch also has a prerelease tag. |
| 17 | for cmp in &req.comparators { |
| 18 | if pre_is_compatible(cmp, ver) { |
| 19 | return true; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | false |
| 24 | } |
| 25 | |
| 26 | pub(crate) fn matches_comparator(cmp: &Comparator, ver: &Version) -> bool { |
| 27 | matches_impl(cmp, ver) && (ver.pre.is_empty() || pre_is_compatible(cmp, ver)) |
| 28 | } |
| 29 | |
| 30 | fn matches_impl(cmp: &Comparator, ver: &Version) -> bool { |
| 31 | match cmp.op { |
| 32 | Op::Exact | Op::Wildcard => matches_exact(cmp, ver), |
| 33 | Op::Greater => matches_greater(cmp, ver), |
| 34 | Op::GreaterEq => matches_exact(cmp, ver) || matches_greater(cmp, ver), |
| 35 | Op::Less => matches_less(cmp, ver), |
| 36 | Op::LessEq => matches_exact(cmp, ver) || matches_less(cmp, ver), |
| 37 | Op::Tilde => matches_tilde(cmp, ver), |
| 38 | Op::Caret => matches_caret(cmp, ver), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn matches_exact(cmp: &Comparator, ver: &Version) -> bool { |
| 43 | if ver.major != cmp.major { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | if let Some(minor) = cmp.minor { |
| 48 | if ver.minor != minor { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if let Some(patch) = cmp.patch { |
| 54 | if ver.patch != patch { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | ver.pre == cmp.pre |
| 60 | } |
| 61 | |
| 62 | fn matches_greater(cmp: &Comparator, ver: &Version) -> bool { |
| 63 | if ver.major != cmp.major { |
| 64 | return ver.major > cmp.major; |
| 65 | } |
| 66 | |
| 67 | match cmp.minor { |
| 68 | None => return false, |
| 69 | Some(minor) => { |
| 70 | if ver.minor != minor { |
| 71 | return ver.minor > minor; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | match cmp.patch { |
| 77 | None => return false, |
| 78 | Some(patch) => { |
| 79 | if ver.patch != patch { |
| 80 | return ver.patch > patch; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ver.pre > cmp.pre |
| 86 | } |
| 87 | |
| 88 | fn matches_less(cmp: &Comparator, ver: &Version) -> bool { |
| 89 | if ver.major != cmp.major { |
| 90 | return ver.major < cmp.major; |
| 91 | } |
| 92 | |
| 93 | match cmp.minor { |
| 94 | None => return false, |
| 95 | Some(minor) => { |
| 96 | if ver.minor != minor { |
| 97 | return ver.minor < minor; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | match cmp.patch { |
| 103 | None => return false, |
| 104 | Some(patch) => { |
| 105 | if ver.patch != patch { |
| 106 | return ver.patch < patch; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | ver.pre < cmp.pre |
| 112 | } |
| 113 | |
| 114 | fn matches_tilde(cmp: &Comparator, ver: &Version) -> bool { |
| 115 | if ver.major != cmp.major { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | if let Some(minor) = cmp.minor { |
| 120 | if ver.minor != minor { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if let Some(patch) = cmp.patch { |
| 126 | if ver.patch != patch { |
| 127 | return ver.patch > patch; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | ver.pre >= cmp.pre |
| 132 | } |
| 133 | |
| 134 | fn matches_caret(cmp: &Comparator, ver: &Version) -> bool { |
| 135 | if ver.major != cmp.major { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | let Some(minor) = cmp.minor else { |
| 140 | return true; |
| 141 | }; |
| 142 | |
| 143 | let Some(patch) = cmp.patch else { |
| 144 | if cmp.major > 0 { |
| 145 | return ver.minor >= minor; |
| 146 | } else { |
| 147 | return ver.minor == minor; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | if cmp.major > 0 { |
| 152 | if ver.minor != minor { |
| 153 | return ver.minor > minor; |
| 154 | } else if ver.patch != patch { |
| 155 | return ver.patch > patch; |
| 156 | } |
| 157 | } else if minor > 0 { |
| 158 | if ver.minor != minor { |
| 159 | return false; |
| 160 | } else if ver.patch != patch { |
| 161 | return ver.patch > patch; |
| 162 | } |
| 163 | } else if ver.minor != minor || ver.patch != patch { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | ver.pre >= cmp.pre |
| 168 | } |
| 169 | |
| 170 | fn pre_is_compatible(cmp: &Comparator, ver: &Version) -> bool { |
| 171 | cmp.major == ver.major |
| 172 | && cmp.minor == Some(ver.minor) |
| 173 | && cmp.patch == Some(ver.patch) |
| 174 | && !cmp.pre.is_empty() |
| 175 | } |