From ad3a3bd1c59258076192c9aca559c9506020c442 Mon Sep 17 00:00:00 2001 From: Lucas Colombo Date: Fri, 21 Jun 2024 15:35:20 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20is=5F*=20functions=20?= =?UTF-8?q?tu=20Rustler=20for=20status?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit adds: - is_connecting - is_connected - is_disconnecting - is_disconnected --- lib/rustlers/rustler.rs | 80 +++++++++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/lib/rustlers/rustler.rs b/lib/rustlers/rustler.rs index 2e4b300..68ca85d 100644 --- a/lib/rustlers/rustler.rs +++ b/lib/rustlers/rustler.rs @@ -221,39 +221,88 @@ impl Ticker { pub trait RustlerAccessor { // #region fields g&s + + /// ๐ŸŽ ยป returns the name of the rustler fn name(&self) -> String; - fn static_name() -> String - where - Self: Sized; - + /// ๐ŸŽ ยป returns the [`RustlerStatus`] of the rustler fn status(&self) -> &RustlerStatus; + /// ๐ŸŽ ยป sets the [`RustlerStatus`] of the rustler fn set_status(&mut self, status: RustlerStatus) -> Result<()>; + /// ๐ŸŽ ยป returns `true` if the rustler's [`RustlerStatus`] is [RustlerStatus::Connecting] + fn is_connecting(&self) -> bool { + self.status() == &RustlerStatus::Connecting + } + /// ๐ŸŽ ยป returns `true` if the rustler's [`RustlerStatus`] is [RustlerStatus::Connected] + fn is_connected(&self) -> bool { + self.status() == &RustlerStatus::Connected + } + /// ๐ŸŽ ยป returns `true` if the rustler's [`RustlerStatus`] is [RustlerStatus::Disconnecting] + fn is_disconnecting(&self) -> bool { + self.status() == &RustlerStatus::Disconnecting + } + /// ๐ŸŽ ยป returns `true` if the rustler's [`RustlerStatus`] is [RustlerStatus::Disconnected] + fn is_disconnected(&self) -> bool { + self.status() == &RustlerStatus::Disconnected + } + /// ๐ŸŽ ยป returns `true` if the rustler's [`RustlerStatus`] is [RustlerStatus::Connected] or + /// [RustlerStatus::Connecting] + fn is_connected_or_connecting(&self) -> bool { + self.is_connected() || self.is_connecting() + } + /// ๐ŸŽ ยป returns `true` if the rustler's [`RustlerStatus`] is [RustlerStatus::Disconnected] or + /// [RustlerStatus::Disconnecting] + fn is_disconnected_or_disconnecting(&self) -> bool { + self.is_disconnected() || self.is_disconnecting() + } + + /// ๐ŸŽ ยป returns the next run time of the rustler fn next_run(&self) -> &DateTime; + /// ๐ŸŽ ยป sets the next run time of the rustler fn set_next_run(&mut self, next_run: DateTime); + /// ๐ŸŽ ยป returns the next stop time of the rustler fn next_stop(&self) -> &Option>; + //// ๐ŸŽ ยป sets the next stop time of the rustler fn set_next_stop(&mut self, next_stop: Option>); + /// ๐ŸŽ ยป returns the last run time of the rustler fn last_run(&self) -> &Option>; + /// ๐ŸŽ ยป sets the last run time of the rustler fn set_last_run(&mut self, last_run: Option>); + /// ๐ŸŽ ยป returns the last stop time of the rustler fn last_stop(&self) -> &Option>; + /// ๐ŸŽ ยป sets the last stop time of the rustler fn set_last_stop(&mut self, last_stop: Option>); + /// ๐ŸŽ ยป returns the last update time of the rustler fn last_update(&self) -> &Option>; + /// ๐ŸŽ ยป sets the last update time of the rustler fn set_last_update(&mut self, last_update: Option>); + /// ๐ŸŽ ยป returns the options of the rustler fn opts(&self) -> &RustlerOpts; + /// ๐ŸŽ ยป sets the options (see [`RustlerOpts`]) of the rustler fn set_opts(&mut self, opts: RustlerOpts); + /// ๐ŸŽ ยป returns the [`Ticker`]s of the rustler fn tickers(&self) -> &HashMap; + /// ๐ŸŽ ยป returns the [`Ticker`]s of the rustler as mutable fn tickers_mut(&mut self) -> &mut HashMap; + /// ๐ŸŽ ยป sets the [`Ticker`]s of the rustler fn set_tickers(&mut self, tickers: HashMap); + /// ๐ŸŽ ยป returns the message sender of the rustler + /// + /// the message sender is used to send messages back to the rustler service; if the message is + /// a [`RustlerMsg::QuoteMsg`] then the rustler will publish the quote to the bus (redis + /// probably) fn msg_sender(&self) -> &Option>; + /// ๐ŸŽ ยป returns the message sender of the rustler as mutable fn msg_sender_mut(&mut self) -> &mut Option>; + /// ๐ŸŽ ยป sets the message sender of the rustler fn set_msg_sender(&mut self, sender: Option>); // #endregion } @@ -327,7 +376,14 @@ pub trait Rustler: RustlerAccessor + Send + Sync { Ok(()) } - /// adds new tickers to the rustler + /// ๐ŸŽ ยป adds tickers to the rustler + /// + /// Will call the [`Rustler::on_add`] function, so that the implementation can decide what to do after + /// adding the tickers (e.g. sending a message to a websocket to start listening for quotes, + /// send an http request, etc.). + /// + /// Depending on the `connect_on_add` option in the rustler's options, the rustler will + /// call [`Rustler::connect`] if it is disconnected before calling [`Rustler::on_add`]. async fn add(&mut self, new_tickers: &Vec) -> Result<()> { let tickers = self.tickers_mut(); let mut added_tickers = vec![]; @@ -356,7 +412,14 @@ pub trait Rustler: RustlerAccessor + Send + Sync { Ok(()) } - /// deletes tickers from the rustler + /// ๐ŸŽ ยป deletes tickers from the rustler + /// + /// Will call the [`Rustler::on_delete`] function, so that the implementation can decide what to + /// do after deleting the tickers (e.g. sending a message to a websocket to stop listening for + /// quotes, etc.). + /// + /// If after deleting the tickers the tickers map is empty, the rustler will call + /// [`Rustler::disconnect`] to disconnect the rustler from the data source. async fn delete(&mut self, new_tickers: &Vec) -> Result<()> { let tickers = self.tickers_mut(); let mut removed_tickers = vec![]; @@ -384,7 +447,7 @@ pub trait Rustler: RustlerAccessor + Send + Sync { /// macro that expands to the accessor functions for a `Rustler` struct /// -/// for internal use +/// __intended for internal use only__ #[macro_export] macro_rules! rustler_accessors { ( @@ -393,9 +456,6 @@ macro_rules! rustler_accessors { fn name(&self) -> String { stringify!($name).to_string() } - fn static_name() -> String { - stringify!($name).to_string() - } fn status(&self) -> &$crate::rustlers::RustlerStatus { &self.status }