Use js_int macros to improve readability

master
Jonas Platte 2020-07-11 21:06:21 +02:00
parent ffc5204109
commit 9b5f95672b
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
4 changed files with 24 additions and 30 deletions

View File

@ -46,7 +46,7 @@ use crate::events::{
use crate::identifiers::{RoomAliasId, RoomId, UserId}; use crate::identifiers::{RoomAliasId, RoomId, UserId};
use crate::js_int::{Int, UInt}; use crate::js_int::{uint, Int, UInt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(feature = "messages")] #[cfg(feature = "messages")]
@ -253,12 +253,11 @@ impl RoomName {
let joined = self.joined_member_count.unwrap_or(UInt::MIN); let joined = self.joined_member_count.unwrap_or(UInt::MIN);
let invited = self.invited_member_count.unwrap_or(UInt::MIN); let invited = self.invited_member_count.unwrap_or(UInt::MIN);
let heroes = UInt::new(self.heroes.len() as u64).unwrap(); let heroes = UInt::new(self.heroes.len() as u64).unwrap();
let one = UInt::new(1).unwrap();
let invited_joined = if invited + joined == UInt::MIN { let invited_joined = if invited + joined == UInt::MIN {
UInt::MIN UInt::MIN
} else { } else {
invited + joined - one invited + joined - uint!(1)
}; };
let members = joined_members.values().chain(invited_members.values()); let members = joined_members.values().chain(invited_members.values());
@ -277,7 +276,7 @@ impl RoomName {
// stabilize ordering // stabilize ordering
names.sort(); names.sort();
names.join(", ") names.join(", ")
} else if heroes < invited_joined && invited + joined > one { } else if heroes < invited_joined && invited + joined > uint!(1) {
let mut names = members let mut names = members
.filter(|m| m.user_id != *own_user_id) .filter(|m| m.user_id != *own_user_id)
.take(3) .take(3)
@ -1094,13 +1093,10 @@ mod test {
assert!(room.power_levels.is_some()); assert!(room.power_levels.is_some());
assert_eq!( assert_eq!(
room.power_levels.as_ref().unwrap().kick, room.power_levels.as_ref().unwrap().kick,
crate::js_int::Int::new(50).unwrap() crate::js_int::int!(50)
); );
let admin = room.joined_members.get(&user_id).unwrap(); let admin = room.joined_members.get(&user_id).unwrap();
assert_eq!( assert_eq!(admin.power_level.unwrap(), crate::js_int::int!(100));
admin.power_level.unwrap(),
crate::js_int::Int::new(100).unwrap()
);
} }
#[async_test] #[async_test]

View File

@ -23,7 +23,7 @@ use crate::events::room::{
use crate::events::StateEventStub; use crate::events::StateEventStub;
use crate::identifiers::{RoomId, UserId}; use crate::identifiers::{RoomId, UserId};
use crate::js_int::{Int, UInt}; use crate::js_int::{int, Int, UInt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
// Notes: if Alice invites Bob into a room we will get an event with the sender as Alice and the state key as Bob. // Notes: if Alice invites Bob into a room we will get an event with the sender as Alice and the state key as Bob.
@ -143,8 +143,8 @@ impl RoomMember {
self.power_level = Some(event.content.users_default); self.power_level = Some(event.content.users_default);
} }
if max_power > Int::from(0) { if max_power > int!(0) {
self.power_level_norm = Some((self.power_level.unwrap() * Int::from(100)) / max_power); self.power_level_norm = Some((self.power_level.unwrap() * int!(100)) / max_power);
} }
changed changed
@ -216,7 +216,7 @@ mod test {
use crate::identifiers::{RoomId, UserId}; use crate::identifiers::{RoomId, UserId};
use crate::{BaseClient, Session}; use crate::{BaseClient, Session};
use crate::js_int::Int; use crate::js_int::int;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*; use wasm_bindgen_test::*;
@ -258,7 +258,7 @@ mod test {
.joined_members .joined_members
.get(&UserId::try_from("@example:localhost").unwrap()) .get(&UserId::try_from("@example:localhost").unwrap())
.unwrap(); .unwrap();
assert_eq!(member.power_level, Int::new(100)); assert_eq!(member.power_level, Some(int!(100)));
} }
#[async_test] #[async_test]
@ -283,7 +283,7 @@ mod test {
.get(&UserId::try_from("@example:localhost").unwrap()) .get(&UserId::try_from("@example:localhost").unwrap())
.unwrap(); .unwrap();
assert_eq!(member.power_level, Int::new(100)); assert_eq!(member.power_level, Some(int!(100)));
assert!(member.avatar_url.is_none()); assert!(member.avatar_url.is_none());
assert_eq!(member.last_active_ago, None); assert_eq!(member.last_active_ago, None);

View File

@ -12,7 +12,7 @@ version = "0.1.0"
[dependencies] [dependencies]
instant = { version = "0.1.4", features = ["wasm-bindgen", "now"] } instant = { version = "0.1.4", features = ["wasm-bindgen", "now"] }
js_int = "0.1.5" js_int = "0.1.8"
[dependencies.ruma] [dependencies.ruma]
git = "https://github.com/ruma/ruma" git = "https://github.com/ruma/ruma"

View File

@ -1485,9 +1485,10 @@ impl OlmMachine {
mod test { mod test {
static USER_ID: &str = "@bob:example.org"; static USER_ID: &str = "@bob:example.org";
use matrix_sdk_common::js_int::UInt; use matrix_sdk_common::js_int::uint;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::convert::TryInto;
use std::sync::atomic::AtomicU64; use std::sync::atomic::AtomicU64;
use std::time::SystemTime; use std::time::SystemTime;
@ -1681,20 +1682,18 @@ mod test {
.unwrap(); .unwrap();
assert!(machine.should_upload_keys().await); assert!(machine.should_upload_keys().await);
response.one_time_key_counts.insert( response
keys::KeyAlgorithm::SignedCurve25519, .one_time_key_counts
UInt::try_from(10).unwrap(), .insert(keys::KeyAlgorithm::SignedCurve25519, uint!(10));
);
machine machine
.receive_keys_upload_response(&response) .receive_keys_upload_response(&response)
.await .await
.unwrap(); .unwrap();
assert!(machine.should_upload_keys().await); assert!(machine.should_upload_keys().await);
response.one_time_key_counts.insert( response
keys::KeyAlgorithm::SignedCurve25519, .one_time_key_counts
UInt::try_from(50).unwrap(), .insert(keys::KeyAlgorithm::SignedCurve25519, uint!(50));
);
machine machine
.receive_keys_upload_response(&response) .receive_keys_upload_response(&response)
.await .await
@ -1718,10 +1717,9 @@ mod test {
assert!(machine.should_upload_keys().await); assert!(machine.should_upload_keys().await);
assert!(machine.generate_one_time_keys().await.is_ok()); assert!(machine.generate_one_time_keys().await.is_ok());
response.one_time_key_counts.insert( response
keys::KeyAlgorithm::SignedCurve25519, .one_time_key_counts
UInt::try_from(50).unwrap(), .insert(keys::KeyAlgorithm::SignedCurve25519, uint!(50));
);
machine machine
.receive_keys_upload_response(&response) .receive_keys_upload_response(&response)
.await .await
@ -1829,7 +1827,7 @@ mod test {
let mut response = keys_upload_response(); let mut response = keys_upload_response();
response.one_time_key_counts.insert( response.one_time_key_counts.insert(
keys::KeyAlgorithm::SignedCurve25519, keys::KeyAlgorithm::SignedCurve25519,
UInt::new_wrapping(one_time_keys.unwrap().len() as u64), (one_time_keys.unwrap().len() as u64).try_into().unwrap(),
); );
machine machine