Use js_int types for numbers.

Closes #27.
This commit is contained in:
Jimmy Cuadra 2019-06-16 16:16:37 -07:00
parent 04cdc96b8b
commit c3f48c5be3
33 changed files with 94 additions and 51 deletions

View File

@ -16,6 +16,10 @@ ruma-identifiers = "0.13.0"
ruma-signatures = "0.4.2"
serde_json = "1.0.39"
[dependencies.js_int]
version = "0.1.0"
features = ["serde"]
[dependencies.serde]
version = "1.0.92"
features = ["derive"]

View File

@ -1,5 +1,6 @@
//! Types for the *m.call.answer* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
use super::SessionDescription;
@ -19,5 +20,5 @@ pub struct AnswerEventContent {
pub call_id: String,
/// The version of the VoIP specification this messages adheres to.
pub version: u64,
pub version: UInt,
}

View File

@ -1,5 +1,6 @@
//! Types for the *m.call.candidates* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
room_event! {
@ -19,7 +20,7 @@ pub struct CandidatesEventContent {
pub candidates: Vec<Candidate>,
/// The version of the VoIP specification this messages adheres to.
pub version: u64,
pub version: UInt,
}
/// An ICE (Interactive Connectivity Establishment) candidate.
@ -34,5 +35,5 @@ pub struct Candidate {
/// The index of the SDP "m" line this candidate is intended for.
#[serde(rename = "sdpMLineIndex")]
pub sdp_m_line_index: u64,
pub sdp_m_line_index: UInt,
}

View File

@ -1,5 +1,6 @@
//! Types for the *m.call.hangup* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
room_event! {
@ -15,7 +16,7 @@ pub struct HangupEventContent {
pub call_id: String,
/// The version of the VoIP specification this messages adheres to.
pub version: u64,
pub version: UInt,
/// Optional error reason for the hangup.
pub reason: Option<Reason>,

View File

@ -1,5 +1,6 @@
//! Types for the *m.call.invite* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
use super::SessionDescription;
@ -18,11 +19,11 @@ pub struct InviteEventContent {
/// The time in milliseconds that the invite is valid for. Once the invite age exceeds this
/// value, clients should discard it. They should also no longer show the call as awaiting an
/// answer in the UI.
pub lifetime: u64,
pub lifetime: UInt,
/// The session description object. The session description type must be *offer*.
pub offer: SessionDescription,
/// The version of the VoIP specification this messages adheres to.
pub version: u64,
pub version: UInt,
}

View File

@ -1,5 +1,6 @@
//! Types for the *m.key.verification.request* event.
use js_int::UInt;
use ruma_identifiers::DeviceId;
use serde::{Deserialize, Serialize};
@ -30,5 +31,5 @@ pub struct RequestEventContent {
///
/// If the request is in the future by more than 5 minutes or more than 10 minutes in the past,
/// the message should be ignored by the receiver.
pub timestamp: u64,
pub timestamp: UInt,
}

View File

@ -101,6 +101,7 @@
use std::fmt::{Debug, Display, Error as FmtError, Formatter, Result as FmtResult};
use js_int::UInt;
use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{
de::{Error as SerdeError, IntoDeserializer, Visitor},
@ -301,7 +302,7 @@ pub trait RoomEvent: Event {
/// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this event was
/// sent.
fn origin_server_ts(&self) -> u64;
fn origin_server_ts(&self) -> UInt;
/// The unique identifier for the room associated with this event.
///

View File

@ -93,7 +93,7 @@ macro_rules! room_event {
/// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this
/// event was sent.
pub origin_server_ts: u64,
pub origin_server_ts: UInt,
/// The unique identifier for the room associated with this event.
#[serde(skip_serializing_if="Option::is_none")]
@ -125,7 +125,7 @@ macro_rules! impl_room_event {
&self.event_id
}
fn origin_server_ts(&self) -> u64 {
fn origin_server_ts(&self) -> UInt {
self.origin_server_ts
}
@ -168,7 +168,7 @@ macro_rules! state_event {
pub event_type: $crate::EventType,
/// Timestamp in milliseconds on originating homeserver when this event was sent.
pub origin_server_ts: u64,
pub origin_server_ts: UInt,
/// The previous content for this state key, if any.
#[serde(skip_serializing_if = "Option::is_none")]

View File

@ -1,8 +1,8 @@
//! Types for the *m.presence* event.
use serde::{Deserialize, Serialize};
use js_int::UInt;
use ruma_identifiers::UserId;
use serde::{Deserialize, Serialize};
event! {
/// Informs the client of a user's presence state change.
@ -29,7 +29,7 @@ pub struct PresenceEventContent {
/// The last time since this user performed some action, in milliseconds.
#[serde(skip_serializing_if = "Option::is_none")]
pub last_active_ago: Option<u64>,
pub last_active_ago: Option<UInt>,
/// The presence state for this user.
pub presence: PresenceState,
@ -73,6 +73,7 @@ impl_enum! {
mod tests {
use std::convert::TryFrom;
use js_int::UInt;
use ruma_identifiers::UserId;
use serde_json::{from_str, to_string};
@ -88,7 +89,7 @@ mod tests {
avatar_url: Some("mxc://localhost:wefuiwegh8742w".to_string()),
currently_active: Some(false),
displayname: None,
last_active_ago: Some(2_478_593),
last_active_ago: Some(UInt::try_from(2_478_593).unwrap()),
presence: PresenceState::Online,
status_msg: Some("Making cupcakes".to_string()),
},

View File

@ -2,6 +2,7 @@
use std::collections::HashMap;
use js_int::UInt;
use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{Deserialize, Serialize};
@ -37,5 +38,5 @@ pub type UserReceipts = HashMap<UserId, Receipt>;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Receipt {
/// The timestamp (milliseconds since the Unix epoch) when the receipt was sent.
pub ts: Option<u64>,
pub ts: Option<UInt>,
}

View File

@ -4,6 +4,7 @@
use std::collections::HashMap;
use js_int::UInt;
use serde::{Deserialize, Serialize};
pub mod aliases;
@ -32,12 +33,12 @@ pub struct ImageInfo {
/// The height of the image in pixels.
#[serde(rename = "h")]
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<u64>,
pub height: Option<UInt>,
/// The width of the image in pixels.
#[serde(rename = "w")]
#[serde(skip_serializing_if = "Option::is_none")]
pub width: Option<u64>,
pub width: Option<UInt>,
/// The MIME type of the image, e.g. "image/png."
#[serde(skip_serializing_if = "Option::is_none")]
@ -45,7 +46,7 @@ pub struct ImageInfo {
/// The file size of the image in bytes.
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<u64>,
pub size: Option<UInt>,
/// Metadata about the image referred to in `thumbnail_url`.
#[serde(skip_serializing_if = "Option::is_none")]
@ -66,12 +67,12 @@ pub struct ThumbnailInfo {
/// The height of the thumbnail in pixels.
#[serde(rename = "h")]
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<u64>,
pub height: Option<UInt>,
/// The width of the thumbnail in pixels.
#[serde(rename = "w")]
#[serde(skip_serializing_if = "Option::is_none")]
pub width: Option<u64>,
pub width: Option<UInt>,
/// The MIME type of the thumbnail, e.g. "image/png."
#[serde(skip_serializing_if = "Option::is_none")]
@ -79,7 +80,7 @@ pub struct ThumbnailInfo {
/// The file size of the thumbnail in bytes.
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<u64>,
pub size: Option<UInt>,
}
/// A file sent to a room with end-to-end encryption enabled.

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.aliases* event.
use js_int::UInt;
use ruma_identifiers::RoomAliasId;
use serde::{Deserialize, Serialize};

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.avatar* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
use super::ImageInfo;

View File

@ -1,9 +1,11 @@
//! Types for the *m.room.canonical_alias* event.
use crate::empty_string_as_none;
use js_int::UInt;
use ruma_identifiers::RoomAliasId;
use serde::{Deserialize, Serialize};
use crate::empty_string_as_none;
state_event! {
/// Informs the room as to which alias is the canonical one.
pub struct CanonicalAliasEvent(CanonicalAliasEventContent) {}

View File

@ -2,6 +2,7 @@
use std::convert::TryFrom;
use js_int::UInt;
use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId};
use serde::{Deserialize, Serialize};

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.encrypted* event.
use js_int::UInt;
use ruma_identifiers::DeviceId;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{from_value, Value};
@ -52,7 +53,7 @@ pub struct CiphertextInfo {
/// The Olm message type.
#[serde(rename = "type")]
pub message_type: u64,
pub message_type: UInt,
}
/// The payload of an *m.room.encrypted* event using the *m.megolm.v1.aes-sha2* algorithm.

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.encryption* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
use crate::Algorithm;
@ -20,10 +21,10 @@ pub struct EncryptionEventContent {
/// How long the session should be used before changing it.
///
/// 604800000 (a week) is the recommended default.
pub rotation_period_ms: Option<u64>,
pub rotation_period_ms: Option<UInt>,
/// How many messages should be sent before changing the session.
///
/// 100 is the recommended default.
pub rotation_period_msgs: Option<u64>,
pub rotation_period_msgs: Option<UInt>,
}

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.guest_access* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
state_event! {

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.history_visibility* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
state_event! {

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.join_rules* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
state_event! {

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.member* event.
use js_int::UInt;
use ruma_identifiers::UserId;
use ruma_signatures::Signatures;
use serde::{Deserialize, Serialize};

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.message* event.
use js_int::UInt;
use ruma_identifiers::EventId;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{from_value, Value};
@ -119,7 +120,7 @@ pub struct AudioMessageEventContent {
pub struct AudioInfo {
/// The duration of the audio in milliseconds.
#[serde(skip_serializing_if = "Option::is_none")]
pub duration: Option<u64>,
pub duration: Option<UInt>,
/// The mimetype of the audio, e.g. "audio/aac."
#[serde(skip_serializing_if = "Option::is_none")]
@ -127,7 +128,7 @@ pub struct AudioInfo {
/// The size of the audio clip in bytes.
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<u64>,
pub size: Option<UInt>,
}
/// The payload of an emote message.
@ -184,7 +185,7 @@ pub struct FileInfo {
pub mimetype: Option<String>,
/// The size of the file in bytes.
pub size: Option<u64>,
pub size: Option<UInt>,
/// Metadata about the image referred to in `thumbnail_url`.
#[serde(skip_serializing_if = "Option::is_none")]
@ -369,17 +370,17 @@ pub struct VideoMessageEventContent {
pub struct VideoInfo {
/// The duration of the video in milliseconds.
#[serde(skip_serializing_if = "Option::is_none")]
pub duration: Option<u64>,
pub duration: Option<UInt>,
/// The height of the video in pixels.
#[serde(rename = "h")]
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<u64>,
pub height: Option<UInt>,
/// The width of the video in pixels.
#[serde(rename = "w")]
#[serde(skip_serializing_if = "Option::is_none")]
pub width: Option<u64>,
pub width: Option<UInt>,
/// The mimetype of the video, e.g. "video/mp4."
#[serde(skip_serializing_if = "Option::is_none")]
@ -387,7 +388,7 @@ pub struct VideoInfo {
/// The size of the video in bytes.
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<u64>,
pub size: Option<UInt>,
/// Metadata about an image.
#[serde(skip_serializing_if = "Option::is_none")]

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.message.feedback* event.
use js_int::UInt;
use ruma_identifiers::EventId;
use serde::{Deserialize, Serialize};

View File

@ -1,8 +1,10 @@
//! Types for the *m.room.name* event.
use crate::empty_string_as_none;
use js_int::UInt;
use serde::{Deserialize, Serialize};
use crate::empty_string_as_none;
state_event! {
/// A human-friendly room name designed to be displayed to the end-user.
pub struct NameEvent(NameEventContent) {}

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.pinned_events* event.
use js_int::UInt;
use ruma_identifiers::EventId;
use serde::{Deserialize, Serialize};
@ -17,6 +18,9 @@ pub struct PinnedEventsContent {
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use js_int::UInt;
use ruma_identifiers::{EventId, RoomId, UserId};
use serde_json::{from_str, to_string};
@ -36,7 +40,7 @@ mod tests {
content: content.clone(),
event_id: EventId::new("example.com").unwrap(),
event_type: EventType::RoomPinnedEvents,
origin_server_ts: 1_432_804_485_886,
origin_server_ts: UInt::try_from(1_432_804_485_886u64).unwrap(),
prev_content: None,
room_id: Some(RoomId::new("example.com").unwrap()),
sender: UserId::new("example.com").unwrap(),

View File

@ -2,6 +2,7 @@
use std::collections::HashMap;
use js_int::{Int, UInt};
use ruma_identifiers::UserId;
use serde::{Deserialize, Serialize};
@ -17,41 +18,41 @@ state_event! {
pub struct PowerLevelsEventContent {
/// The level required to ban a user.
#[serde(default = "default_power_level")]
pub ban: u64,
pub ban: Int,
/// The level required to send specific event types.
///
/// This is a mapping from event type to power level required.
pub events: HashMap<EventType, u64>,
pub events: HashMap<EventType, Int>,
/// The default level required to send message events.
#[serde(default)]
pub events_default: u64,
pub events_default: Int,
/// The level required to invite a user.
#[serde(default = "default_power_level")]
pub invite: u64,
pub invite: Int,
/// The level required to kick a user.
#[serde(default = "default_power_level")]
pub kick: u64,
pub kick: Int,
/// The level required to redact an event.
#[serde(default = "default_power_level")]
pub redact: u64,
pub redact: Int,
/// The default level required to send state events.
#[serde(default = "default_power_level")]
pub state_default: u64,
pub state_default: Int,
/// The power levels for specific users.
///
/// This is a mapping from `user_id` to power level for that user.
pub users: HashMap<UserId, u64>,
pub users: HashMap<UserId, Int>,
/// The default power level for every user in the room.
#[serde(default)]
pub users_default: u64,
pub users_default: Int,
/// The power level requirements for specific notification types.
///
@ -64,10 +65,10 @@ pub struct PowerLevelsEventContent {
pub struct NotificationPowerLevels {
/// The level required to trigger an `@room` notification.
#[serde(default = "default_power_level")]
pub room: u64,
pub room: Int,
}
/// Used to default power levels to 50 during deserialization.
fn default_power_level() -> u64 {
50
fn default_power_level() -> Int {
Int::from(50)
}

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.redaction* event.
use js_int::UInt;
use ruma_identifiers::EventId;
use serde::{Deserialize, Serialize};

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.server_acl* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
use crate::default_true;

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.third_party_invite* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
state_event! {

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.tombstone* event.
use js_int::UInt;
use ruma_identifiers::RoomId;
use serde::{Deserialize, Serialize};

View File

@ -1,5 +1,6 @@
//! Types for the *m.room.topic* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
state_event! {

View File

@ -1,5 +1,6 @@
//! Types for the *m.sticker* event.
use js_int::UInt;
use serde::{Deserialize, Serialize};
use crate::room::ImageInfo;

View File

@ -260,6 +260,7 @@ pub type StrippedRoomTopic = StrippedStateContent<TopicEventContent>;
mod tests {
use std::convert::TryFrom;
use js_int::UInt;
use ruma_identifiers::UserId;
use serde_json::{from_str, to_string};
@ -361,11 +362,14 @@ mod tests {
StrippedState::RoomAvatar(event) => {
let image_info = event.content.info.unwrap();
assert_eq!(image_info.height.unwrap(), 128);
assert_eq!(image_info.width.unwrap(), 128);
assert_eq!(image_info.height.unwrap(), UInt::try_from(128).unwrap());
assert_eq!(image_info.width.unwrap(), UInt::try_from(128).unwrap());
assert_eq!(image_info.mimetype.unwrap(), "image/jpeg");
assert_eq!(image_info.size.unwrap(), 1024);
assert_eq!(image_info.thumbnail_info.unwrap().size.unwrap(), 32);
assert_eq!(image_info.size.unwrap(), UInt::try_from(1024).unwrap());
assert_eq!(
image_info.thumbnail_info.unwrap().size.unwrap(),
UInt::try_from(32).unwrap()
);
assert_eq!(event.content.url, "https://domain.com/image.jpg");
assert_eq!(event.event_type, EventType::RoomAvatar);
assert_eq!(event.state_key, "");