diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-11-01 00:00:00 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-11-01 00:00:00 +0000 |
| commit | c37ceaf1c3e4371f320798bf423719a9a6fe71fd (patch) | |
| tree | e202a3eccb05f7ca0734cb4130ce69c1076452f5 /src/sub_commands/repo_ref.rs | |
| parent | 0753e0bcdd3d606f8f0226a3980bcd817117abaa (diff) | |
refactor(claim) add RepoRef struct
enable wider usage of repoistory reference details
Diffstat (limited to 'src/sub_commands/repo_ref.rs')
| -rw-r--r-- | src/sub_commands/repo_ref.rs | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/sub_commands/repo_ref.rs b/src/sub_commands/repo_ref.rs new file mode 100644 index 0000000..82580df --- /dev/null +++ b/src/sub_commands/repo_ref.rs | |||
| @@ -0,0 +1,137 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use nostr::Tag; | ||
| 3 | |||
| 4 | #[derive(Default)] | ||
| 5 | pub struct RepoRef { | ||
| 6 | name: String, | ||
| 7 | description: String, | ||
| 8 | root_commit: String, | ||
| 9 | relays: Vec<String>, | ||
| 10 | // git_server: String, | ||
| 11 | // other maintainers | ||
| 12 | // code languages and hashtags | ||
| 13 | } | ||
| 14 | |||
| 15 | impl RepoRef { | ||
| 16 | pub fn set_name(&mut self, name: String) -> &mut Self { | ||
| 17 | self.name = name; | ||
| 18 | self | ||
| 19 | } | ||
| 20 | |||
| 21 | pub fn set_description(&mut self, description: String) -> &mut Self { | ||
| 22 | self.description = description; | ||
| 23 | self | ||
| 24 | } | ||
| 25 | |||
| 26 | pub fn set_root_commit(&mut self, root_commit: String) -> &mut Self { | ||
| 27 | self.root_commit = root_commit; | ||
| 28 | self | ||
| 29 | } | ||
| 30 | |||
| 31 | pub fn set_relays(&mut self, relays: Vec<String>) -> &mut Self { | ||
| 32 | self.relays = relays; | ||
| 33 | self | ||
| 34 | } | ||
| 35 | |||
| 36 | pub fn to_event(&self, keys: &nostr::Keys) -> Result<nostr::Event> { | ||
| 37 | nostr_sdk::EventBuilder::new( | ||
| 38 | nostr::event::Kind::Custom(30017), | ||
| 39 | "", | ||
| 40 | &[ | ||
| 41 | vec![ | ||
| 42 | Tag::Identifier(self.root_commit.to_string()), | ||
| 43 | Tag::Reference(format!("r-{}", self.root_commit)), | ||
| 44 | Tag::Name(self.name.clone()), | ||
| 45 | Tag::Description(self.description.clone()), | ||
| 46 | ], | ||
| 47 | self.relays.iter().map(|r| Tag::Relay(r.into())).collect(), | ||
| 48 | // git_servers | ||
| 49 | // other maintainers | ||
| 50 | // code languages and hashtags | ||
| 51 | ] | ||
| 52 | .concat(), | ||
| 53 | ) | ||
| 54 | .to_event(keys) | ||
| 55 | .context("failed to create repository reference event") | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | #[cfg(test)] | ||
| 60 | mod tests { | ||
| 61 | use test_utils::*; | ||
| 62 | |||
| 63 | use super::*; | ||
| 64 | |||
| 65 | mod to_event { | ||
| 66 | use super::*; | ||
| 67 | mod tags { | ||
| 68 | use super::*; | ||
| 69 | fn create() -> nostr::Event { | ||
| 70 | RepoRef::default() | ||
| 71 | .set_name("test name".to_string()) | ||
| 72 | .set_description("test description".to_string()) | ||
| 73 | .set_root_commit("23471389461".to_string()) | ||
| 74 | .set_relays(vec![ | ||
| 75 | "ws://relay1.io".to_string(), | ||
| 76 | "ws://relay2.io".to_string(), | ||
| 77 | ]) | ||
| 78 | .to_event(&TEST_KEY_1_KEYS) | ||
| 79 | .unwrap() | ||
| 80 | } | ||
| 81 | |||
| 82 | #[test] | ||
| 83 | fn name() { | ||
| 84 | assert!( | ||
| 85 | create() | ||
| 86 | .tags | ||
| 87 | .iter() | ||
| 88 | .any(|t| t.as_vec()[0].eq("name") && t.as_vec()[1].eq("test name")) | ||
| 89 | ) | ||
| 90 | } | ||
| 91 | #[test] | ||
| 92 | fn description() { | ||
| 93 | assert!(create().tags.iter().any( | ||
| 94 | |t| t.as_vec()[0].eq("description") && t.as_vec()[1].eq("test description") | ||
| 95 | )) | ||
| 96 | } | ||
| 97 | |||
| 98 | #[test] | ||
| 99 | fn root_commit_as_d_replaceable_event_identifier() { | ||
| 100 | assert!( | ||
| 101 | create() | ||
| 102 | .tags | ||
| 103 | .iter() | ||
| 104 | .any(|t| t.as_vec()[0].eq("d") && t.as_vec()[1].eq("23471389461")) | ||
| 105 | ) | ||
| 106 | } | ||
| 107 | |||
| 108 | #[test] | ||
| 109 | fn root_commit_as_reference() { | ||
| 110 | assert!( | ||
| 111 | create() | ||
| 112 | .tags | ||
| 113 | .iter() | ||
| 114 | .any(|t| t.as_vec()[0].eq("r") && t.as_vec()[1].eq("r-23471389461")) | ||
| 115 | ) | ||
| 116 | } | ||
| 117 | |||
| 118 | #[test] | ||
| 119 | fn relays() { | ||
| 120 | let event = create(); | ||
| 121 | let relay_tags = event | ||
| 122 | .tags | ||
| 123 | .iter() | ||
| 124 | .filter(|t| t.as_vec()[0].eq("relay")) | ||
| 125 | .collect::<Vec<&nostr::Tag>>(); | ||
| 126 | assert_eq!(relay_tags[0].as_vec().len(), 2); | ||
| 127 | assert_eq!(relay_tags[0].as_vec()[1], "ws://relay1.io"); | ||
| 128 | assert_eq!(relay_tags[1].as_vec()[1], "ws://relay2.io"); | ||
| 129 | } | ||
| 130 | |||
| 131 | #[test] | ||
| 132 | fn no_other_tags() { | ||
| 133 | assert_eq!(create().tags.len(), 6) | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||