From 8ac43397bf56a660c226803b0c81bdc4c2288b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ko=C5=82aczkowski?= Date: Tue, 23 Jul 2024 16:21:19 +0200 Subject: [PATCH] Add join, read_words, read_resource_words fns --- src/scripting/functions.rs | 42 ++++++++++++++++++++++++++++++++++++++ src/scripting/mod.rs | 3 +++ 2 files changed, 45 insertions(+) diff --git a/src/scripting/functions.rs b/src/scripting/functions.rs index 9330a05..dbf5690 100644 --- a/src/scripting/functions.rs +++ b/src/scripting/functions.rs @@ -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 { + 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 { @@ -171,6 +187,24 @@ pub fn read_lines(filename: &str) -> io::Result> { Ok(result) } +/// Reads a file into a vector of words. +#[rune::function] +pub fn read_words(filename: &str) -> io::Result> { + 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 { let resource = Resources::get(path).ok_or_else(|| { @@ -194,6 +228,14 @@ pub fn read_resource_lines(path: &str) -> io::Result> { .collect()) } +#[rune::function] +pub fn read_resource_words(path: &str) -> io::Result> { + 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, key: Ref, cql: Ref) -> Result<(), CassError> { ctx.prepare(&key, &cql).await diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs index b9501c5..bc3a09a 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -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)?; @@ -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)?;