init utilities

This commit is contained in:
boneyard93501 2021-06-17 19:56:02 -05:00
parent b759aa746c
commit b6248ae332
5 changed files with 190 additions and 0 deletions

5
echo-greeter/utilities/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
debug/
target/
Cargo.lock
**/*.bk
**/*.bak

View File

@ -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"

View File

@ -0,0 +1,6 @@
modules_dir = "artifacts/"
[[module]]
name = "utilities"
mem_pages_count = 1
logger_enabled = true

View File

@ -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/

View File

@ -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<u64>, chunk_size: u32) -> Vec<Vec<u64>> {
let mut my_vecs: Vec<Vec<u64>> = 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<u32>, chunk_size: u32) -> Vec<Vec<u32>> {
let mut my_vecs: Vec<Vec<u32>> = 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);
}
}