twilight_lavalink/model.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
//! Models to (de)serialize incoming/outgoing websocket events and HTTP
//! responses.
use serde::{Deserialize, Serialize};
/// The type of event that something is.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub enum Opcode {
/// Destroy a player from a node.
Destroy,
/// Equalize a player.
Equalizer,
/// Meta information about a track starting or ending.
Event,
/// Pause a player.
Pause,
/// Play a track.
Play,
/// An update about a player's current track.
PlayerUpdate,
/// Seek a player's active track to a new position.
Seek,
/// Updated statistics about a node.
Stats,
/// Stop a player.
Stop,
/// A combined voice server and voice state update.
VoiceUpdate,
/// Set the volume of a player.
Volume,
}
pub mod outgoing {
//! Events that clients send to Lavalink.
use super::Opcode;
use serde::{Deserialize, Serialize};
use twilight_model::{
gateway::payload::incoming::VoiceServerUpdate,
id::{marker::GuildMarker, Id},
};
/// An outgoing event to send to Lavalink.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(untagged)]
pub enum OutgoingEvent {
/// Destroy a player for a guild.
Destroy(Destroy),
/// Equalize a player.
Equalizer(Equalizer),
/// Pause or unpause a player.
Pause(Pause),
/// Play a track.
Play(Play),
/// Seek a player's active track to a new position.
Seek(Seek),
/// Stop a player.
Stop(Stop),
/// A combined voice server and voice state update.
VoiceUpdate(VoiceUpdate),
/// Set the volume of a player.
Volume(Volume),
}
impl From<Destroy> for OutgoingEvent {
fn from(event: Destroy) -> OutgoingEvent {
Self::Destroy(event)
}
}
impl From<Equalizer> for OutgoingEvent {
fn from(event: Equalizer) -> OutgoingEvent {
Self::Equalizer(event)
}
}
impl From<Pause> for OutgoingEvent {
fn from(event: Pause) -> OutgoingEvent {
Self::Pause(event)
}
}
impl From<Play> for OutgoingEvent {
fn from(event: Play) -> OutgoingEvent {
Self::Play(event)
}
}
impl From<Seek> for OutgoingEvent {
fn from(event: Seek) -> OutgoingEvent {
Self::Seek(event)
}
}
impl From<Stop> for OutgoingEvent {
fn from(event: Stop) -> OutgoingEvent {
Self::Stop(event)
}
}
impl From<VoiceUpdate> for OutgoingEvent {
fn from(event: VoiceUpdate) -> OutgoingEvent {
Self::VoiceUpdate(event)
}
}
impl From<Volume> for OutgoingEvent {
fn from(event: Volume) -> OutgoingEvent {
Self::Volume(event)
}
}
/// Destroy a player from a node.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Destroy {
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The opcode of the event.
pub op: Opcode,
}
impl Destroy {
/// Create a new destroy event.
pub const fn new(guild_id: Id<GuildMarker>) -> Self {
Self {
guild_id,
op: Opcode::Destroy,
}
}
}
impl From<Id<GuildMarker>> for Destroy {
fn from(guild_id: Id<GuildMarker>) -> Self {
Self {
guild_id,
op: Opcode::Destroy,
}
}
}
/// Equalize a player.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Equalizer {
/// The bands to use as part of the equalizer.
pub bands: Vec<EqualizerBand>,
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The opcode of the event.
pub op: Opcode,
}
impl Equalizer {
/// Create a new equalizer event.
pub fn new(guild_id: Id<GuildMarker>, bands: Vec<EqualizerBand>) -> Self {
Self::from((guild_id, bands))
}
}
impl From<(Id<GuildMarker>, Vec<EqualizerBand>)> for Equalizer {
fn from((guild_id, bands): (Id<GuildMarker>, Vec<EqualizerBand>)) -> Self {
Self {
bands,
guild_id,
op: Opcode::Equalizer,
}
}
}
/// A band of the equalizer event.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct EqualizerBand {
/// The band.
pub band: i64,
/// The gain.
pub gain: f64,
}
impl EqualizerBand {
/// Create a new equalizer band.
pub fn new(band: i64, gain: f64) -> Self {
Self::from((band, gain))
}
}
impl From<(i64, f64)> for EqualizerBand {
fn from((band, gain): (i64, f64)) -> Self {
Self { band, gain }
}
}
/// Pause or unpause a player.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Pause {
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The opcode of the event.
pub op: Opcode,
/// Whether to pause the player.
///
/// Set to `true` to pause or `false` to resume.
pub pause: bool,
}
impl Pause {
/// Create a new pause event.
///
/// Set to `true` to pause the player or `false` to resume it.
pub fn new(guild_id: Id<GuildMarker>, pause: bool) -> Self {
Self::from((guild_id, pause))
}
}
impl From<(Id<GuildMarker>, bool)> for Pause {
fn from((guild_id, pause): (Id<GuildMarker>, bool)) -> Self {
Self {
guild_id,
op: Opcode::Pause,
pause,
}
}
}
/// Play a track, optionally specifying to not skip the current track.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Play {
/// The position in milliseconds to end the track.
///
/// This currently [does nothing] as of this writing.
///
/// [does nothing]: https://github.com/freyacodes/Lavalink/issues/179
#[serde(skip_serializing_if = "Option::is_none")]
pub end_time: Option<u64>,
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// Whether or not to replace the currently playing track with this new
/// track.
///
/// Set to `true` to keep playing the current playing track, or `false`
/// to replace the current playing track with a new one.
pub no_replace: bool,
/// The opcode of the event.
pub op: Opcode,
/// The position in milliseconds to start the track from.
///
/// For example, set to 5000 to start the track 5 seconds in.
#[serde(skip_serializing_if = "Option::is_none")]
pub start_time: Option<u64>,
/// The base64 track information.
pub track: String,
}
impl Play {
/// Create a new play event.
pub fn new(
guild_id: Id<GuildMarker>,
track: impl Into<String>,
start_time: impl Into<Option<u64>>,
end_time: impl Into<Option<u64>>,
no_replace: bool,
) -> Self {
Self::from((guild_id, track, start_time, end_time, no_replace))
}
}
impl<T: Into<String>> From<(Id<GuildMarker>, T)> for Play {
fn from((guild_id, track): (Id<GuildMarker>, T)) -> Self {
Self::from((guild_id, track, None, None, true))
}
}
impl<T: Into<String>, S: Into<Option<u64>>> From<(Id<GuildMarker>, T, S)> for Play {
fn from((guild_id, track, start_time): (Id<GuildMarker>, T, S)) -> Self {
Self::from((guild_id, track, start_time, None, true))
}
}
impl<T: Into<String>, S: Into<Option<u64>>, E: Into<Option<u64>>>
From<(Id<GuildMarker>, T, S, E)> for Play
{
fn from((guild_id, track, start_time, end_time): (Id<GuildMarker>, T, S, E)) -> Self {
Self::from((guild_id, track, start_time, end_time, true))
}
}
impl<T: Into<String>, S: Into<Option<u64>>, E: Into<Option<u64>>>
From<(Id<GuildMarker>, T, S, E, bool)> for Play
{
fn from(
(guild_id, track, start_time, end_time, no_replace): (Id<GuildMarker>, T, S, E, bool),
) -> Self {
Self {
end_time: end_time.into(),
guild_id,
no_replace,
op: Opcode::Play,
start_time: start_time.into(),
track: track.into(),
}
}
}
/// Seek a player's active track to a new position.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Seek {
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The opcode of the event.
pub op: Opcode,
/// The position in milliseconds to seek to.
pub position: i64,
}
impl Seek {
/// Create a new seek event.
pub fn new(guild_id: Id<GuildMarker>, position: i64) -> Self {
Self::from((guild_id, position))
}
}
impl From<(Id<GuildMarker>, i64)> for Seek {
fn from((guild_id, position): (Id<GuildMarker>, i64)) -> Self {
Self {
guild_id,
op: Opcode::Seek,
position,
}
}
}
/// Stop a player.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Stop {
/// The opcode of the event.
pub op: Opcode,
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
}
impl Stop {
/// Create a new stop event.
pub fn new(guild_id: Id<GuildMarker>) -> Self {
Self::from(guild_id)
}
}
impl From<Id<GuildMarker>> for Stop {
fn from(guild_id: Id<GuildMarker>) -> Self {
Self {
guild_id,
op: Opcode::Stop,
}
}
}
/// A combined voice server and voice state update.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct VoiceUpdate {
/// The inner event being forwarded to a node.
pub event: VoiceServerUpdate,
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The opcode of the event.
pub op: Opcode,
/// The session ID of the voice channel.
pub session_id: String,
}
impl VoiceUpdate {
/// Create a new voice update event.
pub fn new(
guild_id: Id<GuildMarker>,
session_id: impl Into<String>,
event: VoiceServerUpdate,
) -> Self {
Self::from((guild_id, session_id, event))
}
}
impl<T: Into<String>> From<(Id<GuildMarker>, T, VoiceServerUpdate)> for VoiceUpdate {
fn from((guild_id, session_id, event): (Id<GuildMarker>, T, VoiceServerUpdate)) -> Self {
Self {
event,
guild_id,
op: Opcode::VoiceUpdate,
session_id: session_id.into(),
}
}
}
/// Set the volume of a player.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Volume {
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The opcode of the event.
pub op: Opcode,
/// The volume of the player from 0 to 1000. 100 is the default.
pub volume: i64,
}
impl Volume {
/// Create a new volume event.
pub fn new(guild_id: Id<GuildMarker>, volume: i64) -> Self {
Self::from((guild_id, volume))
}
}
impl From<(Id<GuildMarker>, i64)> for Volume {
fn from((guild_id, volume): (Id<GuildMarker>, i64)) -> Self {
Self {
guild_id,
op: Opcode::Volume,
volume,
}
}
}
}
pub mod incoming {
//! Events that Lavalink sends to clients.
use super::Opcode;
use serde::{Deserialize, Serialize};
use twilight_model::id::{marker::GuildMarker, Id};
/// An incoming event from a Lavalink node.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(untagged)]
pub enum IncomingEvent {
/// An update about the information of a player.
PlayerUpdate(PlayerUpdate),
/// New statistics about a node and its host.
Stats(Stats),
/// A track ended.
TrackEnd(TrackEnd),
/// A track started.
TrackStart(TrackStart),
/// The voice websocket connection was closed.
WeboscketClosed(WebsocketClosed),
}
impl From<PlayerUpdate> for IncomingEvent {
fn from(event: PlayerUpdate) -> IncomingEvent {
Self::PlayerUpdate(event)
}
}
impl From<Stats> for IncomingEvent {
fn from(event: Stats) -> IncomingEvent {
Self::Stats(event)
}
}
/// An update about the information of a player.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct PlayerUpdate {
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The opcode of the event.
pub op: Opcode,
/// The new state of the player.
pub state: PlayerUpdateState,
}
/// New statistics about a node and its host.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct PlayerUpdateState {
/// True when the player is connected to the voice gateway.
pub connected: bool,
/// Unix timestamp of the player in milliseconds.
pub time: i64,
/// Track position in milliseconds. None if not playing anything.
pub position: Option<i64>,
}
/// Statistics about a node and its host.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Stats {
/// CPU information about the node's host.
pub cpu: StatsCpu,
/// Statistics about audio frames.
#[serde(rename = "frameStats", skip_serializing_if = "Option::is_none")]
pub frames: Option<StatsFrames>,
/// Memory information about the node's host.
pub memory: StatsMemory,
/// The current number of total players (active and not active) within
/// the node.
pub players: u64,
/// The current number of active players within the node.
pub playing_players: u64,
/// The opcode of the event.
pub op: Opcode,
/// The uptime of the Lavalink server in seconds.
pub uptime: u64,
}
/// CPU information about a node and its host.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct StatsCpu {
/// The number of CPU cores.
pub cores: usize,
/// The load of the Lavalink server.
pub lavalink_load: f64,
/// The load of the system as a whole.
pub system_load: f64,
}
/// CPU information about a node and its host.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct StatsFrames {
/// The number of CPU cores.
pub sent: u64,
/// The load of the Lavalink server.
pub nulled: u64,
/// The load of the system as a whole.
pub deficit: u64,
}
/// Memory information about a node and its host.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct StatsMemory {
/// The number of bytes allocated.
pub allocated: u64,
/// The number of bytes free.
pub free: u64,
/// The number of bytes reservable.
pub reservable: u64,
/// The number of bytes used.
pub used: u64,
}
/// The type of track event that was received.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub enum TrackEventType {
/// A track for a player ended.
#[serde(rename = "TrackEndEvent")]
End,
/// A track for a player started.
#[serde(rename = "TrackStartEvent")]
Start,
/// The voice websocket connection to Discord has been closed.
#[serde(rename = "WebSocketClosedEvent")]
WebsocketClosed,
}
/// A track ended.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct TrackEnd {
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The type of track event.
#[serde(rename = "type")]
pub kind: TrackEventType,
/// The opcode of the event.
pub op: Opcode,
/// The reason that the track ended.
///
/// For example, this may be `"FINISHED"`.
pub reason: String,
/// The base64 track that was affected.
pub track: String,
}
/// A track started.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct TrackStart {
/// The guild ID of the player.
pub guild_id: Id<GuildMarker>,
/// The type of track event.
#[serde(rename = "type")]
pub kind: TrackEventType,
/// The opcode of the event.
pub op: Opcode,
/// The base64 track that was affected.
pub track: String,
}
/// The voice websocket connection to Discord has been closed.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct WebsocketClosed {
/// Guild ID of the associated player.
pub guild_id: Id<GuildMarker>,
/// Type of track event.
#[serde(rename = "type")]
pub kind: TrackEventType,
/// Lavalink websocket opcode of the event.
pub op: Opcode,
/// Discord websocket opcode that closed the connection.
pub code: u64,
/// True if Discord closed the connection, false if Lavalink closed it.
pub by_remote: bool,
/// Reason the connection was closed.
pub reason: String,
}
}
pub use self::{
incoming::{
IncomingEvent, PlayerUpdate, PlayerUpdateState, Stats, StatsCpu, StatsFrames, StatsMemory,
TrackEnd, TrackEventType, TrackStart, WebsocketClosed,
},
outgoing::{
Destroy, Equalizer, EqualizerBand, OutgoingEvent, Pause, Play, Seek, Stop, VoiceUpdate,
Volume,
},
};
#[cfg(test)]
mod tests {
use super::{
incoming::{
IncomingEvent, PlayerUpdate, PlayerUpdateState, Stats, StatsCpu, StatsFrames,
StatsMemory, TrackEnd, TrackEventType, TrackStart, WebsocketClosed,
},
outgoing::{
Destroy, Equalizer, EqualizerBand, OutgoingEvent, Pause, Play, Seek, Stop, VoiceUpdate,
Volume,
},
Opcode,
};
use serde::{Deserialize, Serialize};
use serde_test::Token;
use static_assertions::{assert_fields, assert_impl_all};
use std::fmt::Debug;
use twilight_model::{
gateway::payload::incoming::VoiceServerUpdate,
id::{marker::GuildMarker, Id},
};
assert_fields!(Destroy: guild_id, op);
assert_impl_all!(
Destroy: Clone,
Debug,
Deserialize<'static>,
Eq,
From<Id<GuildMarker>>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(EqualizerBand: band, gain);
assert_impl_all!(
EqualizerBand: Clone,
Debug,
Deserialize<'static>,
From<(i64, f64)>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(Equalizer: bands, guild_id, op);
assert_impl_all!(
Equalizer: Clone,
Debug,
Deserialize<'static>,
From<(Id<GuildMarker>, Vec<EqualizerBand>)>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_impl_all!(
IncomingEvent: Clone,
Debug,
Deserialize<'static>,
From<PlayerUpdate>,
From<Stats>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_impl_all!(
OutgoingEvent: Clone,
Debug,
Deserialize<'static>,
From<Destroy>,
From<Equalizer>,
From<Pause>,
From<Play>,
From<Seek>,
From<Stop>,
From<VoiceUpdate>,
From<Volume>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(Pause: guild_id, op, pause);
assert_impl_all!(
Pause: Clone,
Debug,
Deserialize<'static>,
Eq,
From<(Id<GuildMarker>, bool)>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(PlayerUpdateState: position, time);
assert_impl_all!(
PlayerUpdateState: Clone,
Debug,
Deserialize<'static>,
Eq,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(PlayerUpdate: guild_id, op, state);
assert_impl_all!(
PlayerUpdate: Clone,
Debug,
Deserialize<'static>,
Eq,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(Play: end_time, guild_id, no_replace, op, start_time, track);
assert_impl_all!(
Play: Clone,
Debug,
Deserialize<'static>,
Eq,
From<(Id<GuildMarker>, String)>,
From<(Id<GuildMarker>, String, Option<u64>)>,
From<(Id<GuildMarker>, String, u64)>,
From<(Id<GuildMarker>, String, Option<u64>, Option<u64>)>,
From<(Id<GuildMarker>, String, Option<u64>, u64)>,
From<(Id<GuildMarker>, String, u64, Option<u64>)>,
From<(Id<GuildMarker>, String, u64, u64)>,
From<(Id<GuildMarker>, String, Option<u64>, Option<u64>, bool)>,
From<(Id<GuildMarker>, String, Option<u64>, u64, bool)>,
From<(Id<GuildMarker>, String, u64, Option<u64>, bool)>,
From<(Id<GuildMarker>, String, u64, u64, bool)>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(Seek: guild_id, op, position);
assert_impl_all!(
Seek: Clone,
Debug,
Deserialize<'static>,
Eq,
From<(Id<GuildMarker>, i64)>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(
Stats: cpu,
frames,
memory,
players,
playing_players,
op,
uptime
);
assert_impl_all!(
Stats: Clone,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(StatsCpu: cores, lavalink_load, system_load);
assert_impl_all!(
StatsCpu: Clone,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(StatsFrames: deficit, nulled, sent);
assert_impl_all!(
StatsFrames: Clone,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(StatsMemory: allocated, free, reservable, used);
assert_impl_all!(
StatsMemory: Clone,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(Stop: guild_id, op);
assert_impl_all!(
Stop: Clone,
Debug,
Deserialize<'static>,
Eq,
From<Id<GuildMarker>>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(TrackEnd: guild_id, kind, op, reason, track);
assert_impl_all!(
TrackEnd: Clone,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_impl_all!(
TrackEventType: Clone,
Copy,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(TrackStart: guild_id, kind, op, track);
assert_impl_all!(
TrackStart: Clone,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(WebsocketClosed: guild_id, kind, op, code, reason, by_remote);
assert_impl_all!(
WebsocketClosed: Clone,
Debug,
Deserialize<'static>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(VoiceUpdate: event, guild_id, op, session_id);
assert_impl_all!(
VoiceUpdate: Clone,
Debug,
Deserialize<'static>,
Eq,
From<(Id<GuildMarker>, String, VoiceServerUpdate)>,
PartialEq,
Send,
Serialize,
Sync,
);
assert_fields!(Volume: guild_id, op, volume);
assert_impl_all!(
Volume: Clone,
Debug,
Deserialize<'static>,
Eq,
PartialEq,
Send,
Serialize,
Sync,
);
#[test]
fn stats_frames_not_provided() {
const LAVALINK_LOAD: f64 = 0.276_119_402_985_074_65;
const MEM_ALLOCATED: u64 = 62_914_560;
const MEM_FREE: u64 = 27_664_576;
const MEM_RESERVABLE: u64 = 4_294_967_296;
const MEM_USED: u64 = 35_249_984;
const SYSTEM_LOAD: f64 = 0.195_380_536_378_835_9;
let expected = Stats {
cpu: StatsCpu {
cores: 4,
lavalink_load: LAVALINK_LOAD,
system_load: SYSTEM_LOAD,
},
frames: None,
memory: StatsMemory {
allocated: MEM_ALLOCATED,
free: MEM_FREE,
reservable: MEM_RESERVABLE,
used: MEM_USED,
},
players: 0,
playing_players: 0,
op: Opcode::Stats,
uptime: 18589,
};
serde_test::assert_de_tokens(
&expected,
&[
Token::Struct {
name: "Stats",
len: 6,
},
Token::Str("cpu"),
Token::Struct {
name: "StatsCpu",
len: 3,
},
Token::Str("cores"),
Token::U64(4),
Token::Str("lavalinkLoad"),
Token::F64(LAVALINK_LOAD),
Token::Str("systemLoad"),
Token::F64(SYSTEM_LOAD),
Token::StructEnd,
Token::Str("memory"),
Token::Struct {
name: "StatsMemory",
len: 4,
},
Token::Str("allocated"),
Token::U64(MEM_ALLOCATED),
Token::Str("free"),
Token::U64(MEM_FREE),
Token::Str("reservable"),
Token::U64(MEM_RESERVABLE),
Token::Str("used"),
Token::U64(MEM_USED),
Token::StructEnd,
Token::Str("op"),
Token::UnitVariant {
name: "Opcode",
variant: "stats",
},
Token::Str("players"),
Token::U64(0),
Token::Str("playingPlayers"),
Token::U64(0),
Token::Str("uptime"),
Token::U64(18589),
Token::StructEnd,
],
);
}
}