upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/lib/git/nostr_url.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-11-29 16:09:38 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-11-29 16:57:46 +0000
commit8c763d0483309c85a32a7f4a20ba0279083ee40f (patch)
tree47c5dc7f5778760dc99804a7918be81ab383e778 /src/lib/git/nostr_url.rs
parent9b3b242652bf7c1ad638ff0f64f1d59aef7fe0ad (diff)
feat(init): set remote `origin`
check whether remote `origin` is nostr url and if not attempt to set it.
Diffstat (limited to 'src/lib/git/nostr_url.rs')
-rw-r--r--src/lib/git/nostr_url.rs140
1 files changed, 138 insertions, 2 deletions
diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs
index e4b6825..a501765 100644
--- a/src/lib/git/nostr_url.rs
+++ b/src/lib/git/nostr_url.rs
@@ -3,7 +3,7 @@ use std::{collections::HashSet, 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;
6use nostr_sdk::{PublicKey, RelayUrl, Url}; 6use nostr_sdk::{PublicKey, RelayUrl, ToBech32, Url};
7 7
8#[derive(Debug, PartialEq, Default, Clone)] 8#[derive(Debug, PartialEq, Default, Clone)]
9pub enum ServerProtocol { 9pub enum ServerProtocol {
@@ -61,6 +61,33 @@ pub struct NostrUrlDecoded {
61 pub user: Option<String>, 61 pub user: Option<String>,
62} 62}
63 63
64impl fmt::Display for NostrUrlDecoded {
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 write!(f, "nostr://")?;
67 if let Some(user) = &self.user {
68 write!(f, "{}@", user)?;
69 }
70 if let Some(protocol) = &self.protocol {
71 write!(f, "{}/", protocol)?;
72 }
73 let c = self.coordinates.iter().next().unwrap();
74 write!(f, "{}/", c.public_key.to_bech32().unwrap())?;
75 if let Some(relay) = c.relays.first() {
76 write!(
77 f,
78 "{}/",
79 urlencoding::encode(
80 relay
81 .as_str_without_trailing_slash()
82 .replace("wss://", "")
83 .as_str()
84 )
85 )?;
86 }
87 write!(f, "{}", c.identifier)
88 }
89}
90
64static INCORRECT_NOSTR_URL_FORMAT_ERROR: &str = "incorrect nostr git url format. try nostr://naddr123 or nostr://npub123/my-repo or nostr://ssh/npub123/relay.damus.io/my-repo"; 91static INCORRECT_NOSTR_URL_FORMAT_ERROR: &str = "incorrect nostr git url format. try nostr://naddr123 or nostr://npub123/my-repo or nostr://ssh/npub123/relay.damus.io/my-repo";
65 92
66impl std::str::FromStr for NostrUrlDecoded { 93impl std::str::FromStr for NostrUrlDecoded {
@@ -837,8 +864,117 @@ mod tests {
837 assert!(result.is_err()); 864 assert!(result.is_err());
838 } 865 }
839 } 866 }
867 mod nostr_git_url_format {
868 use std::collections::HashSet;
869
870 use nostr::nips::nip01::Coordinate;
871 use nostr_sdk::PublicKey;
872
873 use super::*;
874 use crate::git::nostr_url::NostrUrlDecoded;
875
876 #[test]
877 fn standard() -> Result<()> {
878 assert_eq!(
879 format!(
880 "{}",
881 NostrUrlDecoded {
882 original_string: String::new(),
883 coordinates: HashSet::from_iter(vec![Coordinate {
884 identifier: "ngit".to_string(),
885 public_key: PublicKey::parse(
886 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
887 )
888 .unwrap(),
889 kind: nostr_sdk::Kind::GitRepoAnnouncement,
890 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()],
891 }]),
892 protocol: None,
893 user: None,
894 }
895 ),
896 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit",
897 );
898 Ok(())
899 }
900
901 #[test]
902 fn no_relay() -> Result<()> {
903 assert_eq!(
904 format!(
905 "{}",
906 NostrUrlDecoded {
907 original_string: String::new(),
908 coordinates: HashSet::from_iter(vec![Coordinate {
909 identifier: "ngit".to_string(),
910 public_key: PublicKey::parse(
911 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
912 )
913 .unwrap(),
914 kind: nostr_sdk::Kind::GitRepoAnnouncement,
915 relays: vec![],
916 }]),
917 protocol: None,
918 user: None,
919 }
920 ),
921 "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit",
922 );
923 Ok(())
924 }
925
926 #[test]
927 fn with_protocol() -> Result<()> {
928 assert_eq!(
929 format!(
930 "{}",
931 NostrUrlDecoded {
932 original_string: String::new(),
933 coordinates: HashSet::from_iter(vec![Coordinate {
934 identifier: "ngit".to_string(),
935 public_key: PublicKey::parse(
936 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
937 )
938 .unwrap(),
939 kind: nostr_sdk::Kind::GitRepoAnnouncement,
940 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()],
941 }]),
942 protocol: Some(ServerProtocol::Ssh),
943 user: None,
944 }
945 ),
946 "nostr://ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit",
947 );
948 Ok(())
949 }
950
951 #[test]
952 fn with_protocol_and_user() -> Result<()> {
953 assert_eq!(
954 format!(
955 "{}",
956 NostrUrlDecoded {
957 original_string: String::new(),
958 coordinates: HashSet::from_iter(vec![Coordinate {
959 identifier: "ngit".to_string(),
960 public_key: PublicKey::parse(
961 "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr",
962 )
963 .unwrap(),
964 kind: nostr_sdk::Kind::GitRepoAnnouncement,
965 relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()],
966 }]),
967 protocol: Some(ServerProtocol::Ssh),
968 user: Some("bla".to_string()),
969 }
970 ),
971 "nostr://bla@ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit",
972 );
973 Ok(())
974 }
975 }
840 976
841 mod nostr_git_url_paramemters_from_str { 977 mod nostr_url_decoded_paramemters_from_str {
842 use std::str::FromStr; 978 use std::str::FromStr;
843 979
844 use super::*; 980 use super::*;