upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/lib/git
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/git')
-rw-r--r--src/lib/git/nostr_url.rs72
1 files changed, 34 insertions, 38 deletions
diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs
index a501765..c26bb2e 100644
--- a/src/lib/git/nostr_url.rs
+++ b/src/lib/git/nostr_url.rs
@@ -1,5 +1,5 @@
1use core::fmt; 1use core::fmt;
2use std::{collections::HashSet, str::FromStr}; 2use std::str::FromStr;
3 3
4use anyhow::{anyhow, bail, Context, Error, Result}; 4use anyhow::{anyhow, bail, Context, Error, Result};
5use nostr::nips::nip01::Coordinate; 5use nostr::nips::nip01::Coordinate;
@@ -56,7 +56,7 @@ impl FromStr for ServerProtocol {
56#[derive(Debug, PartialEq)] 56#[derive(Debug, PartialEq)]
57pub struct NostrUrlDecoded { 57pub struct NostrUrlDecoded {
58 pub original_string: String, 58 pub original_string: String,
59 pub coordinates: HashSet<Coordinate>, 59 pub coordinate: Coordinate,
60 pub protocol: Option<ServerProtocol>, 60 pub protocol: Option<ServerProtocol>,
61 pub user: Option<String>, 61 pub user: Option<String>,
62} 62}
@@ -70,9 +70,8 @@ impl fmt::Display for NostrUrlDecoded {
70 if let Some(protocol) = &self.protocol { 70 if let Some(protocol) = &self.protocol {
71 write!(f, "{}/", protocol)?; 71 write!(f, "{}/", protocol)?;
72 } 72 }
73 let c = self.coordinates.iter().next().unwrap(); 73 write!(f, "{}/", self.coordinate.public_key.to_bech32().unwrap())?;
74 write!(f, "{}/", c.public_key.to_bech32().unwrap())?; 74 if let Some(relay) = self.coordinate.relays.first() {
75 if let Some(relay) = c.relays.first() {
76 write!( 75 write!(
77 f, 76 f,
78 "{}/", 77 "{}/",
@@ -84,7 +83,7 @@ impl fmt::Display for NostrUrlDecoded {
84 ) 83 )
85 )?; 84 )?;
86 } 85 }
87 write!(f, "{}", c.identifier) 86 write!(f, "{}", self.coordinate.identifier)
88 } 87 }
89} 88}
90 89
@@ -94,7 +93,6 @@ impl std::str::FromStr for NostrUrlDecoded {
94 type Err = anyhow::Error; 93 type Err = anyhow::Error;
95 94
96 fn from_str(url: &str) -> Result<Self> { 95 fn from_str(url: &str) -> Result<Self> {
97 let mut coordinates = HashSet::new();
98 let mut protocol = None; 96 let mut protocol = None;
99 let mut user = None; 97 let mut user = None;
100 let mut relays = vec![]; 98 let mut relays = vec![];
@@ -157,9 +155,9 @@ impl std::str::FromStr for NostrUrlDecoded {
157 // extract naddr npub/<optional-relays>/identifer 155 // extract naddr npub/<optional-relays>/identifer
158 let part = parts.first().context(INCORRECT_NOSTR_URL_FORMAT_ERROR)?; 156 let part = parts.first().context(INCORRECT_NOSTR_URL_FORMAT_ERROR)?;
159 // naddr used 157 // naddr used
160 if let Ok(coordinate) = Coordinate::parse(part) { 158 let coordinate = if let Ok(coordinate) = Coordinate::parse(part) {
161 if coordinate.kind.eq(&nostr_sdk::Kind::GitRepoAnnouncement) { 159 if coordinate.kind.eq(&nostr_sdk::Kind::GitRepoAnnouncement) {
162 coordinates.insert(coordinate); 160 coordinate
163 } else { 161 } else {
164 bail!("naddr doesnt point to a git repository announcement"); 162 bail!("naddr doesnt point to a git repository announcement");
165 } 163 }
@@ -181,19 +179,19 @@ impl std::str::FromStr for NostrUrlDecoded {
181 RelayUrl::parse(&decoded).context("could not parse relays in nostr git url")?; 179 RelayUrl::parse(&decoded).context("could not parse relays in nostr git url")?;
182 relays.push(url); 180 relays.push(url);
183 } 181 }
184 coordinates.insert(Coordinate { 182 Coordinate {
185 identifier, 183 identifier,
186 public_key, 184 public_key,
187 kind: nostr_sdk::Kind::GitRepoAnnouncement, 185 kind: nostr_sdk::Kind::GitRepoAnnouncement,
188 relays, 186 relays,
189 }); 187 }
190 } else { 188 } else {
191 bail!(INCORRECT_NOSTR_URL_FORMAT_ERROR); 189 bail!(INCORRECT_NOSTR_URL_FORMAT_ERROR);
192 } 190 };
193 191
194 Ok(Self { 192 Ok(Self {
195 original_string: url.to_string(), 193 original_string: url.to_string(),
196 coordinates, 194 coordinate,
197 protocol, 195 protocol,
198 user, 196 user,
199 }) 197 })
@@ -865,8 +863,6 @@ mod tests {
865 } 863 }
866 } 864 }
867 mod nostr_git_url_format { 865 mod nostr_git_url_format {
868 use std::collections::HashSet;
869
870 use nostr::nips::nip01::Coordinate; 866 use nostr::nips::nip01::Coordinate;
871 use nostr_sdk::PublicKey; 867 use nostr_sdk::PublicKey;
872 868
@@ -880,7 +876,7 @@ mod tests {
880 "{}", 876 "{}",
881 NostrUrlDecoded { 877 NostrUrlDecoded {
882 original_string: String::new(), 878 original_string: String::new(),
883 coordinates: HashSet::from_iter(vec![Coordinate { 879 coordinate: Coordinate {
884 identifier: "ngit".to_string(), 880 identifier: "ngit".to_string(),
885 public_key: PublicKey::parse( 881 public_key: PublicKey::parse(
886 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", 882 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
@@ -888,7 +884,7 @@ mod tests {
888 .unwrap(), 884 .unwrap(),
889 kind: nostr_sdk::Kind::GitRepoAnnouncement, 885 kind: nostr_sdk::Kind::GitRepoAnnouncement,
890 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], 886 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()],
891 }]), 887 },
892 protocol: None, 888 protocol: None,
893 user: None, 889 user: None,
894 } 890 }
@@ -905,7 +901,7 @@ mod tests {
905 "{}", 901 "{}",
906 NostrUrlDecoded { 902 NostrUrlDecoded {
907 original_string: String::new(), 903 original_string: String::new(),
908 coordinates: HashSet::from_iter(vec![Coordinate { 904 coordinate: Coordinate {
909 identifier: "ngit".to_string(), 905 identifier: "ngit".to_string(),
910 public_key: PublicKey::parse( 906 public_key: PublicKey::parse(
911 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", 907 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
@@ -913,7 +909,7 @@ mod tests {
913 .unwrap(), 909 .unwrap(),
914 kind: nostr_sdk::Kind::GitRepoAnnouncement, 910 kind: nostr_sdk::Kind::GitRepoAnnouncement,
915 relays: vec![], 911 relays: vec![],
916 }]), 912 },
917 protocol: None, 913 protocol: None,
918 user: None, 914 user: None,
919 } 915 }
@@ -930,7 +926,7 @@ mod tests {
930 "{}", 926 "{}",
931 NostrUrlDecoded { 927 NostrUrlDecoded {
932 original_string: String::new(), 928 original_string: String::new(),
933 coordinates: HashSet::from_iter(vec![Coordinate { 929 coordinate: Coordinate {
934 identifier: "ngit".to_string(), 930 identifier: "ngit".to_string(),
935 public_key: PublicKey::parse( 931 public_key: PublicKey::parse(
936 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", 932 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
@@ -938,7 +934,7 @@ mod tests {
938 .unwrap(), 934 .unwrap(),
939 kind: nostr_sdk::Kind::GitRepoAnnouncement, 935 kind: nostr_sdk::Kind::GitRepoAnnouncement,
940 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], 936 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()],
941 }]), 937 },
942 protocol: Some(ServerProtocol::Ssh), 938 protocol: Some(ServerProtocol::Ssh),
943 user: None, 939 user: None,
944 } 940 }
@@ -955,7 +951,7 @@ mod tests {
955 "{}", 951 "{}",
956 NostrUrlDecoded { 952 NostrUrlDecoded {
957 original_string: String::new(), 953 original_string: String::new(),
958 coordinates: HashSet::from_iter(vec![Coordinate { 954 coordinate: Coordinate {
959 identifier: "ngit".to_string(), 955 identifier: "ngit".to_string(),
960 public_key: PublicKey::parse( 956 public_key: PublicKey::parse(
961 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", 957 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
@@ -963,7 +959,7 @@ mod tests {
963 .unwrap(), 959 .unwrap(),
964 kind: nostr_sdk::Kind::GitRepoAnnouncement, 960 kind: nostr_sdk::Kind::GitRepoAnnouncement,
965 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], 961 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()],
966 }]), 962 },
967 protocol: Some(ServerProtocol::Ssh), 963 protocol: Some(ServerProtocol::Ssh),
968 user: Some("bla".to_string()), 964 user: Some("bla".to_string()),
969 } 965 }
@@ -1002,7 +998,7 @@ mod tests {
1002 NostrUrlDecoded::from_str(&url)?, 998 NostrUrlDecoded::from_str(&url)?,
1003 NostrUrlDecoded { 999 NostrUrlDecoded {
1004 original_string: url.clone(), 1000 original_string: url.clone(),
1005 coordinates: HashSet::from([Coordinate { 1001 coordinate: Coordinate {
1006 identifier: "ngit".to_string(), 1002 identifier: "ngit".to_string(),
1007 public_key: PublicKey::parse( 1003 public_key: PublicKey::parse(
1008 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", 1004 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
@@ -1011,7 +1007,7 @@ mod tests {
1011 kind: nostr_sdk::Kind::GitRepoAnnouncement, 1007 kind: nostr_sdk::Kind::GitRepoAnnouncement,
1012 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], /* wont add the 1008 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], /* wont add the
1013 * slash */ 1009 * slash */
1014 }]), 1010 },
1015 protocol: None, 1011 protocol: None,
1016 user: None, 1012 user: None,
1017 }, 1013 },
@@ -1031,7 +1027,7 @@ mod tests {
1031 NostrUrlDecoded::from_str(&url)?, 1027 NostrUrlDecoded::from_str(&url)?,
1032 NostrUrlDecoded { 1028 NostrUrlDecoded {
1033 original_string: url.clone(), 1029 original_string: url.clone(),
1034 coordinates: HashSet::from([get_model_coordinate(false)]), 1030 coordinate: get_model_coordinate(false),
1035 protocol: None, 1031 protocol: None,
1036 user: None, 1032 user: None,
1037 }, 1033 },
@@ -1049,7 +1045,7 @@ mod tests {
1049 NostrUrlDecoded::from_str(&url)?, 1045 NostrUrlDecoded::from_str(&url)?,
1050 NostrUrlDecoded { 1046 NostrUrlDecoded {
1051 original_string: url.clone(), 1047 original_string: url.clone(),
1052 coordinates: HashSet::from([get_model_coordinate(true)]), 1048 coordinate: get_model_coordinate(true),
1053 protocol: None, 1049 protocol: None,
1054 user: None, 1050 user: None,
1055 }, 1051 },
@@ -1067,7 +1063,7 @@ mod tests {
1067 NostrUrlDecoded::from_str(&url)?, 1063 NostrUrlDecoded::from_str(&url)?,
1068 NostrUrlDecoded { 1064 NostrUrlDecoded {
1069 original_string: url.clone(), 1065 original_string: url.clone(),
1070 coordinates: HashSet::from([get_model_coordinate(true)]), 1066 coordinate: get_model_coordinate(true),
1071 protocol: None, 1067 protocol: None,
1072 user: None, 1068 user: None,
1073 }, 1069 },
@@ -1086,7 +1082,7 @@ mod tests {
1086 NostrUrlDecoded::from_str(&url)?, 1082 NostrUrlDecoded::from_str(&url)?,
1087 NostrUrlDecoded { 1083 NostrUrlDecoded {
1088 original_string: url.clone(), 1084 original_string: url.clone(),
1089 coordinates: HashSet::from([Coordinate { 1085 coordinate: Coordinate {
1090 identifier: "ngit".to_string(), 1086 identifier: "ngit".to_string(),
1091 public_key: PublicKey::parse( 1087 public_key: PublicKey::parse(
1092 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", 1088 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
@@ -1097,7 +1093,7 @@ mod tests {
1097 RelayUrl::parse("wss://nos.lol/").unwrap(), 1093 RelayUrl::parse("wss://nos.lol/").unwrap(),
1098 RelayUrl::parse("wss://relay.damus.io/").unwrap(), 1094 RelayUrl::parse("wss://relay.damus.io/").unwrap(),
1099 ], 1095 ],
1100 }]), 1096 },
1101 protocol: None, 1097 protocol: None,
1102 user: None, 1098 user: None,
1103 }, 1099 },
@@ -1112,7 +1108,7 @@ mod tests {
1112 NostrUrlDecoded::from_str(&url)?, 1108 NostrUrlDecoded::from_str(&url)?,
1113 NostrUrlDecoded { 1109 NostrUrlDecoded {
1114 original_string: url.clone(), 1110 original_string: url.clone(),
1115 coordinates: HashSet::from([get_model_coordinate(false)]), 1111 coordinate: get_model_coordinate(false),
1116 protocol: Some(ServerProtocol::Ssh), 1112 protocol: Some(ServerProtocol::Ssh),
1117 user: None, 1113 user: None,
1118 }, 1114 },
@@ -1127,7 +1123,7 @@ mod tests {
1127 NostrUrlDecoded::from_str(&url)?, 1123 NostrUrlDecoded::from_str(&url)?,
1128 NostrUrlDecoded { 1124 NostrUrlDecoded {
1129 original_string: url.clone(), 1125 original_string: url.clone(),
1130 coordinates: HashSet::from([get_model_coordinate(false)]), 1126 coordinate: get_model_coordinate(false),
1131 protocol: Some(ServerProtocol::Ssh), 1127 protocol: Some(ServerProtocol::Ssh),
1132 user: Some("fred".to_string()), 1128 user: Some("fred".to_string()),
1133 }, 1129 },
@@ -1146,7 +1142,7 @@ mod tests {
1146 NostrUrlDecoded::from_str(&url)?, 1142 NostrUrlDecoded::from_str(&url)?,
1147 NostrUrlDecoded { 1143 NostrUrlDecoded {
1148 original_string: url.clone(), 1144 original_string: url.clone(),
1149 coordinates: HashSet::from([get_model_coordinate(true)]), 1145 coordinate: get_model_coordinate(true),
1150 protocol: None, 1146 protocol: None,
1151 user: None, 1147 user: None,
1152 }, 1148 },
@@ -1164,7 +1160,7 @@ mod tests {
1164 NostrUrlDecoded::from_str(&url)?, 1160 NostrUrlDecoded::from_str(&url)?,
1165 NostrUrlDecoded { 1161 NostrUrlDecoded {
1166 original_string: url.clone(), 1162 original_string: url.clone(),
1167 coordinates: HashSet::from([get_model_coordinate(true)]), 1163 coordinate: get_model_coordinate(true),
1168 protocol: None, 1164 protocol: None,
1169 user: None, 1165 user: None,
1170 }, 1166 },
@@ -1183,7 +1179,7 @@ mod tests {
1183 NostrUrlDecoded::from_str(&url)?, 1179 NostrUrlDecoded::from_str(&url)?,
1184 NostrUrlDecoded { 1180 NostrUrlDecoded {
1185 original_string: url.clone(), 1181 original_string: url.clone(),
1186 coordinates: HashSet::from([Coordinate { 1182 coordinate: Coordinate {
1187 identifier: "ngit".to_string(), 1183 identifier: "ngit".to_string(),
1188 public_key: PublicKey::parse( 1184 public_key: PublicKey::parse(
1189 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", 1185 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
@@ -1194,7 +1190,7 @@ mod tests {
1194 RelayUrl::parse("wss://nos.lol/").unwrap(), 1190 RelayUrl::parse("wss://nos.lol/").unwrap(),
1195 RelayUrl::parse("wss://relay.damus.io/").unwrap(), 1191 RelayUrl::parse("wss://relay.damus.io/").unwrap(),
1196 ], 1192 ],
1197 }]), 1193 },
1198 protocol: None, 1194 protocol: None,
1199 user: None, 1195 user: None,
1200 }, 1196 },
@@ -1209,7 +1205,7 @@ mod tests {
1209 NostrUrlDecoded::from_str(&url)?, 1205 NostrUrlDecoded::from_str(&url)?,
1210 NostrUrlDecoded { 1206 NostrUrlDecoded {
1211 original_string: url.clone(), 1207 original_string: url.clone(),
1212 coordinates: HashSet::from([get_model_coordinate(false)]), 1208 coordinate: get_model_coordinate(false),
1213 protocol: Some(ServerProtocol::Ssh), 1209 protocol: Some(ServerProtocol::Ssh),
1214 user: None, 1210 user: None,
1215 }, 1211 },
@@ -1224,7 +1220,7 @@ mod tests {
1224 NostrUrlDecoded::from_str(&url)?, 1220 NostrUrlDecoded::from_str(&url)?,
1225 NostrUrlDecoded { 1221 NostrUrlDecoded {
1226 original_string: url.clone(), 1222 original_string: url.clone(),
1227 coordinates: HashSet::from([get_model_coordinate(false)]), 1223 coordinate: get_model_coordinate(false),
1228 protocol: Some(ServerProtocol::Ssh), 1224 protocol: Some(ServerProtocol::Ssh),
1229 user: Some("fred".to_string()), 1225 user: Some("fred".to_string()),
1230 }, 1226 },