object_transfer 1.2.3

An object transfer library for various message broker and/or protocols
Documentation
use ::std::sync::Arc;

use ::async_nats::jetstream::{
  consumer::pull::Config as PullConfig, stream::Config as StreamConfig,
};

/// Configuration options for creating an acknowledgment-based subscriber.
///
/// This struct provides a builder pattern for configuring NATS JetStream
/// consumers with pull-based message consumption and automatic acknowledgment options.
#[derive(Debug, Clone)]
pub struct SubFetcherOpt {
  pub(super) stream_cfg: StreamConfig,
  pub(super) pull_cfg: PullConfig,
}

impl SubFetcherOpt {
  /// Creates a new `SubFetcherOpt` with the specified format and name.
  ///
  /// # Arguments
  /// * `name` - The name for both the stream and consumer
  ///
  /// # Returns
  /// A new `SubFetcherOpt` instance with default settings and auto-acknowledgment enabled
  pub fn new(name: Arc<str>) -> Self {
    Self {
      stream_cfg: StreamConfig {
        name: name.clone().to_string(),
        ..Default::default()
      },
      pull_cfg: PullConfig {
        name: Some(name.clone().to_string()),
        ..PullConfig::default()
      },
    }
  }

  /// Sets the stream name.
  ///
  /// # Arguments
  /// * `name` - The name to assign to the stream
  ///
  /// # Returns
  /// Self for method chaining
  pub fn name(mut self, name: impl Into<String>) -> Self {
    self.stream_cfg.name = name.into();
    self
  }

  /// Sets the subjects that the stream should listen to.
  ///
  /// # Arguments
  /// * `subjects` - A vector of subject patterns to subscribe to
  ///
  /// # Returns
  /// Self for method chaining
  pub fn subjects(mut self, subjects: Vec<impl Into<String>>) -> Self {
    self.stream_cfg.subjects = subjects.into_iter().map(Into::into).collect();
    self
  }

  /// Sets the durable name for the consumer.
  ///
  /// A durable consumer will persist its state and can resume consumption
  /// after disconnection.
  ///
  /// # Arguments
  /// * `durable_name` - The durable name for the consumer
  ///
  /// # Returns
  /// Self for method chaining
  pub fn durable_name(mut self, durable_name: impl Into<String>) -> Self {
    self.pull_cfg.durable_name = Some(durable_name.into());
    self
  }

  /// Sets the complete stream configuration.
  ///
  /// This replaces the entire stream configuration with the provided one.
  ///
  /// # Arguments
  /// * `stream_cfg` - The stream configuration to use
  ///
  /// # Returns
  /// Self for method chaining
  pub fn stream_config(mut self, stream_cfg: StreamConfig) -> Self {
    self.stream_cfg = stream_cfg;
    self
  }

  /// Sets the complete pull consumer configuration.
  ///
  /// This replaces the entire pull consumer configuration with the provided one.
  ///
  /// # Arguments
  /// * `pull_cfg` - The pull consumer configuration to use
  ///
  /// # Returns
  /// Self for method chaining
  pub fn pull_config(mut self, pull_cfg: PullConfig) -> Self {
    self.pull_cfg = pull_cfg;
    self
  }
}