Skip to main content

turtle_build/
context.rs

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