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.rs171
1 files changed, 94 insertions, 77 deletions
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 {
36 36
37#[derive(Debug, PartialEq)] 37#[derive(Debug, PartialEq)]
38pub struct NostrUrlDecoded { 38pub struct NostrUrlDecoded {
39 pub original_string: String,
39 pub coordinates: HashSet<Coordinate>, 40 pub coordinates: HashSet<Coordinate>,
40 pub protocol: Option<ServerProtocol>, 41 pub protocol: Option<ServerProtocol>,
41 pub user: Option<String>, 42 pub user: Option<String>,
@@ -145,6 +146,7 @@ impl std::str::FromStr for NostrUrlDecoded {
145 } 146 }
146 147
147 Ok(Self { 148 Ok(Self {
149 original_string: url.to_string(),
148 coordinates, 150 coordinates,
149 protocol, 151 protocol,
150 user, 152 user,
@@ -831,11 +833,11 @@ mod tests {
831 833
832 #[test] 834 #[test]
833 fn from_naddr() -> Result<()> { 835 fn from_naddr() -> Result<()> {
836 let url = "nostr://naddr1qqzxuemfwsqs6amnwvaz7tmwdaejumr0dspzpgqgmmc409hm4xsdd74sf68a2uyf9pwel4g9mfdg8l5244t6x4jdqvzqqqrhnym0k2qj".to_string();
834 assert_eq!( 837 assert_eq!(
835 NostrUrlDecoded::from_str( 838 NostrUrlDecoded::from_str(&url)?,
836 "nostr://naddr1qqzxuemfwsqs6amnwvaz7tmwdaejumr0dspzpgqgmmc409hm4xsdd74sf68a2uyf9pwel4g9mfdg8l5244t6x4jdqvzqqqrhnym0k2qj"
837 )?,
838 NostrUrlDecoded { 839 NostrUrlDecoded {
840 original_string: url.clone(),
839 coordinates: HashSet::from([Coordinate { 841 coordinates: HashSet::from([Coordinate {
840 identifier: "ngit".to_string(), 842 identifier: "ngit".to_string(),
841 public_key: PublicKey::parse( 843 public_key: PublicKey::parse(
@@ -851,16 +853,19 @@ mod tests {
851 ); 853 );
852 Ok(()) 854 Ok(())
853 } 855 }
856
854 mod from_npub_slash_identifier { 857 mod from_npub_slash_identifier {
855 use super::*; 858 use super::*;
856 859
857 #[test] 860 #[test]
858 fn without_relay() -> Result<()> { 861 fn without_relay() -> Result<()> {
862 let url =
863 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit"
864 .to_string();
859 assert_eq!( 865 assert_eq!(
860 NostrUrlDecoded::from_str( 866 NostrUrlDecoded::from_str(&url)?,
861 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit"
862 )?,
863 NostrUrlDecoded { 867 NostrUrlDecoded {
868 original_string: url.clone(),
864 coordinates: HashSet::from([get_model_coordinate(false)]), 869 coordinates: HashSet::from([get_model_coordinate(false)]),
865 protocol: None, 870 protocol: None,
866 user: None, 871 user: None,
@@ -870,16 +875,15 @@ mod tests {
870 } 875 }
871 876
872 mod with_url_parameters { 877 mod with_url_parameters {
873
874 use super::*; 878 use super::*;
875 879
876 #[test] 880 #[test]
877 fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { 881 fn with_relay_without_scheme_defaults_to_wss() -> Result<()> {
882 let url = "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay=nos.lol".to_string();
878 assert_eq!( 883 assert_eq!(
879 NostrUrlDecoded::from_str( 884 NostrUrlDecoded::from_str(&url)?,
880 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay=nos.lol"
881 )?,
882 NostrUrlDecoded { 885 NostrUrlDecoded {
886 original_string: url.clone(),
883 coordinates: HashSet::from([get_model_coordinate(true)]), 887 coordinates: HashSet::from([get_model_coordinate(true)]),
884 protocol: None, 888 protocol: None,
885 user: None, 889 user: None,
@@ -890,12 +894,14 @@ mod tests {
890 894
891 #[test] 895 #[test]
892 fn with_encoded_relay() -> Result<()> { 896 fn with_encoded_relay() -> Result<()> {
897 let url = format!(
898 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}",
899 urlencoding::encode("wss://nos.lol")
900 );
893 assert_eq!( 901 assert_eq!(
894 NostrUrlDecoded::from_str(&format!( 902 NostrUrlDecoded::from_str(&url)?,
895 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}",
896 urlencoding::encode("wss://nos.lol")
897 ))?,
898 NostrUrlDecoded { 903 NostrUrlDecoded {
904 original_string: url.clone(),
899 coordinates: HashSet::from([get_model_coordinate(true)]), 905 coordinates: HashSet::from([get_model_coordinate(true)]),
900 protocol: None, 906 protocol: None,
901 user: None, 907 user: None,
@@ -903,41 +909,44 @@ mod tests {
903 ); 909 );
904 Ok(()) 910 Ok(())
905 } 911 }
912
906 #[test] 913 #[test]
907 fn with_multiple_encoded_relays() -> Result<()> { 914 fn with_multiple_encoded_relays() -> Result<()> {
908 assert_eq!( 915 let url = format!(
909 NostrUrlDecoded::from_str(&format!( 916 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}&relay1={}",
910 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}&relay1={}", 917 urlencoding::encode("wss://nos.lol"),
911 urlencoding::encode("wss://nos.lol"), 918 urlencoding::encode("wss://relay.damus.io"),
912 urlencoding::encode("wss://relay.damus.io"),
913 ))?,
914 NostrUrlDecoded {
915 coordinates: HashSet::from([Coordinate {
916 identifier: "ngit".to_string(),
917 public_key: PublicKey::parse(
918 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
919 )
920 .unwrap(),
921 kind: nostr_sdk::Kind::GitRepoAnnouncement,
922 relays: vec![
923 "wss://nos.lol/".to_string(),
924 "wss://relay.damus.io/".to_string(),
925 ],
926 }]),
927 protocol: None,
928 user: None,
929 },
930 ); 919 );
920 assert_eq!(
921 NostrUrlDecoded::from_str(&url)?,
922 NostrUrlDecoded {
923 original_string: url.clone(),
924 coordinates: HashSet::from([Coordinate {
925 identifier: "ngit".to_string(),
926 public_key: PublicKey::parse(
927 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
928 )
929 .unwrap(),
930 kind: nostr_sdk::Kind::GitRepoAnnouncement,
931 relays: vec![
932 "wss://nos.lol/".to_string(),
933 "wss://relay.damus.io/".to_string(),
934 ],
935 }]),
936 protocol: None,
937 user: None,
938 },
939 );
931 Ok(()) 940 Ok(())
932 } 941 }
933 942
934 #[test] 943 #[test]
935 fn with_server_protocol() -> Result<()> { 944 fn with_server_protocol() -> Result<()> {
945 let url = "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh".to_string();
936 assert_eq!( 946 assert_eq!(
937 NostrUrlDecoded::from_str( 947 NostrUrlDecoded::from_str(&url)?,
938 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh"
939 )?,
940 NostrUrlDecoded { 948 NostrUrlDecoded {
949 original_string: url.clone(),
941 coordinates: HashSet::from([get_model_coordinate(false)]), 950 coordinates: HashSet::from([get_model_coordinate(false)]),
942 protocol: Some(ServerProtocol::Ssh), 951 protocol: Some(ServerProtocol::Ssh),
943 user: None, 952 user: None,
@@ -945,13 +954,14 @@ mod tests {
945 ); 954 );
946 Ok(()) 955 Ok(())
947 } 956 }
957
948 #[test] 958 #[test]
949 fn with_server_protocol_and_user() -> Result<()> { 959 fn with_server_protocol_and_user() -> Result<()> {
960 let url = "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh&user=fred".to_string();
950 assert_eq!( 961 assert_eq!(
951 NostrUrlDecoded::from_str( 962 NostrUrlDecoded::from_str(&url)?,
952 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh&user=fred"
953 )?,
954 NostrUrlDecoded { 963 NostrUrlDecoded {
964 original_string: url.clone(),
955 coordinates: HashSet::from([get_model_coordinate(false)]), 965 coordinates: HashSet::from([get_model_coordinate(false)]),
956 protocol: Some(ServerProtocol::Ssh), 966 protocol: Some(ServerProtocol::Ssh),
957 user: Some("fred".to_string()), 967 user: Some("fred".to_string()),
@@ -960,16 +970,17 @@ mod tests {
960 Ok(()) 970 Ok(())
961 } 971 }
962 } 972 }
973
963 mod with_parameters_embedded_with_slashes { 974 mod with_parameters_embedded_with_slashes {
964 use super::*; 975 use super::*;
965 976
966 #[test] 977 #[test]
967 fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { 978 fn with_relay_without_scheme_defaults_to_wss() -> Result<()> {
979 let url = "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit".to_string();
968 assert_eq!( 980 assert_eq!(
969 NostrUrlDecoded::from_str( 981 NostrUrlDecoded::from_str(&url)?,
970 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit"
971 )?,
972 NostrUrlDecoded { 982 NostrUrlDecoded {
983 original_string: url.clone(),
973 coordinates: HashSet::from([get_model_coordinate(true)]), 984 coordinates: HashSet::from([get_model_coordinate(true)]),
974 protocol: None, 985 protocol: None,
975 user: None, 986 user: None,
@@ -980,12 +991,14 @@ mod tests {
980 991
981 #[test] 992 #[test]
982 fn with_encoded_relay() -> Result<()> { 993 fn with_encoded_relay() -> Result<()> {
994 let url = format!(
995 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/ngit",
996 urlencoding::encode("wss://nos.lol")
997 );
983 assert_eq!( 998 assert_eq!(
984 NostrUrlDecoded::from_str(&format!( 999 NostrUrlDecoded::from_str(&url)?,
985 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/ngit",
986 urlencoding::encode("wss://nos.lol")
987 ))?,
988 NostrUrlDecoded { 1000 NostrUrlDecoded {
1001 original_string: url.clone(),
989 coordinates: HashSet::from([get_model_coordinate(true)]), 1002 coordinates: HashSet::from([get_model_coordinate(true)]),
990 protocol: None, 1003 protocol: None,
991 user: None, 1004 user: None,
@@ -993,41 +1006,44 @@ mod tests {
993 ); 1006 );
994 Ok(()) 1007 Ok(())
995 } 1008 }
1009
996 #[test] 1010 #[test]
997 fn with_multiple_encoded_relays() -> Result<()> { 1011 fn with_multiple_encoded_relays() -> Result<()> {
998 assert_eq!( 1012 let url = format!(
999 NostrUrlDecoded::from_str(&format!( 1013 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/{}/ngit",
1000 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/{}/ngit", 1014 urlencoding::encode("wss://nos.lol"),
1001 urlencoding::encode("wss://nos.lol"), 1015 urlencoding::encode("wss://relay.damus.io"),
1002 urlencoding::encode("wss://relay.damus.io"),
1003 ))?,
1004 NostrUrlDecoded {
1005 coordinates: HashSet::from([Coordinate {
1006 identifier: "ngit".to_string(),
1007 public_key: PublicKey::parse(
1008 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
1009 )
1010 .unwrap(),
1011 kind: nostr_sdk::Kind::GitRepoAnnouncement,
1012 relays: vec![
1013 "wss://nos.lol/".to_string(),
1014 "wss://relay.damus.io/".to_string(),
1015 ],
1016 }]),
1017 protocol: None,
1018 user: None,
1019 },
1020 ); 1016 );
1017 assert_eq!(
1018 NostrUrlDecoded::from_str(&url)?,
1019 NostrUrlDecoded {
1020 original_string: url.clone(),
1021 coordinates: HashSet::from([Coordinate {
1022 identifier: "ngit".to_string(),
1023 public_key: PublicKey::parse(
1024 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
1025 )
1026 .unwrap(),
1027 kind: nostr_sdk::Kind::GitRepoAnnouncement,
1028 relays: vec![
1029 "wss://nos.lol/".to_string(),
1030 "wss://relay.damus.io/".to_string(),
1031 ],
1032 }]),
1033 protocol: None,
1034 user: None,
1035 },
1036 );
1021 Ok(()) 1037 Ok(())
1022 } 1038 }
1023 1039
1024 #[test] 1040 #[test]
1025 fn with_server_protocol() -> Result<()> { 1041 fn with_server_protocol() -> Result<()> {
1042 let url = "nostr://ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit".to_string();
1026 assert_eq!( 1043 assert_eq!(
1027 NostrUrlDecoded::from_str( 1044 NostrUrlDecoded::from_str(&url)?,
1028 "nostr://ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit"
1029 )?,
1030 NostrUrlDecoded { 1045 NostrUrlDecoded {
1046 original_string: url.clone(),
1031 coordinates: HashSet::from([get_model_coordinate(false)]), 1047 coordinates: HashSet::from([get_model_coordinate(false)]),
1032 protocol: Some(ServerProtocol::Ssh), 1048 protocol: Some(ServerProtocol::Ssh),
1033 user: None, 1049 user: None,
@@ -1035,13 +1051,14 @@ mod tests {
1035 ); 1051 );
1036 Ok(()) 1052 Ok(())
1037 } 1053 }
1054
1038 #[test] 1055 #[test]
1039 fn with_server_protocol_and_user() -> Result<()> { 1056 fn with_server_protocol_and_user() -> Result<()> {
1057 let url = "nostr://fred@ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit".to_string();
1040 assert_eq!( 1058 assert_eq!(
1041 NostrUrlDecoded::from_str( 1059 NostrUrlDecoded::from_str(&url)?,
1042 "nostr://fred@ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit"
1043 )?,
1044 NostrUrlDecoded { 1060 NostrUrlDecoded {
1061 original_string: url.clone(),
1045 coordinates: HashSet::from([get_model_coordinate(false)]), 1062 coordinates: HashSet::from([get_model_coordinate(false)]),
1046 protocol: Some(ServerProtocol::Ssh), 1063 protocol: Some(ServerProtocol::Ssh),
1047 user: Some("fred".to_string()), 1064 user: Some("fred".to_string()),