From b2041200200d5d9df6d98963e4de4128bd547272 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Thu, 6 Feb 2025 18:19:16 +0100 Subject: [PATCH 1/2] Test examples --- .gitignore | 1 + Cargo.toml | 9 ++++++ content/_index.md | 11 ++++++- src/main.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 6efa8bc..83ea235 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ _public/ **/tmp* **/target/ **/Cargo.lock +.env diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..681fd46 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "transformrs-org" +version = "0.1.0" +edition = "2021" + +[dependencies] +regex = "1.11.1" +tempfile = "3" +transformrs = "0.2.0" diff --git a/content/_index.md b/content/_index.md index b8c19e2..54914af 100644 --- a/content/_index.md +++ b/content/_index.md @@ -22,6 +22,14 @@ For example, for DeepInfra, set `DEEPINFRA_KEY` in `.env`: DEEPINFRA_KEY= ``` +and add the library to your `Cargo.toml`: + +```toml +[dependencies] +transformrs = "0.2.0" +tokio = { version = "1", features = ["rt-multi-thread", "macros"] } +``` + Then, you can use the API as follows. ### Chat Completion @@ -31,7 +39,8 @@ use transformrs::openai; use transformrs::Message; use transformrs::Provider; -fn main() { +#[tokio::main] +async fn main() { let messages = vec![ Message { role: "system".to_string(), diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f5a04b4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,74 @@ +use regex::Regex; +use std::fs; +use std::path::Path; +use std::process::Command; + +fn code_blocks(content: &str, lang: &str) -> Vec { + let re = Regex::new(&format!(r"```{lang}\n([\s\S]*?)```")).unwrap(); + + re.captures_iter(&content) + .filter_map(|cap| cap.get(1).map(|code| code.as_str().to_string())) + .collect() +} + +fn create_project(dir: &Path, dependencies: &str, code: &str) { + let project = format!( + r#" + [package] + name = "transformrs-org-test" + version = "0.1.0" + edition = "2021" + + {dependencies} + "# + ); + fs::write(dir.join("Cargo.toml"), project).unwrap(); + + fs::create_dir_all(dir.join("src")).unwrap(); + fs::write(dir.join("src/main.rs"), code).unwrap(); +} + +fn copy_env(dir: &Path) { + let env = fs::read_to_string(".env").unwrap(); + fs::write(dir.join(".env"), env).unwrap(); +} + +fn run_project(dir: &Path) { + // list everything in dir + println!("Listing everything in dir: {:?}", dir); + let entries = fs::read_dir(dir).unwrap(); + for entry in entries { + println!("Entry: {:?}", entry.unwrap().path()); + } + + let _output = Command::new("cargo") + .arg("build") + .current_dir(dir) + .output() + .expect("Failed to run cargo build"); + + let output = Command::new("cargo") + .arg("run") + .current_dir(dir) + .output() + .expect("Failed to run cargo run"); + + println!("Output: {:?}", String::from_utf8(output.stdout).unwrap()); +} + +fn main() { + // Read the markdown file + let content = + fs::read_to_string("content/_index.md").expect("Failed to read content/_index.md"); + + let dependencies = code_blocks(&content, "toml").first().unwrap().clone(); + let code_blocks = code_blocks(&content, "rust"); + + let tmp_dir = tempfile::tempdir().unwrap(); + let tmp_dir_path = tmp_dir.path(); + println!("Tmp dir path: {:?}", tmp_dir_path); + create_project(tmp_dir_path, &dependencies, code_blocks.first().unwrap()); + copy_env(tmp_dir_path); + run_project(tmp_dir_path); + drop(tmp_dir); +} From 7e9ac00340405a6f57953bb402e0b89aa4a96a84 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Thu, 6 Feb 2025 18:22:18 +0100 Subject: [PATCH 2/2] Add test --- .github/workflows/ci.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2b686b2..096cdcf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,6 +19,13 @@ jobs: with: tool: zola@0.19.2 + - run: cargo build + + - run: | + echo "DEEPINFRA_KEY=${{ secrets.DEEPINFRA_KEY }}" > .env + + - run: cargo run + - run: zola build - uses: cloudflare/wrangler-action@v3