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
#![forbid(unsafe_code)]
#![feature(non_ascii_idents)]
#![allow(unknown_lints, uncommon_codepoints)]
use anyhow::Result;
pub mod plugins;
pub mod util;
mod dedup;
mod ham;
#[derive(Clone)]
pub struct GunFunctions {
pub start: fn(&[&str]) -> Result<()>,
}
#[derive(Clone)]
pub struct GunOptions<'a> {
pub peers: &'a [&'a str],
pub radisk: bool,
pub local_storage: bool,
pub uuid: fn() -> String,
}
#[derive(Clone)]
pub struct GunBuilder<'a> {
pub functions: GunFunctions,
pub options: GunOptions<'a>,
}
impl<'a> GunBuilder<'a> {
pub fn new() -> Self {
Self {
functions: GunFunctions {
start: plugins::websockets::start,
},
options: GunOptions {
peers: &[],
radisk: true,
local_storage: true,
uuid: util::uuid,
},
}
}
pub fn new_with_options(options: GunOptions<'a>) -> Self {
Self {
functions: GunFunctions {
start: plugins::websockets::start,
},
options,
}
}
pub fn peers(&mut self, peers: &'a [&str]) -> Self {
self.options.peers = peers;
self.clone()
}
pub fn radisk(&mut self, radisk: bool) -> Self {
self.options.radisk = radisk;
self.clone()
}
pub fn local_storage(&mut self, local_storage: bool) -> Self {
self.options.local_storage = local_storage;
self.clone()
}
pub fn uuid(&mut self, uuid: fn() -> String) -> Self {
self.options.uuid = uuid;
self.clone()
}
pub fn build(&self) -> Gun {
let gun = Gun {
functions: self.functions.clone(),
options: self.options.clone(),
};
drop(self);
gun
}
}
pub struct Gun<'a> {
functions: GunFunctions,
options: GunOptions<'a>,
}
impl Gun<'_> {
pub fn start(&self) -> Result<()> {
(self.functions.start)(self.options.peers)
}
}