thot_local/project/resources/
script.rs

1use crate::common::scripts_file_of;
2use crate::result::Result;
3use crate::system::settings::user_settings::UserSettings;
4use cluFlock::FlockLock;
5use serde::{Deserialize, Serialize};
6use settings_manager::list_setting::ListSetting;
7use settings_manager::local_settings::{LocalSettings, LockSettingsFile};
8use settings_manager::result::{
9    Error as SettingsError, LocalSettingsError, Result as SettingsResult,
10};
11use settings_manager::settings::Settings;
12use settings_manager::system_settings::SystemSettings;
13use settings_manager::types::Priority as SettingsPriority;
14use std::fs::File;
15use std::path::{Path, PathBuf};
16use thot_core::project::Script;
17use thot_core::types::{ResourceId, ResourcePath};
18
19// **************
20// *** Script ***
21// **************
22
23pub struct LocalScript;
24
25impl LocalScript {
26    /// Creates a new [`Script`] with the `creator` field matching the current active creator.
27    pub fn new(path: ResourcePath) -> Result<Script> {
28        let settings = UserSettings::load()?;
29        let creator = settings.active_user;
30
31        let mut script = Script::new(path)?;
32        script.creator = creator;
33        Ok(script)
34    }
35}
36
37// ***************
38// *** Scripts ***
39// ***************
40
41/// Project scripts.
42#[derive(Serialize, Deserialize, Debug, Default)]
43pub struct Scripts {
44    #[serde(skip)]
45    _file_lock: Option<FlockLock<File>>,
46
47    #[serde(skip)]
48    _base_path: Option<PathBuf>,
49
50    pub scripts: Vec<Script>,
51}
52
53impl Scripts {
54    pub fn new() -> Self {
55        Scripts {
56            _file_lock: None,
57            _base_path: None,
58
59            scripts: Vec::new(),
60        }
61    }
62
63    /// Gets a [`Script`] by its [`ResourceId`] if it is registered,
64    /// otherwise returns `None`.
65    pub fn get(&self, rid: &ResourceId) -> Option<&Script> {
66        for script in &self.scripts {
67            if &script.rid == rid {
68                return Some(script);
69            }
70        }
71
72        None
73    }
74
75    /// Returns whether a script with the given id is registered.
76    pub fn contains(&self, rid: &ResourceId) -> bool {
77        self.get(rid).is_some()
78    }
79
80    /// Returns whether a script with the given path is registered.
81    pub fn contains_path(&self, path: &ResourcePath) -> bool {
82        self.by_path(path).is_some()
83    }
84
85    /// Gets a script by its path if it is registered.
86    pub fn by_path(&self, path: &ResourcePath) -> Option<&Script> {
87        for script in &self.scripts {
88            if &script.path == path {
89                return Some(&script);
90            }
91        }
92
93        None
94    }
95}
96
97impl Settings for Scripts {
98    fn store_lock(&mut self, lock: FlockLock<File>) {
99        self._file_lock = Some(lock);
100    }
101
102    fn controls_file(&self) -> bool {
103        self._file_lock.is_some()
104    }
105
106    fn priority(&self) -> SettingsPriority {
107        SettingsPriority::Project
108    }
109}
110
111impl LocalSettings for Scripts {
112    fn rel_path() -> SettingsResult<PathBuf> {
113        Ok(scripts_file_of(Path::new("")))
114    }
115
116    fn base_path(&self) -> SettingsResult<PathBuf> {
117        self._base_path
118            .clone()
119            .ok_or(SettingsError::LocalSettingsError(
120                LocalSettingsError::PathNotSet,
121            ))
122    }
123
124    fn set_base_path(&mut self, path: PathBuf) -> SettingsResult {
125        self._base_path = Some(path);
126        Ok(())
127    }
128}
129
130impl ListSetting for Scripts {
131    type Item = Script;
132
133    fn items(&mut self) -> &mut Vec<Script> {
134        &mut self.scripts
135    }
136}
137
138impl LockSettingsFile for Scripts {}
139
140#[cfg(test)]
141#[path = "./script_test.rs"]
142mod script_test;