aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Daniel López Azaña <daniloaz@gmail.com>2024-03-28 20:21:52 +0100
committerLibravatar Daniel López Azaña <daniloaz@gmail.com>2024-03-28 20:21:52 +0100
commit5c80b291fc776a7114572cebff898a7b3bf0671b (patch)
tree85f3f863f414a15773caa852aaf58a5e918910ae
parente1c0e90581b7ce97f87ddf43267ceb32e28447e3 (diff)
✨ (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 ✨ (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
-rw-r--r--src/lib.rs2
-rw-r--r--src/ls_client.rs (renamed from src/lightstreamer_client.rs)0
-rw-r--r--src/main.rs14
-rw-r--r--src/subscription.rs34
4 files changed, 32 insertions, 18 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 3ae6463..cf5cc4c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,7 +7,7 @@ pub mod item_update;
pub mod subscription_listener;
pub mod connection_details;
pub mod connection_options;
-pub mod lightstreamer_client;
+pub mod ls_client;
pub mod proxy;
pub mod subscription;
diff --git a/src/lightstreamer_client.rs b/src/ls_client.rs
index 214a722..214a722 100644
--- a/src/lightstreamer_client.rs
+++ b/src/ls_client.rs
diff --git a/src/main.rs b/src/main.rs
index 2dddb0d..5c2acc5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,10 +1,10 @@
-use crate::item_update::ItemUpdate;
-use crate::subscription::{Subscription, SubscriptionMode};
-use crate::subscription_listener::SubscriptionListener;
+use lightstreamer_client::item_update::ItemUpdate;
+use lightstreamer_client::ls_client::LightstreamerClient;
+use lightstreamer_client::subscription::{Snapshot, Subscription, SubscriptionMode};
+use lightstreamer_client::subscription_listener::SubscriptionListener;
use futures::stream::StreamExt;
use futures::SinkExt;
-use lightstreamer_client::lightstreamer_client::LightstreamerClient;
use reqwest::Client;
use serde_urlencoded;
use std::error::Error;
@@ -12,10 +12,6 @@ use std::sync::Arc;
use tokio::sync::Mutex;
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
-mod item_update;
-mod subscription;
-mod subscription_listener;
-
async fn establish_persistent_http_connection(
session_id_shared: Arc<Mutex<String>>,
) -> Result<(), reqwest::Error> {
@@ -225,6 +221,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
)?;
subscription.add_listener(Box::new(MySubscriptionListener {}));
+ subscription.set_data_adapter(Some(String::from("QUOTE_ADAPTER")))?;
+ subscription.set_requested_snapshot(Some(Snapshot::Yes))?;
let client = LightstreamerClient::new(
Some("http://push.lightstreamer.com/lightstreamer"),
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<f64>,
/// The snapshot delivery preferences to be requested to Lightstreamer Server for the items in the Subscription.
- requested_snapshot: Option<String>,
+ requested_snapshot: Option<Snapshot>,
/// The selector name for all the items in the Subscription, used as a filter on the updates received.
selector: Option<String>,
/// 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<String>) -> Result<(), String> {
+ pub fn set_requested_snapshot(&mut self, snapshot: Option<Snapshot>) -> 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::<usize>().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()
}