Remove glob imports of C-like enums

This commit is contained in:
Jonas Platte 2020-07-26 17:13:08 +02:00
parent 3f3cf83ceb
commit 1862d15365
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
4 changed files with 63 additions and 61 deletions

View File

@ -98,14 +98,14 @@ impl From<RangeToInclusive<UInt>> for RoomMemberCountIs {
impl Display for RoomMemberCountIs {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use ComparisonOperator::*;
use ComparisonOperator as Op;
let prefix = match self.prefix {
Eq => "",
Lt => "<",
Gt => ">",
Ge => ">=",
Le => "<=",
Op::Eq => "",
Op::Lt => "<",
Op::Gt => ">",
Op::Ge => ">=",
Op::Le => "<=",
};
write!(f, "{}{}", prefix, self.count)
@ -126,15 +126,15 @@ impl FromStr for RoomMemberCountIs {
type Err = js_int::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use ComparisonOperator::*;
use ComparisonOperator as Op;
let (prefix, count_str) = match s {
s if s.starts_with("<=") => (Le, &s[2..]),
s if s.starts_with('<') => (Lt, &s[1..]),
s if s.starts_with(">=") => (Ge, &s[2..]),
s if s.starts_with('>') => (Gt, &s[1..]),
s if s.starts_with("==") => (Eq, &s[2..]),
s => (Eq, s),
s if s.starts_with("<=") => (Op::Le, &s[2..]),
s if s.starts_with('<') => (Op::Lt, &s[1..]),
s if s.starts_with(">=") => (Op::Ge, &s[2..]),
s if s.starts_with('>') => (Op::Gt, &s[1..]),
s if s.starts_with("==") => (Op::Eq, &s[2..]),
s => (Op::Eq, s),
};
Ok(RoomMemberCountIs { prefix, count: UInt::from_str(count_str)? })
@ -153,24 +153,24 @@ impl<'de> Deserialize<'de> for RoomMemberCountIs {
impl RangeBounds<UInt> for RoomMemberCountIs {
fn start_bound(&self) -> Bound<&UInt> {
use ComparisonOperator::*;
use ComparisonOperator as Op;
match self.prefix {
Eq => Bound::Included(&self.count),
Lt | Le => Bound::Unbounded,
Gt => Bound::Excluded(&self.count),
Ge => Bound::Included(&self.count),
Op::Eq => Bound::Included(&self.count),
Op::Lt | Op::Le => Bound::Unbounded,
Op::Gt => Bound::Excluded(&self.count),
Op::Ge => Bound::Included(&self.count),
}
}
fn end_bound(&self) -> Bound<&UInt> {
use ComparisonOperator::*;
use ComparisonOperator as Op;
match self.prefix {
Eq => Bound::Included(&self.count),
Gt | Ge => Bound::Unbounded,
Lt => Bound::Excluded(&self.count),
Le => Bound::Included(&self.count),
Op::Eq => Bound::Included(&self.count),
Op::Gt | Op::Ge => Bound::Unbounded,
Op::Lt => Bound::Excluded(&self.count),
Op::Le => Bound::Included(&self.count),
}
}
}

View File

@ -254,13 +254,13 @@ fn expand_any_redacted(
attrs: &[Attribute],
variants: &[Ident],
) -> TokenStream {
use EventKindVariation::*;
use EventKindVariation as V;
if kind.is_state() {
let full_state = expand_any_with_deser(kind, events, attrs, variants, &Redacted);
let sync_state = expand_any_with_deser(kind, events, attrs, variants, &RedactedSync);
let full_state = expand_any_with_deser(kind, events, attrs, variants, &V::Redacted);
let sync_state = expand_any_with_deser(kind, events, attrs, variants, &V::RedactedSync);
let stripped_state =
expand_any_with_deser(kind, events, attrs, variants, &RedactedStripped);
expand_any_with_deser(kind, events, attrs, variants, &V::RedactedStripped);
quote! {
#full_state
@ -270,8 +270,8 @@ fn expand_any_redacted(
#stripped_state
}
} else if kind.is_message() {
let full_message = expand_any_with_deser(kind, events, attrs, variants, &Redacted);
let sync_message = expand_any_with_deser(kind, events, attrs, variants, &RedactedSync);
let full_message = expand_any_with_deser(kind, events, attrs, variants, &V::Redacted);
let sync_message = expand_any_with_deser(kind, events, attrs, variants, &V::RedactedSync);
quote! {
#full_message
@ -495,9 +495,9 @@ fn generate_custom_variant(
event_struct: &Ident,
var: &EventKindVariation,
) -> (TokenStream, TokenStream) {
use EventKindVariation::*;
use EventKindVariation as V;
if matches!(var, Redacted | RedactedSync | RedactedStripped) {
if matches!(var, V::Redacted | V::RedactedSync | V::RedactedStripped) {
(
quote! {
/// A redacted event not defined by the Matrix specification
@ -559,12 +559,12 @@ fn accessor_methods(
var: &EventKindVariation,
variants: &[Ident],
) -> Option<TokenStream> {
use EventKindVariation::*;
use EventKindVariation as V;
let ident = kind.to_event_enum_ident(var)?;
// matching `EventKindVariation`s
if let Redacted | RedactedSync | RedactedStripped = var {
if let V::Redacted | V::RedactedSync | V::RedactedStripped = var {
return redacted_accessor_methods(kind, var, variants);
}

View File

@ -92,20 +92,20 @@ impl EventKind {
}
pub fn to_event_ident(&self, var: &EventKindVariation) -> Option<Ident> {
use EventKindVariation::*;
use EventKindVariation as V;
// this match is only used to validate the input
match (self, var) {
(_, Full)
| (Self::Ephemeral, Sync)
| (Self::Message, Sync)
| (Self::State, Sync)
| (Self::State, Stripped)
| (Self::Message, Redacted)
| (Self::State, Redacted)
| (Self::Message, RedactedSync)
| (Self::State, RedactedSync)
| (Self::State, RedactedStripped) => {
(_, V::Full)
| (Self::Ephemeral, V::Sync)
| (Self::Message, V::Sync)
| (Self::State, V::Sync)
| (Self::State, V::Stripped)
| (Self::Message, V::Redacted)
| (Self::State, V::Redacted)
| (Self::Message, V::RedactedSync)
| (Self::State, V::RedactedSync)
| (Self::State, V::RedactedStripped) => {
Some(Ident::new(&format!("{}{}", var, self), Span::call_site()))
}
_ => None,

View File

@ -170,7 +170,9 @@ fn membership_change(
sender: &UserId,
state_key: &str,
) -> MembershipChange {
use MembershipState::*;
use MembershipChange as Ch;
use MembershipState as St;
let prev_content = if let Some(prev_content) = &prev_content {
prev_content
} else {
@ -178,38 +180,38 @@ fn membership_change(
avatar_url: None,
displayname: None,
is_direct: None,
membership: Leave,
membership: St::Leave,
third_party_invite: None,
}
};
match (prev_content.membership, &content.membership) {
(Invite, Invite) | (Leave, Leave) | (Ban, Ban) => MembershipChange::None,
(Invite, Join) | (Leave, Join) => MembershipChange::Joined,
(Invite, Leave) => {
(St::Invite, St::Invite) | (St::Leave, St::Leave) | (St::Ban, St::Ban) => Ch::None,
(St::Invite, St::Join) | (St::Leave, St::Join) => Ch::Joined,
(St::Invite, St::Leave) => {
if sender == state_key {
MembershipChange::InvitationRevoked
Ch::InvitationRevoked
} else {
MembershipChange::InvitationRejected
Ch::InvitationRejected
}
}
(Invite, Ban) | (Leave, Ban) => MembershipChange::Banned,
(Join, Invite) | (Ban, Invite) | (Ban, Join) => MembershipChange::Error,
(Join, Join) => MembershipChange::ProfileChanged {
(St::Invite, St::Ban) | (St::Leave, St::Ban) => Ch::Banned,
(St::Join, St::Invite) | (St::Ban, St::Invite) | (St::Ban, St::Join) => Ch::Error,
(St::Join, St::Join) => Ch::ProfileChanged {
displayname_changed: prev_content.displayname != content.displayname,
avatar_url_changed: prev_content.avatar_url != content.avatar_url,
},
(Join, Leave) => {
(St::Join, St::Leave) => {
if sender == state_key {
MembershipChange::Left
Ch::Left
} else {
MembershipChange::Kicked
Ch::Kicked
}
}
(Join, Ban) => MembershipChange::KickedAndBanned,
(Leave, Invite) => MembershipChange::Invited,
(Ban, Leave) => MembershipChange::Unbanned,
(Knock, _) | (_, Knock) => MembershipChange::NotImplemented,
(St::Join, St::Ban) => Ch::KickedAndBanned,
(St::Leave, St::Invite) => Ch::Invited,
(St::Ban, St::Leave) => Ch::Unbanned,
(St::Knock, _) | (_, St::Knock) => Ch::NotImplemented,
}
}