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