cobble_core/minecraft/models/
rule.rs

1use crate::utils::{Architecture, Platform};
2use serde::{Deserialize, Serialize};
3
4/// A rule that can enable/disable functionality on a specific platform/architecture.
5#[derive(Clone, Debug, Deserialize, Serialize)]
6pub struct Rule {
7    /// If the rule allows or disallows functionality.
8    pub action: RuleAction,
9    /// The OS configuration the rule applies on.
10    pub os: Option<RuleOs>,
11    /// Features that need to be enabled for this rule to apply.
12    pub features: Option<RuleFeatures>,
13}
14
15impl Rule {
16    /// Checks wether the rule allows functionality or not.
17    pub fn allows(&self) -> bool {
18        if let Some(os) = &self.os {
19            if let Some(platform) = &os.platform {
20                if platform != &Platform::current() {
21                    return !self.action.to_bool();
22                }
23            }
24        }
25
26        if let Some(features) = &self.features {
27            if features.is_demo_user.is_some() || features.has_custom_resolution.is_some() {
28                return false;
29            }
30        }
31
32        self.action.to_bool()
33    }
34}
35
36/// Action of a rule.
37#[derive(Clone, Debug, Deserialize, Serialize)]
38pub enum RuleAction {
39    /// Allow functionality
40    #[serde(alias = "allow")]
41    Allow,
42    /// Disallow functionality
43    #[serde(alias = "disallow")]
44    Disallow,
45}
46
47impl RuleAction {
48    /// Convert the action to a `bool`.
49    pub fn to_bool(&self) -> bool {
50        match self {
51            RuleAction::Allow => true,
52            RuleAction::Disallow => false,
53        }
54    }
55}
56
57/// OS configuration
58#[derive(Clone, Debug, Deserialize, Serialize)]
59pub struct RuleOs {
60    /// The platform.
61    #[serde(alias = "name")]
62    pub platform: Option<Platform>,
63    /// Version of the platform.
64    pub version: Option<String>,
65    /// Architecture of the platform.
66    pub arch: Option<Architecture>,
67}
68
69/// Special features.
70#[derive(Clone, Debug, Deserialize, Serialize)]
71pub struct RuleFeatures {
72    /// Demo User.
73    pub is_demo_user: Option<bool>,
74    /// Custom resolution feature.
75    pub has_custom_resolution: Option<bool>,
76}