add some helper functions

This commit is contained in:
Sandipsinh Rathod 2024-10-12 14:33:42 -04:00
parent 545598d6e9
commit a9333ee087
No known key found for this signature in database
7 changed files with 2159 additions and 0 deletions

2089
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -4,3 +4,13 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
genai = { git = "https://github.com/laststylebender14/rust-genai.git", rev = "63a542ce20132503c520f4e07108e0d768f243c3", optional = true }
hyper = {version = "1.4.1", features = ["full"]}
pdf = "0.9.0"
async-trait = "0.1.83"
reqwest = "0.12.8"
anyhow = "1.0.89"
hyper-util = { version = "0.1", features = ["tokio"] }
http-body-util = "0.1.0"
bytes = "1.7.2"
tokio = "1.40.0"

17
src/io/fs.rs Normal file

@ -0,0 +1,17 @@
use bytes::Bytes;
use crate::target_rt::FileIO;
pub struct NativeFileIO {}
#[async_trait::async_trait]
impl FileIO for NativeFileIO {
async fn write<'a>(&'a self, path: &'a str, content: &'a Bytes) -> anyhow::Result<()> {
tokio::fs::write(path, content).await?;
Ok(())
}
async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<Bytes> {
let vec = tokio::fs::read(path).await?;
Ok(Bytes::from(vec))
}
}

16
src/io/http.rs Normal file

@ -0,0 +1,16 @@
use anyhow::anyhow;
use reqwest::{Client, Request};
use crate::target_rt::HttpIO;
pub struct NativeHttpIO {
client: Client,
}
#[async_trait::async_trait]
impl HttpIO for NativeHttpIO {
async fn execute(&self, request: Request) -> anyhow::Result<reqwest::Response> {
let resp = self.client.execute(request).await.map_err(|e| anyhow!("{}", e))?;
Ok(resp)
}
}

2
src/io/mod.rs Normal file

@ -0,0 +1,2 @@
pub mod fs;
mod http;

2
src/lib.rs Normal file

@ -0,0 +1,2 @@
pub mod target_rt;
pub mod io;

23
src/target_rt.rs Normal file

@ -0,0 +1,23 @@
use std::sync::Arc;
use bytes::Bytes;
use http_body_util::Full;
#[async_trait::async_trait]
pub trait HttpIO: Sync + Send + 'static {
async fn execute(
&self,
request: reqwest::Request,
) -> anyhow::Result<reqwest::Response>;
}
#[async_trait::async_trait]
pub trait FileIO: Send + Sync {
async fn write<'a>(&'a self, path: &'a str, content: &'a Bytes) -> anyhow::Result<()>;
async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<Bytes>;
}
pub struct TargetRuntime {
pub http: Arc<dyn HttpIO>,
pub fs: Arc<dyn FileIO>,
}