1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#[allow(unused_imports)]
use super::*;
use crate::SwarmBuilder;
use std::marker::PhantomData;
/// Represents the phase where a provider is not yet specified.
/// This is a marker type used in the type-state pattern to ensure compile-time checks of the builder's state.
pub enum NoProviderSpecified {}

// Define enums for each of the possible runtime environments. These are used as markers in the type-state pattern,
// allowing compile-time checks for the appropriate environment configuration.

#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
/// Represents the AsyncStd runtime environment.
pub enum AsyncStd {}

#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
/// Represents the Tokio runtime environment.
pub enum Tokio {}

#[cfg(feature = "wasm-bindgen")]
/// Represents the WasmBindgen environment for WebAssembly.
pub enum WasmBindgen {}

/// Represents a phase in the SwarmBuilder where a provider has been chosen but not yet specified.
pub struct ProviderPhase {}

impl SwarmBuilder<NoProviderSpecified, ProviderPhase> {
    /// Configures the SwarmBuilder to use the AsyncStd runtime.
    /// This method is only available when compiling for non-Wasm targets with the `async-std` feature enabled.
    #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
    pub fn with_async_std(self) -> SwarmBuilder<AsyncStd, TcpPhase> {
        SwarmBuilder {
            keypair: self.keypair,
            phantom: PhantomData,
            phase: TcpPhase {},
        }
    }

    /// Configures the SwarmBuilder to use the Tokio runtime.
    /// This method is only available when compiling for non-Wasm targets with the `tokio` feature enabled
    #[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
    pub fn with_tokio(self) -> SwarmBuilder<Tokio, TcpPhase> {
        SwarmBuilder {
            keypair: self.keypair,
            phantom: PhantomData,
            phase: TcpPhase {},
        }
    }

    /// Configures the SwarmBuilder for WebAssembly using WasmBindgen.
    /// This method is available when the `wasm-bindgen` feature is enabled.
    #[cfg(feature = "wasm-bindgen")]
    pub fn with_wasm_bindgen(self) -> SwarmBuilder<WasmBindgen, TcpPhase> {
        SwarmBuilder {
            keypair: self.keypair,
            phantom: PhantomData,
            phase: TcpPhase {},
        }
    }
}