rcfe_core/
factory.rs

1use tonic::async_trait;
2use crate::{
3    error::Error,
4    options::client::ClientOptions,
5    client::Client
6};
7
8/// A factory trait for creating clients with specified options.
9/// This trait is asynchronous and can be implemented for different client types.
10/// # Examples
11/// ```rust
12/// use rcfe_core::{
13///     ClientFactory,
14///     ClientOptions,
15///     DefaultClientFactory,
16///     Error
17/// };
18///
19/// #[tokio::main]
20/// async fn main() -> Result<(), Error> {
21///     let factory = DefaultClientFactory;
22///     let options = ClientOptions::builder()
23///         .endpoints(vec!["http://localhost:2379"])
24///         .build();
25///
26///     let client = factory.create(options).await?;
27///     println!("Client created with options: {:?}", client.get_options());
28///
29///     Ok(())
30/// }
31/// ```
32#[async_trait]
33pub trait ClientFactory<T>
34where
35    T: Client,
36{
37    /// Asynchronously creates a client with the given options.
38    /// # Arguments
39    /// * `opts` - The client options to configure the client.
40    /// # Returns
41    /// * `Result<T, Error>` - The created client or an error if creation fails.
42    /// # Examples
43    /// ```rust
44    /// use rcfe_core::{
45    ///     ClientFactory,
46    ///     ClientOptions,
47    ///     DefaultClientFactory,
48    ///     Error
49    /// };
50    ///
51    /// #[tokio::main]
52    /// async fn main() -> Result<(), Error> {
53    ///     let factory = DefaultClientFactory;
54    ///     let options = ClientOptions::builder()
55    ///         .endpoints(vec!["http://localhost:2379"])
56    ///         .build();
57    ///
58    ///     let client = factory.create(options).await?;
59    ///     println!("Client created with options: {:?}", client.get_options());
60    ///     Ok(())
61    /// }
62    /// ```
63    /// # Errors
64    /// Returns an `Error` if the client creation fails.
65    async fn create(&self, opts: ClientOptions) -> Result<T, Error>;
66}
67