Skip to content

Commit

Permalink
mod: other
Browse files Browse the repository at this point in the history
  • Loading branch information
bennjii committed Sep 23, 2023
1 parent 4c847e9 commit 12dbaed
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 102 deletions.
Binary file modified .DS_Store
Binary file not shown.
37 changes: 25 additions & 12 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ 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"
web-sys = { version = "*", features = ["File", "FileReader", "FileSystem", "FileSystemDirectoryHandle", "FileSystemDirectoryReader"] }

clap = { version = "4.2.1", features = ["cargo"] }
open-stock = { version = "0.1.15", features = ["types"] }
csv = "1.1"
Expand All @@ -23,9 +25,7 @@ strum_macros = "0.24"
strsim = "0.8.0"
getrandom = { version = "0.2", features = ["js"] }
vfs = "0.9.0"
js-sys = "0.3.64"

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

[target.wasm32-wasi]
rustflags = ["-C", "link-arg=--export-table"]
crate-type = ["cdylib", "rlib"]
Binary file modified examples/.DS_Store
Binary file not shown.
Binary file modified pkg/odm.wasm
100755 → 100644
Binary file not shown.
94 changes: 10 additions & 84 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub mod parser;
use std::{
ffi::{c_char, CStr, CString},
fs::{self, DirEntry},
io,
path::Path,
};

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

pub type InlineDatabase = (
Vec<Product>,
Expand All @@ -17,6 +17,7 @@ pub type InlineDatabase = (
Vec<Kiosk>,
);

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

Expand Down Expand Up @@ -66,92 +67,21 @@ pub fn convert_from_directory(input: String) {
}
}

#[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) => {
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)
}
}
}

/// 🪵 Lays the [wasm] file log into a wasmfs.
#[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();

#[wasm_bindgen]
pub fn lay_file(file_id: String, file_content: String) -> String {
let raw_path = format!("/{}", file_id);
let path = Path::new(raw_path.as_str());

match fs::write(path, file_content) {
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(),
Ok(_) => "Written File.".to_string(),
Err(reason) => format!("Failed to write file. Reason: {}", reason),
}
}

/// 🥬 Leaks the viewable [wasm] directory for debugging purposes.
#[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();

#[wasm_bindgen]
pub fn leek_directory(dir: String) -> String {
let path = Path::new(dir.as_str());

let classifications = match traverse_directories(path, &classify_type) {
Expand All @@ -167,14 +97,10 @@ pub extern "C" fn leek_directory(dir_str: *const c_char) -> *mut c_char {
}
};

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

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

fn traverse_directories(
Expand Down
2 changes: 1 addition & 1 deletion src/parser/format/shopify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ impl Parsable<TransactionRecord> for Transaction {
customer_type: open_stock::CustomerType::Individual,
customer_id: customer.id,
},
kiosk: db.4[0].id.clone(),
kiosk: db.4.get(0).map_or("".to_owned(), |kiosk| kiosk.id.clone()),
transaction_type: open_stock::TransactionType::Out,
products: vec![],
order_total: cloned.total.clone().parse::<f32>().unwrap_or(0.0),
Expand Down

0 comments on commit 12dbaed

Please sign in to comment.