upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/repos/init.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2023-05-21 11:19:43 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2023-05-21 11:19:43 +0000
commita579e90666d49a0f9353319525b1790ad7b26c4a (patch)
tree30f783750741eca1703e1edb3174d7e6cedbf0f8 /src/repos/init.rs
parente98276e79cea0c3e474ca0251c276c474c35ed70 (diff)
repos
Diffstat (limited to 'src/repos/init.rs')
-rw-r--r--src/repos/init.rs183
1 files changed, 183 insertions, 0 deletions
diff --git a/src/repos/init.rs b/src/repos/init.rs
new file mode 100644
index 0000000..3254d3e
--- /dev/null
+++ b/src/repos/init.rs
@@ -0,0 +1,183 @@
1
2use std::fmt::Debug;
3use nostr_sdk::{EventBuilder, Tag, Keys, Event};
4use serde::{Deserialize, Serialize};
5
6use crate::{kind::Kind, ngit_tag::{tag_repo, tag_relays, tag_hashtag, tag_group_with_relays, tag_into_event}};
7
8/// [`InitializeRepo`] error
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11 /// Error serializing or deserializing JSON data
12 #[error("json error: {0}")]
13 Json(#[from] serde_json::Error),
14}
15
16impl InitializeRepo {
17
18 pub fn initialize(&self,keys:&Keys) -> Event {
19 // let keys = Keys::generate();
20 EventBuilder::new(
21 nostr_sdk::Kind::Custom(
22 match self.root_repo {
23 None => u64::from(Kind::InitializeRepo),
24 _ => u64::from(Kind::InitializeBranch),
25 }
26 ),
27 self.as_json(),
28 &self.generate_tags(),
29 )
30 .to_unsigned_event(keys.public_key())
31 .sign(&keys)
32 .unwrap()
33 }
34
35 fn generate_tags(&self) -> Vec<Tag> {
36 let mut tags =
37 vec![
38 self.maintainers_group.as_ref()
39 .expect("there always to be a maintainers group when initialising")
40 .clone(),
41 tag_hashtag("ngit-event"),
42 tag_hashtag("ngit-format-0.0.1"),
43 ];
44 if !self.relays.is_empty() {
45 tags.push(
46 tag_relays(&self.relays)
47 );
48 }
49
50 match &self.root_repo {
51 None =>(),
52 Some(id) => {
53 tags.push(
54 tag_repo(id)
55 );
56 tags.push(
57 tag_into_event(
58 // its a bit silly / lazy reusing this function just to get the tags formatted with relays when it is not a group
59 tag_group_with_relays(id, &self.relays)
60 )
61 );
62
63 }
64 }
65 tags
66 }
67}
68
69/// InitializeRepo
70#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
71pub struct InitializeRepo {
72 /// Name
73 #[serde(skip_serializing_if = "Option::is_none")]
74 pub name: Option<String>,
75 /// Description
76 #[serde(skip_serializing_if = "Option::is_none")]
77 pub about: Option<String>,
78 /// Picture
79 #[serde(skip_serializing_if = "Option::is_none")]
80 pub picture: Option<String>,
81 /// relays
82 pub relays: Vec<String>,
83 /// Maintainers Group
84 #[serde(skip_serializing_if = "Option::is_none")]
85 pub maintainers_group: Option<Tag>,
86 /// Maintainers Group
87 #[serde(skip_serializing_if = "Option::is_none")]
88 pub root_repo: Option<String>,
89}
90
91impl Default for InitializeRepo {
92 fn default() -> Self {
93 Self::new()
94 }
95}
96
97impl InitializeRepo {
98 /// New empty [`InitializeRepo`]
99 pub fn new() -> Self {
100 Self {
101 name: None,
102 about: None,
103 picture: None,
104 relays: vec![],
105 maintainers_group: None,
106 root_repo:None,
107 }
108 }
109
110 /// Deserialize [`InitializeRepo`] from `JSON` string
111 pub fn from_json<S>(json: S) -> Result<Self, Error>
112 where
113 S: Into<String>,
114 {
115 Ok(serde_json::from_str(&json.into())?)
116 }
117
118 /// Serialize [`InitializeRepo`] to `JSON` string
119 pub fn as_json(&self) -> String {
120 serde_json::json!(self).to_string()
121 }
122
123 /// Set name
124 pub fn name<S>(self, name: S) -> Self
125 where
126 S: Into<String>,
127 {
128 Self {
129 name: Some(name.into()),
130 ..self
131 }
132 }
133
134 /// Set about
135 pub fn about<S>(self, about: S) -> Self
136 where
137 S: Into<String>,
138 {
139 Self {
140 about: Some(about.into()),
141 ..self
142 }
143 }
144
145 /// Set picture
146 pub fn picture<S>(self, picture: S) -> Self
147 where
148 S: Into<String>,
149 {
150 Self {
151 picture: Some(picture.into()),
152 ..self
153 }
154 }
155
156 /// Set relays
157 pub fn relays(mut self, relays: &Vec<String>) -> Self {
158 for m in relays {
159 self.relays.push(m.clone());
160 }
161 self
162 }
163
164 /// Set maintainers_group
165 pub fn maintainers_group(self, group_ref: Tag) -> Self {
166 Self {
167 maintainers_group: Some(group_ref),
168 ..self
169 }
170 }
171
172 /// Set root_repo
173 pub fn root_repo<S>(self, root_repo: S) -> Self
174 where
175 S: Into<String>,
176 {
177 Self {
178 root_repo: Some(root_repo.into()),
179 ..self
180 }
181 }
182
183}