rsiot/components/cmp_filesystem/tasks/
input.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
use tokio::fs::write;

use crate::{
    executor::CmpInOut,
    message::{MsgDataBound, ServiceBound},
};

use super::super::{config::FnInput, Error};

pub async fn input<TMsg, TService>(
    directory: String,
    config_fn_input: FnInput<TMsg>,
    mut in_out: CmpInOut<TMsg, TService>,
) -> super::Result<()>
where
    TMsg: MsgDataBound,
    TService: ServiceBound,
{
    while let Ok(msg) = in_out.recv_input().await {
        let name_and_content = (config_fn_input)(msg).map_err(Error::FnInput)?;
        let Some((name, content)) = name_and_content else {
            continue;
        };
        let name = format!("{directory}/{name}");
        write(&name, content).await.map_err(Error::WriteFileError)?;
    }
    Ok(())
}