diff --git a/echo-greeter/utilities/.gitignore b/echo-greeter/utilities/.gitignore new file mode 100644 index 0000000..bbccc88 --- /dev/null +++ b/echo-greeter/utilities/.gitignore @@ -0,0 +1,5 @@ +debug/ +target/ +Cargo.lock +**/*.bk +**/*.bak diff --git a/echo-greeter/utilities/Cargo.toml b/echo-greeter/utilities/Cargo.toml new file mode 100644 index 0000000..e248858 --- /dev/null +++ b/echo-greeter/utilities/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "utilities" +version = "0.1.0" +authors = ["boneyard93501 <4523011+boneyard93501@users.noreply.github.com>"] +edition = "2018" +description = "utilities, a Marine wasi module" +license = "Apache-2.0" + +[[bin]] +name = "utilities" +path = "src/main.rs" + +[dependencies] +marine-rs-sdk = { version="0.6.10", features=["logger"]} +log = "0.4.14" + +[dev-dependencies] +marine-rs-sdk-test = "0.1.11" + +[dev] +[profile.release] +opt-level = "s" diff --git a/echo-greeter/utilities/Config.toml b/echo-greeter/utilities/Config.toml new file mode 100644 index 0000000..5a4a824 --- /dev/null +++ b/echo-greeter/utilities/Config.toml @@ -0,0 +1,6 @@ +modules_dir = "artifacts/" + +[[module]] + name = "utilities" + mem_pages_count = 1 + logger_enabled = true diff --git a/echo-greeter/utilities/scripts/build.sh b/echo-greeter/utilities/scripts/build.sh new file mode 100755 index 0000000..0b992d7 --- /dev/null +++ b/echo-greeter/utilities/scripts/build.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash -o errexit -o nounset -o pipefail + +mkdir -p artifacts +rm -f artifacts/*.wasm +marine build --release +cp target/wasm32-wasi/release/utilities.wasm artifacts/ diff --git a/echo-greeter/utilities/src/main.rs b/echo-greeter/utilities/src/main.rs new file mode 100644 index 0000000..ca3ac04 --- /dev/null +++ b/echo-greeter/utilities/src/main.rs @@ -0,0 +1,151 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use marine_rs_sdk::{marine, module_manifest, WasmLoggerBuilder}; + +module_manifest!(); + +pub fn main() { + WasmLoggerBuilder::new().build().ok(); +} + +#[marine] +pub fn array_splitter_u64(array: Vec, chunk_size: u32) -> Vec> { + let mut my_vecs: Vec> = Vec::new(); + for chunk in array.chunks(chunk_size as usize) { + my_vecs.push(chunk.to_vec()); + } + my_vecs +} + +#[marine] +pub fn array_splitter_u32(array: Vec, chunk_size: u32) -> Vec> { + let mut my_vecs: Vec> = Vec::new(); + for chunk in array.chunks(chunk_size as usize) { + my_vecs.push(chunk.to_vec()); + } + my_vecs +} + +#[marine] +pub struct U64Result { + pub value: u64, + pub err_msg: String, +} + +#[marine] +pub struct U32Result { + pub value: u32, + pub err_msg: String, +} + +#[marine] +pub fn crement_u32(value: u32, step: u32, increment: bool) -> U32Result { + match increment { + true => U32Result { + value: value + step, + err_msg: "".to_string(), + }, + false => { + if value < step { + U32Result { + value: 0, + err_msg: format!("decrementing to negative value is not allowed"), + } + } else { + U32Result { + value: value - step, + err_msg: "".to_string(), + } + } + } + } +} + +#[marine] +pub fn crement_u64(value: u64, step: u64, increment: bool) -> U64Result { + match increment { + true => U64Result { + value: value + step, + err_msg: "".to_string(), + }, + false => { + if value < step { + U64Result { + value: 0, + err_msg: format!("decrementing to negative value is not allowed"), + } + } else { + U64Result { + value: value - step, + err_msg: "".to_string(), + } + } + } + } +} + +#[marine] +pub fn greeting(name: String) -> String { + format!("Hi, {}", name) +} + +#[cfg(test)] +mod tests { + use marine_rs_sdk_test::marine_test; + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")] + fn crement_u32_good() { + let value = 10u32; + let step = 2u32; + let res = utilities.crement_u32(value, step, false); + assert_eq!(res.value, 8); + + let value = 10u32; + let step = 2u32; + let res = utilities.crement_u32(value, step, true); + assert_eq!(res.value, 12); + } + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")] + fn crement_u32_fail() { + let value = 0u32; + let step = 2u32; + let res = utilities.crement_u32(value, step, false); + assert!(res.err_msg.len() > 0); + } + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")] + fn crement_u64_good() { + let value = 10u64; + let step = 2u64; + let res = utilities.crement_u64(value, step, false); + assert_eq!(res.value, 8); + + let value = 10u64; + let step = 2u64; + let res = utilities.crement_u64(value, step, true); + assert_eq!(res.value, 12); + } + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")] + fn crement_u64_fail() { + let value = 0u64; + let step = 2u64; + let res = utilities.crement_u64(value, step, false); + assert!(res.err_msg.len() > 0); + } +}