rsiot/components/cmp_filesystem/tasks/
output.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
use tokio::fs::{read, read_dir};
use tracing::warn;

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

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

pub async fn output<TMsg, TService>(
    directory: String,
    config_fn_output: FnOutput<TMsg>,
    in_out: CmpInOut<TMsg, TService>,
) -> super::Result<()>
where
    TMsg: MsgDataBound,
    TService: ServiceBound,
{
    // Читаем содержимое папки
    let mut reader = read_dir(directory).await.map_err(Error::CreateDirError)?;
    while let Some(entry) = reader
        .next_entry()
        .await
        .map_err(Error::ReadDirEntryError)?
    {
        let content = read(entry.path()).await.map_err(Error::ReadFileError)?;
        let content = String::from_utf8_lossy(&content);
        let msg = (config_fn_output)(&content);
        let msg = match msg {
            Ok(ok) => ok,
            Err(err) => {
                let err = err.to_string();
                warn!("File not load. File: {:?}. Error: {}", entry.path(), err);
                return Ok(());
            }
        };
        let Some(msg) = msg else { return Ok(()) };
        in_out.send_output(msg).await.map_err(Error::CmpOutput)?;
    }

    Ok(())
}