From 5c80b291fc776a7114572cebff898a7b3bf0671b Mon Sep 17 00:00:00 2001 From: Daniel López Azaña Date: Thu, 28 Mar 2024 20:21:52 +0100 Subject: ✨ (lib.rs): introduce `ls_client` module as a cleaner naming convention ✨ (ls_client.rs): add `LightstreamerClient` struct and associated methods for managing communication with Lightstreamer Server ✨ (main.rs): update imports to use new `ls_client` module and add data adapter and snapshot configuration to subscription ♻️ (lib.rs): refactor `lightstreamer_client` to `ls_client` for consistency with new module name 📝 (ls_client.rs): add comprehensive documentation for `LightstreamerClient` and its methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✨ (subscription.rs): add Snapshot enum to define snapshot delivery preferences ♻️ (subscription.rs): refactor requested_snapshot to use Snapshot enum for clarity 💡 (subscription.rs): update comments to reflect changes in snapshot handling --- src/subscription.rs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'src/subscription.rs') diff --git a/src/subscription.rs b/src/subscription.rs index f7e0d88..be10380 100644 --- a/src/subscription.rs +++ b/src/subscription.rs @@ -2,7 +2,16 @@ use crate::subscription_listener::SubscriptionListener; use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; -/// Enum representing the subscription mode +/// Enum representing the snapshot delivery preferences to be requested to Lightstreamer Server for the items in the Subscription. +#[derive(Debug)] +pub enum Snapshot { + Yes, + No, + Number(usize), + None, +} + +/// Enum representing the subscription mode. #[derive(Debug, PartialEq, Eq)] pub enum SubscriptionMode { Merge, @@ -49,7 +58,7 @@ pub struct Subscription { /// The maximum update frequency to be requested to Lightstreamer Server for all the items in the Subscription. requested_max_frequency: Option, /// The snapshot delivery preferences to be requested to Lightstreamer Server for the items in the Subscription. - requested_snapshot: Option, + requested_snapshot: Option, /// The selector name for all the items in the Subscription, used as a filter on the updates received. selector: Option, /// A list of SubscriptionListener instances that will receive events from this Subscription. @@ -618,15 +627,22 @@ impl Subscription { /// /// # See also /// `ItemUpdate.isSnapshot()` - pub fn set_requested_snapshot(&mut self, snapshot: Option) -> Result<(), String> { + pub fn set_requested_snapshot(&mut self, snapshot: Option) -> Result<(), String> { if self.is_active { return Err("Subscription is active".to_string()); } - if self.mode == SubscriptionMode::Raw && snapshot.is_some() { - return Err("Cannot request snapshot for Raw mode".to_string()); - } - if self.mode != SubscriptionMode::Distinct && snapshot.is_some() && snapshot.as_ref().unwrap().parse::().is_ok() { - return Err("Cannot specify snapshot length for non-Distinct mode".to_string()); + match snapshot { + Some(Snapshot::None) => { + if self.mode == SubscriptionMode::Raw { + return Err("Cannot request snapshot for Raw mode".to_string()); + } + } + Some(Snapshot::Number(_)) => { + if self.mode != SubscriptionMode::Distinct { + return Err("Cannot specify snapshot length for non-Distinct mode".to_string()); + } + } + _ => {} } self.requested_snapshot = snapshot; Ok(()) @@ -639,7 +655,7 @@ impl Subscription { /// /// # Returns /// "yes", "no", `None`, or an integer number. - pub fn get_requested_snapshot(&self) -> Option<&String> { + pub fn get_requested_snapshot(&self) -> Option<&Snapshot> { self.requested_snapshot.as_ref() } -- cgit v1.2.3