wick_host/
traits.rs

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
61
62
63
64
65
66
67
68
69
70
use wick_config::WickConfiguration;
use wick_interface_types::ComponentSignature;
use wick_packet::{Entity, Invocation, PacketStream, RuntimeConfig};
pub use wick_runtime::error::RuntimeError;

use crate::error::HostError;
use crate::{AppHost, ComponentHost};

#[async_trait::async_trait]
pub trait Host {
  fn namespace(&self) -> &str;
  fn get_signature(&self, path: Option<&[&str]>, entity: Option<&Entity>) -> Result<ComponentSignature, HostError>;
  async fn invoke(&self, invocation: Invocation, data: Option<RuntimeConfig>) -> Result<PacketStream, HostError>;
  async fn invoke_deep(
    &self,
    path: Option<&[&str]>,
    invocation: Invocation,
    data: Option<RuntimeConfig>,
  ) -> Result<PacketStream, HostError>;
  fn get_active_config(&self) -> WickConfiguration;
}

#[derive(Debug)]
#[allow(clippy::exhaustive_enums)]
pub enum WickHost {
  App(AppHost),
  Component(ComponentHost),
}

#[async_trait::async_trait]
impl Host for WickHost {
  fn namespace(&self) -> &str {
    match self {
      Self::App(h) => h.namespace(),
      Self::Component(h) => h.namespace(),
    }
  }

  fn get_signature(&self, path: Option<&[&str]>, entity: Option<&Entity>) -> Result<ComponentSignature, HostError> {
    match self {
      Self::App(h) => h.get_signature(path, entity),
      Self::Component(h) => h.get_signature(path, entity),
    }
  }

  async fn invoke(&self, invocation: Invocation, data: Option<RuntimeConfig>) -> Result<PacketStream, HostError> {
    match self {
      Self::App(h) => h.invoke(invocation, data).await,
      Self::Component(h) => h.invoke(invocation, data).await,
    }
  }
  async fn invoke_deep(
    &self,
    path: Option<&[&str]>,
    invocation: Invocation,
    data: Option<RuntimeConfig>,
  ) -> Result<PacketStream, HostError> {
    match self {
      Self::App(h) => h.invoke_deep(path, invocation, data).await,
      Self::Component(h) => h.invoke_deep(path, invocation, data).await,
    }
  }

  fn get_active_config(&self) -> WickConfiguration {
    match self {
      Self::App(h) => h.get_active_config(),
      Self::Component(h) => h.get_active_config(),
    }
  }
}