Skip to content

Commit

Permalink
Expose distribute_indexes_ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
Antti committed Mar 18, 2024
1 parent 1ddf2d6 commit e5753e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/distribute_indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,16 @@ mod tests {
vec![0, 3, 4, 1, 6, 5, 2, 7, 8, 10, 9, 12, 11, 13, 14]
);
}

#[test]
fn test_distribute2() {
let data = vec!["Picture", "Post", "Video", "Video", "Picture", "Post", "Picture", "Picture", "Video"];
let result = distribute_ids(&data, 3);
let result_with_labels = result
.iter()
.map(|idx| data[*idx])
.collect::<Vec<_>>();
assert_eq!(result_with_labels, vec!["Picture", "Post", "Video", "Picture", "Post", "Video", "Picture", "Video", "Picture"]);
assert_eq!(result, vec![0, 1, 2, 4, 5, 3, 6, 8, 7]);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ mod ruby_ext;

pub use distribute_csv::distribute as distribute_csv;
pub use distribute_indexes::distribute;
pub use distribute_indexes::distribute_ids;
pub use distributing_iterator::DistributingIterator;
15 changes: 9 additions & 6 deletions src/ruby_ext.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use magnus::{error::Result, function, exception, Error};
use magnus::{error::Result, exception, function, Error, Value};

use crate::distribute_csv;
use crate::{distribute_ids, distribute_csv};

fn distribute_csv_ruby(data: String, field: String, spread: u64) -> Result<String> {
match distribute_csv(&data, &field, spread) {
Ok(result) => Ok(result),
Err(e) => {
Err(Error::new(exception::standard_error(), format!("{:?}", e)))
}
Err(e) => Err(Error::new(exception::standard_error(), format!("{:?}", e))),
}
}

fn distribute_indexes_ruby(data: Vec<String>, spread: usize) -> Result<Vec<usize>> {
Ok(distribute_ids(&data, spread))
}

#[magnus::init]
fn init(ruby: &magnus::Ruby) -> Result<()> {
let module = ruby.define_module("DistributingIterator")?;
module.define_module_function("distribute", function!(distribute_csv_ruby, 3))?;
module.define_module_function("distribute_csv", function!(distribute_csv_ruby, 3))?;
module.define_module_function("distribute_indexes", function!(distribute_indexes_ruby, 2))?;
Ok(())
}

0 comments on commit e5753e9

Please sign in to comment.