1use anyhow::Result;
2use async_trait::async_trait;
3use crb::agent::{Address, Agent, AgentSession, Context};
4use crb::superagent::{OnRequest, Request};
5use serde::de::DeserializeOwned;
6use std::marker::PhantomData;
7use std::path::PathBuf;
8use tokio::fs;
9use toml::Value;
10
11pub trait Config: DeserializeOwned + Send + 'static {
12 }
14
15impl<T> Config for T where T: DeserializeOwned + Send + 'static {}
16
17pub struct ConfigAgent {}
18
19impl Agent for ConfigAgent {
20 type Context = AgentSession<Self>;
21 type Link = Address<Self>;
22}
23
24pub struct ReadConfig<T = Value> {
25 pub path: PathBuf,
26 pub _type: PhantomData<T>,
27}
28
29impl<T: Config> Request for ReadConfig<T> {
30 type Response = T;
31}
32
33#[async_trait]
34impl<T: Config> OnRequest<ReadConfig<T>> for ConfigAgent {
35 async fn on_request(&mut self, msg: ReadConfig<T>, _ctx: &mut Context<Self>) -> Result<T> {
36 let content = fs::read_to_string(&msg.path).await?;
37 let config = toml::from_str(&content)?;
39 Ok(config)
40 }
41}