Skip to content

Commit

Permalink
Use bitflags to hold error states
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons committed Dec 14, 2023
1 parent b5e9e27 commit 4e7005b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
19 changes: 10 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions crates/feedback2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ gpui = { package = "gpui2", path = "../gpui2" }
language = { package = "language2", path = "../language2" }
menu = { package = "menu2", path = "../menu2" }
project = { package = "project2", path = "../project2" }
regex.workspace = true
search = { package = "search2", path = "../search2" }
settings = { package = "settings2", path = "../settings2" }
theme = { package = "theme2", path = "../theme2" }
ui = { package = "ui2", path = "../ui2" }
util = { path = "../util" }
workspace = { package = "workspace2", path = "../workspace2"}

bitflags = "2.4.1"
human_bytes = "0.4.1"

anyhow.workspace = true
futures.workspace = true
human_bytes = "0.4.1"
isahc.workspace = true
lazy_static.workspace = true
log.workspace = true
postage.workspace = true
regex.workspace = true
serde.workspace = true
serde_derive.workspace = true
smallvec.workspace = true
Expand Down
33 changes: 18 additions & 15 deletions crates/feedback2/src/feedback_modal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{ops::RangeInclusive, sync::Arc, time::Duration};

use anyhow::{anyhow, bail};
use bitflags::bitflags;
use client::{Client, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
use db::kvp::KEY_VALUE_STORE;
use editor::{Editor, EditorEvent};
Expand Down Expand Up @@ -48,15 +49,17 @@ struct FeedbackRequestBody<'a> {
token: &'a str,
}

#[derive(Debug, Clone, PartialEq)]
enum InvalidStateIssue {
EmailAddress,
CharacterCount,
bitflags! {
#[derive(Debug, Clone, PartialEq)]
struct InvalidStateFlags: u8 {
const EmailAddress = 0b00000001;
const CharacterCount = 0b00000010;
}
}

#[derive(Debug, Clone, PartialEq)]
enum CannotSubmitReason {
InvalidState { issues: Vec<InvalidStateIssue> },
InvalidState { flags: InvalidStateFlags },
AwaitingSubmission,
}

Expand Down Expand Up @@ -322,45 +325,45 @@ impl FeedbackModal {
return;
}

let mut invalid_state_issues = Vec::new();
let mut invalid_state_flags = InvalidStateFlags::empty();

let valid_email_address = match self.email_address_editor.read(cx).text_option(cx) {
Some(email_address) => Regex::new(EMAIL_REGEX).unwrap().is_match(&email_address),
None => true,
};

if !valid_email_address {
invalid_state_issues.push(InvalidStateIssue::EmailAddress);
invalid_state_flags |= InvalidStateFlags::EmailAddress;
}

if !FEEDBACK_CHAR_LIMIT.contains(&self.character_count) {
invalid_state_issues.push(InvalidStateIssue::CharacterCount);
invalid_state_flags |= InvalidStateFlags::CharacterCount;
}

if invalid_state_issues.is_empty() {
if invalid_state_flags.is_empty() {
self.submission_state = Some(SubmissionState::CanSubmit);
} else {
self.submission_state = Some(SubmissionState::CannotSubmit {
reason: CannotSubmitReason::InvalidState {
issues: invalid_state_issues,
flags: invalid_state_flags,
},
});
}
}

fn valid_email_address(&self) -> bool {
!self.in_invalid_state(InvalidStateIssue::EmailAddress)
!self.in_invalid_state(InvalidStateFlags::EmailAddress)
}

fn valid_character_count(&self) -> bool {
!self.in_invalid_state(InvalidStateIssue::CharacterCount)
!self.in_invalid_state(InvalidStateFlags::CharacterCount)
}

fn in_invalid_state(&self, a: InvalidStateIssue) -> bool {
fn in_invalid_state(&self, flag: InvalidStateFlags) -> bool {
match self.submission_state {
Some(SubmissionState::CannotSubmit {
reason: CannotSubmitReason::InvalidState { ref issues },
}) => issues.contains(&a),
reason: CannotSubmitReason::InvalidState { ref flags },
}) => flags.contains(flag),
_ => false,
}
}
Expand Down

0 comments on commit 4e7005b

Please sign in to comment.