Skip to content

Commit

Permalink
mod: savepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bennjii committed Sep 23, 2023
1 parent 68af538 commit 4c847e9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 18 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wasm-bindgen = "0.2.84"
# wasm-bindgen = "0.2.84"
clap = { version = "4.2.1", features = ["cargo"] }
open-stock = { version = "0.1.15", features = ["types"] }
csv = "1.1"
Expand All @@ -26,3 +26,6 @@ vfs = "0.9.0"

[lib]
crate-type = ["cdylib", "rlib"]

[target.wasm32-wasi]
rustflags = ["-C", "link-arg=--export-table"]
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cargo build --target wasm32-wasi --release
wasmtime target/wasm32-wasi/release/odm_migration_utility.wasm
cp target/wasm32-wasi/release/odm_migration_utility.wasm ./pkg/odm.wasm
Binary file added pkg/odm.wasm
Binary file not shown.
102 changes: 86 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
pub mod parser;
use std::{
ffi::{c_char, CStr, CString},
fs::{self, DirEntry},
io,
path::Path,
};

use vfs::MemoryFS;
use wasm_bindgen::prelude::*;

use open_stock::{Customer, Kiosk, Product, Store, Transaction};
pub use parser::*;

Expand All @@ -19,9 +17,61 @@ pub type InlineDatabase = (
Vec<Kiosk>,
);

#[wasm_bindgen]
pub fn convert_from_directory(folder: String) {
let path = Path::new(folder.as_str());
pub fn convert_from_directory(input: String) {
let path = Path::new(&input);

let classifications = match traverse_directories(path, &classify_type) {
Ok(mut v) => {
v.sort_by(|a, b| (a.variant as u32).cmp(&(b.variant as u32)));
v
}
Err(err) => {
panic!(
"[err]: Execution error in parsing files in provided directory, {}",
err
);
}
};

let mut db: InlineDatabase = (vec![], vec![], vec![], vec![], vec![]);

for c in classifications {
println!("{}", c);

match csv::Reader::from_path(c.path) {
Ok(rdr) => {
read_file(rdr, c.branding, c.variant, &mut db);
}
Err(error) => {
println!("{:?}", error)
}
}
}

match serde_json::to_string(&db) {
Ok(string_value) => {
// We're all good!
match fs::write("output.os", string_value) {
Ok(_) => {
println!("Converted all data. Thank you for using OpenPOS!")
}
Err(error) => {
println!("Failed to save data to file, {:?}", error)
}
}
}
Err(error) => {
println!("Failed to stringify data, {:?}", error)
}
}
}

#[no_mangle]
pub extern "C" fn c_convert_from_directory(input: *mut c_char) {
let pth = unsafe { CStr::from_ptr(input) }
.to_string_lossy()
.into_owned();
let path = Path::new(&pth);

let classifications = match traverse_directories(path, &classify_type) {
Ok(mut v) => {
Expand Down Expand Up @@ -69,23 +119,39 @@ pub fn convert_from_directory(folder: String) {
}
}

#[wasm_bindgen]
/// 🪵 Lays the [wasm] file log into a wasmfs.
pub fn lay_file(file_id: String, file_content: String) -> String {
#[no_mangle]
pub extern "C" fn lay_file(
file_id_str: *const c_char,
file_content_str: *const c_char,
) -> *mut c_char {
let file_id = unsafe { CStr::from_ptr(file_id_str) }
.to_string_lossy()
.into_owned();
let file_content = unsafe { CStr::from_ptr(file_content_str) }
.to_string_lossy()
.into_owned();

let raw_path = format!("/{}", file_id);
let path = Path::new(raw_path.as_str());

match fs::write(path, file_content) {
Ok(_) => format!("Written File."),
Err(reason) => {
format!("Failed to write file. Reason: {}", reason.to_string())
}
Ok(_) => CString::new("Written File.")
.expect("CString Conversion Failure")
.into_raw(),
Err(reason) => CString::new(format!("Failed to write file. Reason: {}", reason))
.expect("CString Conversion Failure")
.into_raw(),
}
}

#[wasm_bindgen]
/// 🥬 Leaks the viewable [wasm] directory for debugging purposes.
pub fn leek_directory(dir: String) -> String {
#[no_mangle]
pub extern "C" fn leek_directory(dir_str: *const c_char) -> *mut c_char {
let dir = unsafe { CStr::from_ptr(dir_str) }
.to_string_lossy()
.into_owned();

let path = Path::new(dir.as_str());

let classifications = match traverse_directories(path, &classify_type) {
Expand All @@ -101,10 +167,14 @@ pub fn leek_directory(dir: String) -> String {
}
};

classifications
let value: String = classifications
.into_iter()
.map(|classification| classification.to_string())
.collect()
.collect();

CString::new(value)
.expect("CString Conversion Failure")
.into_raw()
}

fn traverse_directories(
Expand Down

0 comments on commit 4c847e9

Please sign in to comment.