Skip to main content

yog_script_analyze/
versions.rs

1//! Game versions matched with what a source says of them: `targets mc …;`
2//! and `#if mc …` — the one way, compared as Yog compares versions.
3
4use std::cmp::Ordering::{Equal, Greater, Less};
5
6use crate::ast::{BinOp, VersionCond};
7
8/// Whether version `v` meets `cond`.
9#[must_use]
10pub fn takes(cond: &VersionCond, v: &str) -> bool {
11    match cond {
12        VersionCond::Range { from, to } => {
13            yog_home::version::compare(v, from) != Less
14                && yog_home::version::compare(v, to) != Greater
15        }
16        VersionCond::Compare { op, version } => {
17            let ord = yog_home::version::compare(v, version);
18            match op {
19                BinOp::GtEq => ord != Less,
20                BinOp::Gt => ord == Greater,
21                BinOp::LtEq => ord != Greater,
22                BinOp::Lt => ord == Less,
23                _ => ord == Equal,
24            }
25        }
26    }
27}
28
29/// The versions Yog runs on that `cond` takes, oldest first.
30#[must_use]
31pub fn targeted(cond: &VersionCond) -> Vec<&'static str> {
32    yog_ids::game_version::ALL_VALUES
33        .iter()
34        .copied()
35        .filter(|v| takes(cond, v))
36        .collect()
37}
38
39/// The version a source is read as when no version is asked for (an
40/// editor's check): the newest Yog runs on.
41#[must_use]
42pub fn newest() -> &'static str {
43    yog_ids::game_version::ALL_VALUES
44        .last()
45        .copied()
46        .unwrap_or_default()
47}