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
116
117
118
119
120
121
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use super::*;
use serde::{Deserialize, Serialize};
use std::rc::Rc;

/// Create Bridge object from its representation in Rust Object Notation
impl TryFrom<String> for Bridge {
    type Error = ron::error::SpannedError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        ron::from_str(value.as_str())
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Bridge {
    uuid: Uuid,
    forest_uuid: Uuid,
    path: ContainerPath,
    link: Vec<u8>,

    #[serde(skip, default = "use_default_database")]
    db: Rc<StoreDb>,
}

impl Bridge {
    pub fn new(forest_uuid: Uuid, path: ContainerPath, link: Vec<u8>, db: Rc<StoreDb>) -> Self {
        Bridge {
            uuid: Uuid::new_v4(),
            forest_uuid,
            path,
            link,
            db,
        }
    }
}

impl IBridge for Bridge {
    fn uuid(&self) -> Uuid {
        self.uuid
    }

    fn path(&self) -> ContainerPath {
        self.path.clone()
    }

    fn forest(&self) -> CatlibResult<crate::forest::Forest> {
        fetch_forest_by_uuid(self.db.clone(), self.forest_uuid)
    }

    fn link(&self) -> Vec<u8> {
        self.link.clone()
    }

    fn update(&mut self, link: Vec<u8>) -> CatlibResult<crate::bridge::Bridge> {
        self.link = link;
        self.save()?;
        Ok(self.clone())
    }
}

impl Model for Bridge {
    fn save(&mut self) -> CatlibResult<()> {
        save_model(
            self.db.clone(),
            format!("bridge-{}", self.uuid),
            ron::to_string(self).unwrap(),
        )
    }

    fn delete(&mut self) -> CatlibResult<()> {
        delete_model(self.db.clone(), format!("bridge-{}", self.uuid))
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use rstest::*;
    use uuid::Bytes;

    #[fixture]
    fn catlib() -> CatLib {
        db::init_catlib(rand::random::<Bytes>())
    }

    #[rstest]
    fn create_bridge(catlib: CatLib) {
        let forest = catlib
            .create_forest(Identity([1; 32]), Signers::new(), vec![])
            .unwrap();

        let mut bridge = forest
            .create_bridge("/other/forest".to_string(), vec![])
            .unwrap();

        forest.find_bridge("/other/forest".to_string()).unwrap();

        bridge.delete().unwrap();

        let bridge = forest.find_bridge("/other/forest".to_string());

        assert_eq!(bridge.err(), Some(CatlibError::NoRecordsFound));
    }
}