From 6e7e7bd3497d2a77fda34e27f65955b8ac09b3be Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 6 Sep 2024 10:45:21 +0100 Subject: fix(remote): `list` apply protocols selection used in fetch and tweak the error reporting --- src/lib/git/nostr_url.rs | 171 ++++++++++++++++++++++++++--------------------- 1 file changed, 94 insertions(+), 77 deletions(-) (limited to 'src/lib/git') diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs index ea8d221..2717916 100644 --- a/src/lib/git/nostr_url.rs +++ b/src/lib/git/nostr_url.rs @@ -36,6 +36,7 @@ impl fmt::Display for ServerProtocol { #[derive(Debug, PartialEq)] pub struct NostrUrlDecoded { + pub original_string: String, pub coordinates: HashSet, pub protocol: Option, pub user: Option, @@ -145,6 +146,7 @@ impl std::str::FromStr for NostrUrlDecoded { } Ok(Self { + original_string: url.to_string(), coordinates, protocol, user, @@ -831,11 +833,11 @@ mod tests { #[test] fn from_naddr() -> Result<()> { + let url = "nostr://naddr1qqzxuemfwsqs6amnwvaz7tmwdaejumr0dspzpgqgmmc409hm4xsdd74sf68a2uyf9pwel4g9mfdg8l5244t6x4jdqvzqqqrhnym0k2qj".to_string(); assert_eq!( - NostrUrlDecoded::from_str( - "nostr://naddr1qqzxuemfwsqs6amnwvaz7tmwdaejumr0dspzpgqgmmc409hm4xsdd74sf68a2uyf9pwel4g9mfdg8l5244t6x4jdqvzqqqrhnym0k2qj" - )?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([Coordinate { identifier: "ngit".to_string(), public_key: PublicKey::parse( @@ -851,16 +853,19 @@ mod tests { ); Ok(()) } + mod from_npub_slash_identifier { use super::*; #[test] fn without_relay() -> Result<()> { + let url = + "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" + .to_string(); assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" - )?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(false)]), protocol: None, user: None, @@ -870,16 +875,15 @@ mod tests { } mod with_url_parameters { - use super::*; #[test] fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { + let url = "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay=nos.lol".to_string(); assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay=nos.lol" - )?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(true)]), protocol: None, user: None, @@ -890,12 +894,14 @@ mod tests { #[test] fn with_encoded_relay() -> Result<()> { + let url = format!( + "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}", + urlencoding::encode("wss://nos.lol") + ); assert_eq!( - NostrUrlDecoded::from_str(&format!( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}", - urlencoding::encode("wss://nos.lol") - ))?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(true)]), protocol: None, user: None, @@ -903,41 +909,44 @@ mod tests { ); Ok(()) } + #[test] fn with_multiple_encoded_relays() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str(&format!( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}&relay1={}", - urlencoding::encode("wss://nos.lol"), - urlencoding::encode("wss://relay.damus.io"), - ))?, - NostrUrlDecoded { - coordinates: HashSet::from([Coordinate { - identifier: "ngit".to_string(), - public_key: PublicKey::parse( - "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", - ) - .unwrap(), - kind: nostr_sdk::Kind::GitRepoAnnouncement, - relays: vec![ - "wss://nos.lol/".to_string(), - "wss://relay.damus.io/".to_string(), - ], - }]), - protocol: None, - user: None, - }, + let url = format!( + "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}&relay1={}", + urlencoding::encode("wss://nos.lol"), + urlencoding::encode("wss://relay.damus.io"), ); + assert_eq!( + NostrUrlDecoded::from_str(&url)?, + NostrUrlDecoded { + original_string: url.clone(), + coordinates: HashSet::from([Coordinate { + identifier: "ngit".to_string(), + public_key: PublicKey::parse( + "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", + ) + .unwrap(), + kind: nostr_sdk::Kind::GitRepoAnnouncement, + relays: vec![ + "wss://nos.lol/".to_string(), + "wss://relay.damus.io/".to_string(), + ], + }]), + protocol: None, + user: None, + }, + ); Ok(()) } #[test] fn with_server_protocol() -> Result<()> { + let url = "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh".to_string(); assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh" - )?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(false)]), protocol: Some(ServerProtocol::Ssh), user: None, @@ -945,13 +954,14 @@ mod tests { ); Ok(()) } + #[test] fn with_server_protocol_and_user() -> Result<()> { + let url = "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh&user=fred".to_string(); assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh&user=fred" - )?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(false)]), protocol: Some(ServerProtocol::Ssh), user: Some("fred".to_string()), @@ -960,16 +970,17 @@ mod tests { Ok(()) } } + mod with_parameters_embedded_with_slashes { use super::*; #[test] fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { + let url = "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit".to_string(); assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit" - )?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(true)]), protocol: None, user: None, @@ -980,12 +991,14 @@ mod tests { #[test] fn with_encoded_relay() -> Result<()> { + let url = format!( + "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/ngit", + urlencoding::encode("wss://nos.lol") + ); assert_eq!( - NostrUrlDecoded::from_str(&format!( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/ngit", - urlencoding::encode("wss://nos.lol") - ))?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(true)]), protocol: None, user: None, @@ -993,41 +1006,44 @@ mod tests { ); Ok(()) } + #[test] fn with_multiple_encoded_relays() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str(&format!( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/{}/ngit", - urlencoding::encode("wss://nos.lol"), - urlencoding::encode("wss://relay.damus.io"), - ))?, - NostrUrlDecoded { - coordinates: HashSet::from([Coordinate { - identifier: "ngit".to_string(), - public_key: PublicKey::parse( - "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", - ) - .unwrap(), - kind: nostr_sdk::Kind::GitRepoAnnouncement, - relays: vec![ - "wss://nos.lol/".to_string(), - "wss://relay.damus.io/".to_string(), - ], - }]), - protocol: None, - user: None, - }, + let url = format!( + "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/{}/ngit", + urlencoding::encode("wss://nos.lol"), + urlencoding::encode("wss://relay.damus.io"), ); + assert_eq!( + NostrUrlDecoded::from_str(&url)?, + NostrUrlDecoded { + original_string: url.clone(), + coordinates: HashSet::from([Coordinate { + identifier: "ngit".to_string(), + public_key: PublicKey::parse( + "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", + ) + .unwrap(), + kind: nostr_sdk::Kind::GitRepoAnnouncement, + relays: vec![ + "wss://nos.lol/".to_string(), + "wss://relay.damus.io/".to_string(), + ], + }]), + protocol: None, + user: None, + }, + ); Ok(()) } #[test] fn with_server_protocol() -> Result<()> { + let url = "nostr://ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit".to_string(); assert_eq!( - NostrUrlDecoded::from_str( - "nostr://ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" - )?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(false)]), protocol: Some(ServerProtocol::Ssh), user: None, @@ -1035,13 +1051,14 @@ mod tests { ); Ok(()) } + #[test] fn with_server_protocol_and_user() -> Result<()> { + let url = "nostr://fred@ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit".to_string(); assert_eq!( - NostrUrlDecoded::from_str( - "nostr://fred@ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" - )?, + NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { + original_string: url.clone(), coordinates: HashSet::from([get_model_coordinate(false)]), protocol: Some(ServerProtocol::Ssh), user: Some("fred".to_string()), -- cgit v1.2.3