summaryrefslogtreecommitdiff
path: root/crates/vcs_data/src/data/local/config.rs
blob: 338d01baae8a67225f92011ce3a7921ef5ef3dee (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use cfg_file::ConfigFile;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

use crate::constants::CLIENT_FILE_WORKSPACE;
use crate::constants::PORT;
use crate::data::member::MemberId;
use crate::data::vault::config::VaultUuid;

#[derive(Serialize, Deserialize, ConfigFile)]
#[cfg_file(path = CLIENT_FILE_WORKSPACE)]
pub struct LocalConfig {
    /// The upstream address, representing the upstream address of the local workspace,
    /// to facilitate timely retrieval of new updates from the upstream source.
    upstream_addr: SocketAddr,

    /// The member ID used by the current local workspace.
    /// This ID will be used to verify access permissions when connecting to the upstream server.
    using_account: MemberId,

    /// Whether the local workspace is stained.
    ///
    /// If stained, it can only set an upstream server with the same identifier.
    ///
    /// If the value is None, it means not stained;
    /// otherwise, it contains the stain identifier (i.e., the upstream vault's unique ID)
    stained_uuid: Option<VaultUuid>,
}

impl Default for LocalConfig {
    fn default() -> Self {
        Self {
            upstream_addr: SocketAddr::V4(std::net::SocketAddrV4::new(
                std::net::Ipv4Addr::new(127, 0, 0, 1),
                PORT,
            )),
            using_account: "unknown".to_string(),
            stained_uuid: None,
        }
    }
}

impl LocalConfig {
    /// Set the vault address.
    pub fn set_vault_addr(&mut self, addr: SocketAddr) {
        self.upstream_addr = addr;
    }

    /// Get the vault address.
    pub fn vault_addr(&self) -> SocketAddr {
        self.upstream_addr
    }

    /// Set the currently used account
    pub fn set_current_account(&mut self, account: MemberId) {
        self.using_account = account;
    }

    /// Get the currently used account
    pub fn current_account(&self) -> MemberId {
        self.using_account.clone()
    }

    /// Check if the local workspace is stained.
    pub fn stained(&self) -> bool {
        self.stained_uuid.is_some()
    }

    /// Stain the local workspace with the given UUID.
    pub fn stain(&mut self, uuid: VaultUuid) {
        self.stained_uuid = Some(uuid);
    }

    /// Unstain the local workspace.
    pub fn unstain(&mut self) {
        self.stained_uuid = None;
    }
}