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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use self::file::{AllocationRequest, Allocations, LifeCycle, Notary};

pub mod allocation;
pub mod client;
pub mod datacap;
pub mod file;
pub mod lifecycle;
pub mod project;

impl file::ApplicationFile {
    pub async fn new(
        issue_number: String,
        multisig_address: String,
        version: u8,
        id: String,
        client: file::Client,
        project: file::Project,
        datacap: file::Datacap,
    ) -> Self {
        let allocation = Allocations::default();
        let lifecycle = LifeCycle::submitted(id.clone(), multisig_address.clone());
        Self {
            version,
            issue_number,
            id,
            client,
            project,
            datacap,
            lifecycle,
            allocation,
        }
    }

    pub fn reached_total_datacap(&self) -> Self {
        let new_life_cycle = self.lifecycle.clone().reached_total_datacap();
        Self {
            lifecycle: new_life_cycle,
            ..self.clone()
        }
    }

    pub fn move_back_to_governance_review(&self) -> Self {
        let new_life_cycle = self.lifecycle.clone().move_back_to_governance_review(); // move back to submitted state
        let allocation = Allocations::default(); // empty allocations
        Self {
            lifecycle: new_life_cycle,
            allocation,
            ..self.clone()
        }
    }

    pub fn complete_governance_review(&self, actor: String, request: AllocationRequest) -> Self {
        let new_life_cycle = self
            .lifecycle
            .clone()
            .finish_governance_review(actor, request.id.clone());
        let allocations = Allocations::init(request.clone());
        Self {
            lifecycle: new_life_cycle,
            allocation: allocations,
            ..self.clone()
        }
    }

    pub fn start_refill_request(&mut self, request: AllocationRequest) -> Self {
        let new_life_cycle = self
            .lifecycle
            .clone()
            .start_refill_request(request.id.clone());
        let allocations = self.allocation.clone().push(request.clone());
        Self {
            lifecycle: new_life_cycle,
            allocation: allocations,
            ..self.clone()
        }
    }

    pub fn add_signer_to_allocation(
        &self,
        signer: Notary,
        request_id: String,
        app_lifecycle: LifeCycle,
    ) -> Self {
        let new_allocation = self.allocation.clone().add_signer(request_id, signer);
        Self {
            allocation: new_allocation,
            lifecycle: app_lifecycle,
            ..self.clone()
        }
    }

    pub fn add_signer_to_allocation_and_complete(
        &self,
        signer: Notary,
        request_id: String,
        app_lifecycle: LifeCycle,
    ) -> Self {
        let new_allocation = self
            .allocation
            .clone()
            .add_signer_and_complete(request_id, signer);
        Self {
            allocation: new_allocation,
            lifecycle: app_lifecycle,
            ..self.clone()
        }
    }
}

impl std::str::FromStr for file::ApplicationFile {
    type Err = serde_json::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        serde_json::from_str(s)
    }
}