Skip to main content

turtle_build/
context.rs

1use crate::infrastructure::{CommandRunner, Console, Database, FileSystem};
2use alloc::sync::Arc;
3use tokio::sync::Mutex;
4
5/// A context.
6pub struct Context {
7    command_runner: Box<dyn CommandRunner + Send + Sync>,
8    console: Arc<Mutex<dyn Console + Send + Sync>>,
9    database: Box<dyn Database + Send + Sync>,
10    file_system: Arc<dyn FileSystem + Send + Sync>,
11}
12
13impl Context {
14    /// Creates a context.
15    pub fn new(
16        command_runner: impl CommandRunner + Send + Sync + 'static,
17        console: Arc<Mutex<impl Console + Send + Sync + 'static>>,
18        database: impl Database + Send + Sync + 'static,
19        file_system: impl FileSystem + Send + Sync + 'static,
20    ) -> Self {
21        Self {
22            command_runner: Box::new(command_runner),
23            console,
24            file_system: Arc::new(file_system),
25            database: Box::new(database),
26        }
27    }
28
29    /// Returns a command runner.
30    pub fn command_runner(&self) -> &(dyn CommandRunner + Send + Sync) {
31        &*self.command_runner
32    }
33
34    /// Returns a console.
35    pub fn console(&self) -> &Mutex<dyn Console + Send + Sync> {
36        &self.console
37    }
38
39    /// Returns a database.
40    pub fn database(&self) -> &(dyn Database + Send + Sync) {
41        &*self.database
42    }
43
44    /// Returns a file system.
45    pub fn file_system(&self) -> &Arc<dyn FileSystem + Send + Sync> {
46        &self.file_system
47    }
48}