diff --git a/bindings/r/src/rust/src/io.rs b/bindings/r/src/rust/src/io.rs new file mode 100644 index 0000000..13d4c73 --- /dev/null +++ b/bindings/r/src/rust/src/io.rs @@ -0,0 +1,30 @@ +use extendr_api::prelude::*; + +use gtars::io::{read_tokens_from_gtok, write_tokens_to_gtok}; + +/// Write tokens to a gtok file +/// @export +/// @param filename A string representing the path to the gtok file. +#[extendr(r_name = "write_tokens_to_gtok")] +pub fn r_write_tokens_to_gtok(filename: String, tokens: Vec) { + let tokens: Vec = tokens.into_iter().map(|t| t as u32).collect(); + let _ = write_tokens_to_gtok(&filename, &tokens); +} + +/// Write tokens to a gtok file +/// @export +/// @param filename A string representing the path to the gtok file. +#[extendr(r_name = "read_tokens_from_gtok")] +pub fn r_read_tokens_from_gtok(filename: String) -> Vec { + read_tokens_from_gtok(&filename) + .unwrap() + .into_iter() + .map(|gtok| gtok as i32) + .collect() +} + +extendr_module! { + mod io; + fn r_read_tokens_from_gtok; + fn r_write_tokens_to_gtok; +} \ No newline at end of file diff --git a/bindings/r/src/rust/src/lib.rs b/bindings/r/src/rust/src/lib.rs index 2498955..77c03bc 100644 --- a/bindings/r/src/rust/src/lib.rs +++ b/bindings/r/src/rust/src/lib.rs @@ -1,41 +1,11 @@ use extendr_api::prelude::*; -use gtars::io::{read_tokens_from_gtok, write_tokens_to_gtok}; - -/// Return string `"Hello world!"` to R. -/// @export -#[extendr] -fn hello_world() -> &'static str { - "Hello world!" -} - -/// Write tokens to a gtok file -/// @export -/// @param filename A string representing the path to the gtok file. -#[extendr(r_name = "write_tokens_to_gtok")] -fn r_write_tokens_to_gtok(filename: String, tokens: Vec) { - let tokens: Vec = tokens.into_iter().map(|t| t as u32).collect(); - let _ = write_tokens_to_gtok(&filename, &tokens); -} - -/// Write tokens to a gtok file -/// @export -/// @param filename A string representing the path to the gtok file. -#[extendr(r_name = "read_tokens_from_gtok")] -fn r_read_tokens_from_gtok(filename: String) -> Vec { - read_tokens_from_gtok(&filename) - .unwrap() - .into_iter() - .map(|gtok| gtok as i32) - .collect() -} +pub mod io; // Macro to generate exports. // This ensures exported functions are registered with R. // See corresponding C code in `entrypoint.c`. extendr_module! { mod gtars; - fn hello_world; - fn r_write_tokens_to_gtok; - fn r_read_tokens_from_gtok; + use io; }