media_remote/high_level/
controller.rs

1use crate::{send_command, Command};
2
3macro_rules! send_command {
4    ($self:expr,$command:expr) => {{
5        if $self.is_info_some() {
6            send_command($command)
7        } else {
8            false
9        }
10    }};
11}
12
13pub trait Controller {
14    fn is_info_some(&self) -> bool;
15
16    /// Toggles between play and pause states.
17    ///
18    /// # Returns
19    /// - `true` if the command was successfully sent.
20    /// - `false` if the command failed.
21    ///
22    /// # Example
23    /// ```rust
24    /// use media_remote::prelude::*;
25    ///
26    /// let now_playing = NowPlaying::new();
27    /// now_playing.toggle();
28    /// ```
29    fn toggle(&self) -> bool {
30        send_command!(self, Command::TogglePlayPause)
31    }
32
33    /// Play the currently playing media.
34    ///
35    /// # Returns
36    /// - `true` if the command was successfully sent.
37    /// - `false` if the operation failed.
38    ///
39    /// # Example
40    /// ```rust
41    /// use media_remote::prelude::*;
42    ///
43    /// let now_playing = NowPlaying::new();
44    /// now_playing.play();
45    /// ```
46    fn play(&self) -> bool {
47        send_command!(self, Command::Play)
48    }
49
50    /// Pauses the currently playing media.
51    ///
52    /// # Returns
53    /// - `true` if the command was successfully sent.
54    /// - `false` if the command failed.
55    ///
56    /// # Example
57    /// ```rust
58    /// use media_remote::prelude::*;
59    ///
60    /// let now_playing = NowPlaying::new();
61    /// now_playing.pause();
62    /// ```
63    fn pause(&self) -> bool {
64        send_command!(self, Command::Pause)
65    }
66
67    /// Skips to the next track in the playback queue.
68    ///
69    /// # Returns
70    /// - `true` if the command was successfully sent.
71    /// - `false` if the command failed.
72    ///
73    /// # Example
74    /// ```rust
75    /// use media_remote::prelude::*;
76    ///
77    /// let now_playing = NowPlaying::new();
78    /// now_playing.next();
79    /// ```
80    fn next(&self) -> bool {
81        send_command!(self, Command::NextTrack)
82    }
83
84    /// Returns to the previous track in the playback queue.
85    ///
86    /// # Returns
87    /// - `true` if the command was successfully sent.
88    /// - `false` if the command failed.
89    ///
90    /// # Example
91    /// ```rust
92    /// use media_remote::prelude::*;
93    ///
94    /// let now_playing = NowPlaying::new();
95    /// now_playing.previous();
96    /// ```
97    fn previous(&self) -> bool {
98        send_command!(self, Command::PreviousTrack)
99    }
100}