thot_local/system/collections/
scripts.rs

1//! Script collection for the system.
2use crate::system::common::config_dir_path;
3use cluFlock::FlockLock;
4use derivative::{self, Derivative};
5use serde::{Deserialize, Serialize};
6use settings_manager::list_setting::ListSetting;
7use settings_manager::result::Result as SettingsResult;
8use settings_manager::settings::Settings;
9use settings_manager::system_settings::{LockSettingsFile, SystemSettings};
10use settings_manager::types::Priority as SettingsPriority;
11use std::collections::HashMap;
12use std::default::Default;
13use std::fs::File;
14use std::path::PathBuf;
15use thot_core::system::Script;
16use thot_core::types::ResourceId;
17
18// ****************
19// *** Scripts ***
20// ****************
21
22#[derive(Serialize, Deserialize, Derivative, Default)]
23#[derivative(Debug)]
24pub struct Scripts {
25    #[serde(skip)]
26    _file_lock: Option<FlockLock<File>>,
27
28    pub scripts: Vec<Script>,
29}
30
31impl Settings for Scripts {
32    fn store_lock(&mut self, file_lock: FlockLock<File>) {
33        self._file_lock = Some(file_lock);
34    }
35
36    fn controls_file(&self) -> bool {
37        self._file_lock.is_some()
38    }
39
40    fn priority(&self) -> SettingsPriority {
41        SettingsPriority::User
42    }
43}
44
45impl SystemSettings for Scripts {
46    /// Returns the path to the system settings file.
47    fn path() -> SettingsResult<PathBuf> {
48        let settings_dir = config_dir_path()?;
49        Ok(settings_dir.join("scripts.json"))
50    }
51}
52
53impl ListSetting for Scripts {
54    type Item = Script;
55
56    fn items(&mut self) -> &mut Vec<Script> {
57        &mut self.scripts
58    }
59}
60
61impl LockSettingsFile for Scripts {}
62
63impl Into<HashMap<ResourceId, Script>> for Scripts {
64    fn into(self) -> HashMap<ResourceId, Script> {
65        self.scripts
66            .into_iter()
67            .map(|s| (s.rid.clone(), s))
68            .collect::<HashMap<ResourceId, Script>>()
69    }
70}
71
72#[cfg(test)]
73#[path = "./scripts_test.rs"]
74mod scripts_test;