Skip to content

Commit

Permalink
Rust example (#406)
Browse files Browse the repository at this point in the history
* Create gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update test_language_bindings.yml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Create target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update test_language_bindings.yml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Create cargo.toml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Rename cargo.toml to Cargo.toml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update README.md

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Cargo.toml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Cargo.toml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Cargo.toml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Cargo.toml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Cargo.toml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Create build.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update test_language_bindings.yml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update test_language_bindings.yml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update test_language_bindings.yml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update test_language_bindings.yml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update test_language_bindings.yml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Cargo.toml

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update target.mk

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update gnoll_bindings.rs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

---------

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>
  • Loading branch information
ianfhunter committed Nov 27, 2023
1 parent 8ff47ec commit 969e809
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/test_language_bindings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- test_php
- test_R
- test_ruby
- test_rust
runs-on: ubuntu-latest
steps:
- name: NoOp
Expand Down Expand Up @@ -210,3 +211,14 @@ jobs:
DICE_PATH: src/PHP/
- name: make php
run: make php
test_rust:
name: "Test: Rust"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Rust
run: |
curl https://sh.rustup.rs -sSf | sh -s -- -y
- name: Run Rust
run: |
make rust
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ GNOLL was written to be the definitive solution to dice notation. The core has b
![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)
![R](https://img.shields.io/badge/r-%23276DC3.svg?style=for-the-badge&logo=r&logoColor=white)
![Ruby](https://img.shields.io/badge/ruby-%23CC342D.svg?style=for-the-badge&logo=ruby&logoColor=white)
![Rust](https://img.shields.io/badge/rust-%23000000.svg?style=for-the-badge&logo=rust&logoColor=white)

Primarily tested on Linux (Ubuntu), but functional in various forms on Windows (10, WSL) and Mac.

Expand Down
16 changes: 16 additions & 0 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "gnoll_example"
version = "1.0.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cty = "*"

[[bin]]
name = "example_app"
path = "gnoll_bindings.rs"

[build]
rustc-link-search = ["$PWD/../../build/"]
6 changes: 6 additions & 0 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
// Link with your external library
println!("cargo:rustc-link-lib=dice");
// You might need to specify the library path if it's not in standard locations
// println!("cargo:rustc-link-search=native=/path/to/library");
}
55 changes: 55 additions & 0 deletions src/rust/gnoll_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//use std::ffi::CStr;
use std::process;
use std::fs::File;
use std::io::{self, BufRead};

extern "C" {
pub fn roll_and_write(
die: *mut cty::c_char,
fp: *mut cty::c_char
);
}

fn main() {

println!("Example of GNOLL in rust...\n");

let die = "10d20\0".as_ptr() as *mut cty::c_char;
let fp = "output.txt\0".as_ptr() as *mut cty::c_char;

unsafe { roll_and_write(die, fp) }

// Read the result from the memory pointed to by fp
unsafe {
//let result_cstr = CStr::from_ptr(fp);
let file_path = "output.txt";
let file = File::open(file_path).unwrap();
let reader = io::BufReader::new(file);

// Read the first line
if let Some(Ok(first_line)) = reader.lines().next() {
// Use the first line as needed
let result_cstr = std::ffi::CString::new(first_line).unwrap();
// Rest of your code...

if let Ok(result_str) = result_cstr.to_str() {
let cleaned_string = result_str.replace(";", "");
if let Ok(result_num) = cleaned_string.parse::<u32>() {
if result_num > 1 {
println!("Result is: {}\n", result_num);
process::exit(0);
} else {
println!("Result is not greater than 1\n");
}
} else {
println!("Failed to parse the result '{}' as an integer\n", cleaned_string);
}
} else {
println!("Failed to convert CStr to str\n");
}
}
}
println!("Fatal.\n");
process::exit(1);

}
8 changes: 8 additions & 0 deletions src/rust/target.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: rust

rust: all
export LD_LIBRARY_PATH=$(shell pwd)/build/
export LIBRARY_PATH=$LD_LIBRARY_PATH
sudo cp $(shell pwd)/build/libdice.so /usr/local/lib/libdice.so
cd src/rust && cargo build
cd src/rust && LD_LIBRARY_PATH=$(shell pwd)/build/ cargo run --verbose

0 comments on commit 969e809

Please sign in to comment.