Skip to content

Commit

Permalink
Add join, read_words, read_resource_words fns
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolaczk committed Aug 12, 2024
1 parent 327497e commit bd6e2ca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/scripting/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ pub fn hash_select(i: i64, collection: &[Value]) -> Value {
collection[(hash_inner(i) % collection.len() as i64) as usize].clone()
}

/// Joins all strings in vector with given separator
#[rune::function]
pub fn join(collection: &[Value], separator: &str) -> VmResult<String> {
let mut result = String::new();
let mut first = true;
for v in collection {
let v = vm_try!(v.clone().into_string());
if !first {
result.push_str(separator);
}
result.push_str(vm_try!(v.borrow_ref()).as_str());
first = false;
}
VmResult::Ok(result)
}

/// Reads a file into a string.
#[rune::function]
pub fn read_to_string(filename: &str) -> io::Result<String> {
Expand All @@ -171,6 +187,24 @@ pub fn read_lines(filename: &str) -> io::Result<Vec<String>> {
Ok(result)
}

/// Reads a file into a vector of words.
#[rune::function]
pub fn read_words(filename: &str) -> io::Result<Vec<String>> {
let file = File::open(filename)
.map_err(|e| io::Error::new(e.kind(), format!("Failed to open file {filename}: {e}")))?;
let buf = BufReader::new(file);
let mut result = Vec::new();
for line in buf.lines() {
let line = line?;
let words = line
.split(|c: char| !c.is_alphabetic())
.map(|s| s.to_string())
.filter(|s| !s.is_empty());
result.extend(words);
}
Ok(result)
}

/// Reads a resource file as a string.
fn read_resource_to_string_inner(path: &str) -> io::Result<String> {
let resource = Resources::get(path).ok_or_else(|| {
Expand All @@ -194,6 +228,14 @@ pub fn read_resource_lines(path: &str) -> io::Result<Vec<String>> {
.collect())
}

#[rune::function]
pub fn read_resource_words(path: &str) -> io::Result<Vec<String>> {
Ok(read_resource_to_string_inner(path)?
.split(|c: char| !c.is_alphabetic())
.map(|s| s.to_string())
.collect())
}

#[rune::function(instance)]
pub async fn prepare(mut ctx: Mut<Context>, key: Ref<str>, cql: Ref<str>) -> Result<(), CassError> {
ctx.prepare(&key, &cql).await
Expand Down
3 changes: 3 additions & 0 deletions src/scripting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn try_install(
latte_module.function_meta(functions::blob)?;
latte_module.function_meta(functions::text)?;
latte_module.function_meta(functions::vector)?;
latte_module.function_meta(functions::join)?;
latte_module.function_meta(functions::now_timestamp)?;
latte_module.function_meta(functions::hash)?;
latte_module.function_meta(functions::hash2)?;
Expand All @@ -68,8 +69,10 @@ fn try_install(
let mut fs_module = Module::with_crate("fs")?;
fs_module.function_meta(functions::read_to_string)?;
fs_module.function_meta(functions::read_lines)?;
fs_module.function_meta(functions::read_words)?;
fs_module.function_meta(functions::read_resource_to_string)?;
fs_module.function_meta(functions::read_resource_lines)?;
fs_module.function_meta(functions::read_resource_words)?;

rune_ctx.install(&context_module)?;
rune_ctx.install(&err_module)?;
Expand Down

0 comments on commit bd6e2ca

Please sign in to comment.