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
#[cfg(feature = "frontend")]
use crate::SharedFiles;
use buildsrs_database::AnyMetadata;
use buildsrs_storage::AnyStorage;

/// Backend state.
///
/// This struct contains all shared state that is needed to implement the backend service.
#[derive(Clone, Debug)]
pub struct Backend {
    database: AnyMetadata,
    storage: AnyStorage,
    #[cfg(feature = "frontend")]
    frontend: SharedFiles,
}

impl Backend {
    /// Create new backend state from a database connection and storage instance.
    pub fn new(database: AnyMetadata, storage: AnyStorage) -> Self {
        Backend {
            database,
            storage,
            #[cfg(feature = "frontend")]
            frontend: Default::default(),
        }
    }

    /// Replace frontend files
    #[cfg(feature = "frontend")]
    #[must_use]
    pub fn with_frontend(self, frontend: SharedFiles) -> Self {
        Self { frontend, ..self }
    }

    /// Frontend files
    #[cfg(feature = "frontend")]
    pub fn frontend(&self) -> &SharedFiles {
        &self.frontend
    }

    /// Return a reference to the database.
    pub fn database(&self) -> &AnyMetadata {
        &self.database
    }

    /// Return a reference to the storage.
    pub fn storage(&self) -> &AnyStorage {
        &self.storage
    }
}