aboutsummaryrefslogtreecommitdiff
path: root/karlcommon/src/protocol.rs
blob: 69b9964abeb2d55375260f44190abbdf89786bf5 (plain)
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
use std::ops::Range;

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data", rename_all = "snake_case")]
pub enum ClientboundPacket {
    Handshake { version: String },
    Error(ProtoError),
    TaskList(Vec<Task>),
    InstanceList(Vec<Range<Option<i64>>>),
    InvalidateState,
    Sync,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", content = "details", rename_all = "snake_case")]
pub enum ProtoError {
    UnknownTask,
    FormatError(String),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data", rename_all = "snake_case")]
pub enum ServerboundPacket {
    Sync,
    Handshake {
        version: String,
    },
    ListTasks,
    ListInstances {
        task: u64,
        range: Range<Option<i64>>,
        limit: usize,
    },
    UpdateTask(Task),
    RemoveTask(u64),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Task {
    pub id: u64,
    pub name: String,
    pub description: Option<String>,
    pub tags: Vec<String>,
    pub schedule: Schedule,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "options", rename_all = "snake_case")]
pub enum Schedule {
    Never,
    Dynamic {
        priority: f64,
        scheduled: Option<Range<i64>>,
        duration: i64,
        condition: Condition, // duration, during which the task should be scheduled
    },
    Condition(Condition),
    Static(Range<i64>),
}

impl Schedule {
    pub fn is_dynamic(&self) -> bool {
        matches!(self, Self::Dynamic { .. })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Condition {
    Never,
    From(Box<Condition>),

    Or(Vec<Condition>),
    And(Vec<Condition>),
    Invert(Box<Condition>),

    Equal {
        prop: Property,
        value: i64,
        modulus: Option<i64>,
    },
    Range {
        prop: Property,
        min: i64,
        max: i64,
        modulus: Option<i64>,
    },
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Property {
    Year,
    Monthofyear,
    Weekofmonth,
    Dayofyear,
    Dayofmonth,
    Dayofweek,
    Hour,
    Minute,
    Second,
    Unix,
}