diff options
Diffstat (limited to 'src/funcs/get_branch_event_from_user_input.rs')
| -rw-r--r-- | src/funcs/get_branch_event_from_user_input.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/funcs/get_branch_event_from_user_input.rs b/src/funcs/get_branch_event_from_user_input.rs new file mode 100644 index 0000000..1b2cd03 --- /dev/null +++ b/src/funcs/get_branch_event_from_user_input.rs | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | use std::path::PathBuf; | ||
| 2 | |||
| 3 | use nostr::{Event}; | ||
| 4 | |||
| 5 | use crate::{branch_refs::BranchRefs, repo_config::RepoConfig, cli_helpers::valid_event_id_from_input}; | ||
| 6 | |||
| 7 | pub fn get_branch_event_from_user_input( | ||
| 8 | branch_string_param:&Option<String>, | ||
| 9 | branch_refs: &BranchRefs, | ||
| 10 | repo_dir_path: &PathBuf, | ||
| 11 | ) -> Event { | ||
| 12 | get_branch_event_with_options( | ||
| 13 | branch_string_param, | ||
| 14 | branch_refs, | ||
| 15 | repo_dir_path, | ||
| 16 | true, | ||
| 17 | ) | ||
| 18 | } | ||
| 19 | |||
| 20 | pub fn get_unmapped_branch_event_from_user_input( | ||
| 21 | branch_string_param:&Option<String>, | ||
| 22 | branch_refs: &BranchRefs, | ||
| 23 | repo_dir_path: &PathBuf, | ||
| 24 | ) -> Event { | ||
| 25 | get_branch_event_with_options( | ||
| 26 | branch_string_param, | ||
| 27 | branch_refs, | ||
| 28 | repo_dir_path, | ||
| 29 | false, | ||
| 30 | ) | ||
| 31 | } | ||
| 32 | |||
| 33 | fn get_branch_event_with_options( | ||
| 34 | branch_string_param:&Option<String>, | ||
| 35 | branch_refs: &BranchRefs, | ||
| 36 | repo_dir_path: &PathBuf, | ||
| 37 | retrun_unmapped_branches: bool, | ||
| 38 | ) -> Event { | ||
| 39 | |||
| 40 | let mut string_param = branch_string_param.clone(); | ||
| 41 | loop { | ||
| 42 | let valid_id = valid_event_id_from_input( | ||
| 43 | string_param.clone(), | ||
| 44 | &"nevent note or hex of remote branch to pull".to_string(), | ||
| 45 | ); | ||
| 46 | |||
| 47 | match branch_refs.branches.iter().find(|g| g.id.eq(&valid_id)) { | ||
| 48 | Some(branch_event) => { | ||
| 49 | let repo_config = RepoConfig::open(repo_dir_path); | ||
| 50 | if !retrun_unmapped_branches { | ||
| 51 | match repo_config.branch_name_from_id(&valid_id.to_string()) { | ||
| 52 | // branch is alreay mapped | ||
| 53 | Some(name) => { | ||
| 54 | println!( | ||
| 55 | "local branch '{}' is already linked to this nostr branch. having multiple local branches linked to a nostr branch isn't supported right now. feel free to create a feature request :)", | ||
| 56 | name, | ||
| 57 | ); | ||
| 58 | string_param = None; | ||
| 59 | continue | ||
| 60 | } | ||
| 61 | None => (), | ||
| 62 | } | ||
| 63 | } | ||
| 64 | break branch_event.clone(); | ||
| 65 | }, | ||
| 66 | None => { | ||
| 67 | println!("valid id but the branch cannot be found in this respository on the specified relays. try again."); | ||
| 68 | string_param = None; | ||
| 69 | continue | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||