Skip to main content

hermes_tokio_runtime_components/impls/fs/
read_file.rs

1use std::io::Error as IoError;
2use std::path::Path;
3
4use cgp::prelude::*;
5use hermes_runtime_components::traits::fs::file_path::HasFilePathType;
6use hermes_runtime_components::traits::fs::read_file::FileAsStringReader;
7use tokio::fs::read_to_string;
8
9pub struct TokioReadFileAsString;
10
11impl<Runtime> FileAsStringReader<Runtime> for TokioReadFileAsString
12where
13    Runtime: HasFilePathType + CanRaiseError<IoError>,
14    Runtime::FilePath: AsRef<Path>,
15{
16    async fn read_file_as_string(
17        _runtime: &Runtime,
18        file_path: &Runtime::FilePath,
19    ) -> Result<String, Runtime::Error> {
20        let content = read_to_string(file_path)
21            .await
22            .map_err(Runtime::raise_error)?;
23
24        Ok(content)
25    }
26}