Skip to main content

agp_config/
component.rs

1// Copyright AGNTCY Contributors (https://github.com/agntcy)
2// SPDX-License-Identifier: Apache-2.0
3
4use thiserror::Error;
5
6pub mod configuration;
7pub mod id;
8
9// Define the trait for the base component
10#[derive(Error, Debug)]
11pub enum ComponentError {
12    #[error("configuration error {0}")]
13    ConfigError(String),
14    #[error("runtime error: {0}")]
15    RuntimeError(String),
16    #[error("unknown error")]
17    Unknown,
18}
19
20pub trait Component {
21    // Get name of the component
22    fn identifier(&self) -> &id::ID;
23
24    // start the component
25    #[allow(async_fn_in_trait)]
26    async fn start(&mut self) -> Result<(), ComponentError>;
27}
28
29pub trait ComponentBuilder {
30    // Associated types
31    type Config;
32    type Component: Component;
33
34    /// ID of the component
35    fn kind(&self) -> id::Kind;
36
37    /// Build the component
38    fn build(&self, name: String) -> Result<Self::Component, ComponentError>;
39
40    /// Build the component with configuration
41    fn build_with_config(
42        &self,
43        name: &str,
44        config: &Self::Config,
45    ) -> Result<Self::Component, ComponentError>;
46}