~anteater/vgmms

6d6173ab51cc94587fc6308cfc28418ec50d73eb — anteater 3 months ago 6b16784 master
actions: factor out target parsing
1 files changed, 24 insertions(+), 40 deletions(-)

M src/actions.rs
M src/actions.rs => src/actions.rs +24 -40
@@ 34,6 34,22 @@ pub fn generate_actions(my_country: Country) -> Vec<VNode<WindowModel>> {
			on activate = |_a, _| ui_message()
		/>}
	}
	fn parse_target_variant(num_strs: Option<&glib::Variant>, my_country: Country) -> Option<Vec<Number>> {
		let num_strs = num_strs.unwrap().get::<(Vec<String>,)>().unwrap().0;
		parse_target(&num_strs, my_country)
	}
	fn parse_target(num_strs: &[String], my_country: Country) -> Option<Vec<Number>> {
		let mut nums = vec![];
		for num_str in num_strs.iter() {
			if let Some(n) = Number::normalize(num_str, my_country) {
				nums.push(n);
			} else {
				eprintln!("could not parse number '{}'", num_str);
				return None
			}
		}
		Some(nums)
	}
	vec![
		attachment_action("open-attachment", AttachmentAction::Open),
		attachment_action("save-attachment-dialog", AttachmentAction::SaveAsDialog),


@@ 54,18 70,7 @@ pub fn generate_actions(my_country: Country) -> Vec<VNode<WindowModel>> {
		gtk! {<SimpleAction::new("open-chat",
			Some(glib::VariantTy::new("(as)").unwrap())) enabled=true
			on activate = |_a, num_strs| {
				let (num_strs,) = num_strs.unwrap().get::<(Vec<String>,)>().unwrap();
				let mut valid = true;
				let mut nums = vec![];
				for num_str in num_strs.iter() {
					if let Some(n) = Number::normalize(num_str, my_country) {
						nums.push(n);
					} else {
						eprintln!("could not parse number '{}'", num_str);
						valid = false;
					}
				}
				if valid {
				if let Some(nums) = parse_target_variant(num_strs, my_country) {
					UiMessage::OpenChat(nums)
				} else {
					UiMessage::Nop


@@ 75,18 80,7 @@ pub fn generate_actions(my_country: Country) -> Vec<VNode<WindowModel>> {
		gtk! {<SimpleAction::new("open-chat-and-activate",
			Some(glib::VariantTy::new("(as)").unwrap())) enabled=true
			on activate = |_a, num_strs| {
				let (num_strs,) = num_strs.unwrap().get::<(Vec<String>,)>().unwrap();
				let mut valid = true;
				let mut nums = vec![];
				for num_str in num_strs.iter() {
					if let Some(n) = Number::normalize(num_str, my_country) {
						nums.push(n);
					} else {
						eprintln!("could not parse number '{}'", num_str);
						valid = false;
					}
				}
				if valid {
				if let Some(nums) = parse_target_variant(num_strs, my_country) {
					UiMessage::OpenChatAndActivate(nums)
				} else {
					UiMessage::Nop


@@ 99,20 93,9 @@ pub fn generate_actions(my_country: Country) -> Vec<VNode<WindowModel>> {
				let (num_strs, message, attachment_paths) =
					nums_message_atts.unwrap().get::<(Vec<String>, String, Vec<std::path::PathBuf>)>().unwrap();

				let mut nums_valid = true;
				let mut nums = vec![];
				for num_str in num_strs.iter() {
					if let Some(n) = Number::normalize(num_str, my_country) {
						nums.push(n);
					} else {
						eprintln!("could not parse number '{}'", num_str);
						nums_valid = false;
					}
				}

				let mut atts_valid = true;
				let mut items = vec![];
				if nums_valid {
				if let Some(mut nums) = parse_target(&num_strs, my_country) {
					nums.sort();
					if message.len() > 0 {
						items.push(DraftItem::Text(message));


@@ 127,10 110,11 @@ pub fn generate_actions(my_country: Country) -> Vec<VNode<WindowModel>> {
							},
						}
					}
				}

				if nums_valid && atts_valid {
					UiMessage::Send((Chat {numbers: nums}, items))
					if atts_valid {
						UiMessage::Send((Chat {numbers: nums}, items))
					} else {
						UiMessage::Nop
					}
				} else {
					UiMessage::Nop
				}