rcfe_core/
factory.rs

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