diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-06 10:43:34 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-06 11:05:05 +0100 |
| commit | 935bc0ca630d7964082966e4c0caeb255f5a4f57 (patch) | |
| tree | 21a2eec2abf81a54714c40c974ecc3c4e9170e11 /src/bin/git_remote_nostr/utils.rs | |
| parent | 92e06d80540a12d91e23ed6e557cc074d90d4d66 (diff) | |
fix(remote): improve protocol selction / fallback
abstract the protocols and order to try under different scenarios
add some additional scenarios eg hardcoded http
tweak error reporting
Diffstat (limited to 'src/bin/git_remote_nostr/utils.rs')
| -rw-r--r-- | src/bin/git_remote_nostr/utils.rs | 93 |
1 files changed, 92 insertions, 1 deletions
diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index c53c34f..a31dcbf 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs | |||
| @@ -10,7 +10,10 @@ use ngit::{ | |||
| 10 | get_all_proposal_patch_events_from_cache, get_events_from_cache, | 10 | get_all_proposal_patch_events_from_cache, get_events_from_cache, |
| 11 | get_proposals_and_revisions_from_cache, | 11 | get_proposals_and_revisions_from_cache, |
| 12 | }, | 12 | }, |
| 13 | git::{Repo, RepoActions}, | 13 | git::{ |
| 14 | nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol}, | ||
| 15 | Repo, RepoActions, | ||
| 16 | }, | ||
| 14 | git_events::{ | 17 | git_events::{ |
| 15 | event_is_revision_root, event_to_cover_letter, get_most_recent_patch_with_ancestors, | 18 | event_is_revision_root, event_to_cover_letter, get_most_recent_patch_with_ancestors, |
| 16 | status_kinds, | 19 | status_kinds, |
| @@ -246,3 +249,91 @@ pub fn find_proposal_and_patches_by_branch_name<'a>( | |||
| 246 | } | 249 | } |
| 247 | }) | 250 | }) |
| 248 | } | 251 | } |
| 252 | |||
| 253 | pub fn join_with_and<T: ToString>(items: &[T]) -> String { | ||
| 254 | match items.len() { | ||
| 255 | 0 => String::new(), | ||
| 256 | 1 => items[0].to_string(), | ||
| 257 | _ => { | ||
| 258 | let last_item = items.last().unwrap().to_string(); | ||
| 259 | let rest = &items[..items.len() - 1]; | ||
| 260 | format!( | ||
| 261 | "{} and {}", | ||
| 262 | rest.iter() | ||
| 263 | .map(std::string::ToString::to_string) | ||
| 264 | .collect::<Vec<_>>() | ||
| 265 | .join(", "), | ||
| 266 | last_item | ||
| 267 | ) | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | /// get an ordered vector of server protocols to attempt | ||
| 273 | pub fn get_read_protocols_to_try( | ||
| 274 | server_url: &CloneUrl, | ||
| 275 | decoded_nostr_url: &NostrUrlDecoded, | ||
| 276 | ) -> Vec<ServerProtocol> { | ||
| 277 | if server_url.protocol() == ServerProtocol::Filesystem { | ||
| 278 | vec![(ServerProtocol::Filesystem)] | ||
| 279 | } else if let Some(protocol) = &decoded_nostr_url.protocol { | ||
| 280 | vec![protocol.clone()] | ||
| 281 | } else if server_url.protocol() == ServerProtocol::Http { | ||
| 282 | vec![ | ||
| 283 | ServerProtocol::UnauthHttp, | ||
| 284 | ServerProtocol::Ssh, | ||
| 285 | ServerProtocol::Http, | ||
| 286 | ] | ||
| 287 | } else if server_url.protocol() == ServerProtocol::Ftp { | ||
| 288 | vec![ServerProtocol::Ftp, ServerProtocol::Ssh] | ||
| 289 | } else { | ||
| 290 | vec![ | ||
| 291 | ServerProtocol::UnauthHttps, | ||
| 292 | ServerProtocol::Ssh, | ||
| 293 | ServerProtocol::Https, | ||
| 294 | ] | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | #[cfg(test)] | ||
| 299 | mod tests { | ||
| 300 | use super::*; | ||
| 301 | mod join_with_and { | ||
| 302 | use super::*; | ||
| 303 | #[test] | ||
| 304 | fn test_empty() { | ||
| 305 | let items: Vec<&str> = vec![]; | ||
| 306 | assert_eq!(join_with_and(&items), ""); | ||
| 307 | } | ||
| 308 | |||
| 309 | #[test] | ||
| 310 | fn test_single_item() { | ||
| 311 | let items = vec!["apple"]; | ||
| 312 | assert_eq!(join_with_and(&items), "apple"); | ||
| 313 | } | ||
| 314 | |||
| 315 | #[test] | ||
| 316 | fn test_two_items() { | ||
| 317 | let items = vec!["apple", "banana"]; | ||
| 318 | assert_eq!(join_with_and(&items), "apple and banana"); | ||
| 319 | } | ||
| 320 | |||
| 321 | #[test] | ||
| 322 | fn test_three_items() { | ||
| 323 | let items = vec!["apple", "banana", "cherry"]; | ||
| 324 | assert_eq!(join_with_and(&items), "apple, banana and cherry"); | ||
| 325 | } | ||
| 326 | |||
| 327 | #[test] | ||
| 328 | fn test_four_items() { | ||
| 329 | let items = vec!["apple", "banana", "cherry", "date"]; | ||
| 330 | assert_eq!(join_with_and(&items), "apple, banana, cherry and date"); | ||
| 331 | } | ||
| 332 | |||
| 333 | #[test] | ||
| 334 | fn test_multiple_items() { | ||
| 335 | let items = vec!["one", "two", "three", "four", "five"]; | ||
| 336 | assert_eq!(join_with_and(&items), "one, two, three, four and five"); | ||
| 337 | } | ||
| 338 | } | ||
| 339 | } | ||