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
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub type JobId = u64;
pub type JobTyp = String;

pub type SettingsMap = HashMap<JobTyp, Option<serde_json::Value>>;

#[derive(Serialize, Deserialize, Debug, Clone, Default, JsonSchema)]
pub struct JobTypId {
    typ: String,
    id: JobId,
}

impl JobTypId {
    pub fn new(typ: String, id: JobId) -> Self {
        Self { typ, id }
    }

    pub fn typ(&self) -> &str {
        &self.typ
    }

    pub fn id(&self) -> JobId {
        self.id
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default, JsonSchema)]
pub struct JobsLog {
    #[serde(default, skip_serializing_if = "SettingsMap::is_empty")]
    settings: SettingsMap,
}

impl JobsLog {
    pub fn is_empty(self) -> bool {
        self.settings.is_empty()
    }

    pub fn setting(&self, typ: &str) -> Option<&serde_json::Value> {
        self.settings.get(typ).map(|x| x.as_ref()).flatten()
    }

    pub fn setting_mut(&mut self, typ: &str) -> Option<&mut serde_json::Value> {
        self.settings.get_mut(typ).map(|x| x.as_mut()).flatten()
    }

    pub fn settings(&self) -> &SettingsMap {
        &self.settings
    }

    pub fn settings_mut(&mut self) -> &mut SettingsMap {
        &mut self.settings
    }

    pub fn copy_settings(&mut self, settings: &SettingsMap) {
        for (key, value) in settings {
            self.settings.insert(key.to_string(), value.clone());
        }
    }
}