Update create_transaction with new fields
Also better interface, just use RawTransaction as a param. Rust is complaining that this isn't FFI safe though, so I need to figure out why.
This commit is contained in:
parent
7847b19c3e
commit
a5e3b402fa
16
bindings.h
16
bindings.h
@ -5,6 +5,9 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T = void>
|
||||||
|
struct Option;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct FFIResult {
|
struct FFIResult {
|
||||||
enum class Tag : uint8_t {
|
enum class Tag : uint8_t {
|
||||||
@ -96,10 +99,14 @@ struct RawShop {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct RawTransaction {
|
struct RawTransaction {
|
||||||
uint32_t id;
|
Option<uint32_t> id;
|
||||||
uint32_t shop_id;
|
uint32_t shop_id;
|
||||||
const char *mod_name;
|
const char *mod_name;
|
||||||
uint32_t local_form_id;
|
uint32_t local_form_id;
|
||||||
|
const char *name;
|
||||||
|
uint32_t form_type;
|
||||||
|
bool is_food;
|
||||||
|
uint32_t price;
|
||||||
bool is_sell;
|
bool is_sell;
|
||||||
uint32_t quantity;
|
uint32_t quantity;
|
||||||
uint32_t amount;
|
uint32_t amount;
|
||||||
@ -165,12 +172,7 @@ FFIResult<RawShop> create_shop(const char *api_url,
|
|||||||
|
|
||||||
FFIResult<RawTransaction> create_transaction(const char *api_url,
|
FFIResult<RawTransaction> create_transaction(const char *api_url,
|
||||||
const char *api_key,
|
const char *api_key,
|
||||||
uint32_t shop_id,
|
RawTransaction raw_transaction);
|
||||||
const char *mod_name,
|
|
||||||
uint32_t local_form_id,
|
|
||||||
bool is_sell,
|
|
||||||
uint32_t quantity,
|
|
||||||
uint32_t amount);
|
|
||||||
|
|
||||||
void free_string(char *ptr);
|
void free_string(char *ptr);
|
||||||
|
|
||||||
|
@ -20,6 +20,10 @@ pub struct Transaction {
|
|||||||
pub shop_id: u32,
|
pub shop_id: u32,
|
||||||
pub mod_name: String,
|
pub mod_name: String,
|
||||||
pub local_form_id: u32,
|
pub local_form_id: u32,
|
||||||
|
pub name: String,
|
||||||
|
pub form_type: u32,
|
||||||
|
pub is_food: bool,
|
||||||
|
pub price: u32,
|
||||||
pub is_sell: bool,
|
pub is_sell: bool,
|
||||||
pub quantity: u32,
|
pub quantity: u32,
|
||||||
pub amount: u32,
|
pub amount: u32,
|
||||||
@ -30,6 +34,10 @@ impl Transaction {
|
|||||||
shop_id: u32,
|
shop_id: u32,
|
||||||
mod_name: &str,
|
mod_name: &str,
|
||||||
local_form_id: u32,
|
local_form_id: u32,
|
||||||
|
name: &str,
|
||||||
|
form_type: u32,
|
||||||
|
is_food: bool,
|
||||||
|
price: u32,
|
||||||
is_sell: bool,
|
is_sell: bool,
|
||||||
quantity: u32,
|
quantity: u32,
|
||||||
amount: u32,
|
amount: u32,
|
||||||
@ -39,6 +47,10 @@ impl Transaction {
|
|||||||
shop_id,
|
shop_id,
|
||||||
mod_name: mod_name.to_string(),
|
mod_name: mod_name.to_string(),
|
||||||
local_form_id,
|
local_form_id,
|
||||||
|
name: name.to_string(),
|
||||||
|
form_type,
|
||||||
|
is_food,
|
||||||
|
price,
|
||||||
is_sell,
|
is_sell,
|
||||||
quantity,
|
quantity,
|
||||||
amount,
|
amount,
|
||||||
@ -49,12 +61,18 @@ impl Transaction {
|
|||||||
impl From<RawTransaction> for Transaction {
|
impl From<RawTransaction> for Transaction {
|
||||||
fn from(raw_transaction: RawTransaction) -> Self {
|
fn from(raw_transaction: RawTransaction) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: Some(raw_transaction.id),
|
id: raw_transaction.id,
|
||||||
shop_id: raw_transaction.shop_id,
|
shop_id: raw_transaction.shop_id,
|
||||||
mod_name: unsafe { CStr::from_ptr(raw_transaction.mod_name) }
|
mod_name: unsafe { CStr::from_ptr(raw_transaction.mod_name) }
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string(),
|
.to_string(),
|
||||||
local_form_id: raw_transaction.local_form_id,
|
local_form_id: raw_transaction.local_form_id,
|
||||||
|
name: unsafe { CStr::from_ptr(raw_transaction.name) }
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string(),
|
||||||
|
form_type: raw_transaction.form_type,
|
||||||
|
is_food: raw_transaction.is_food,
|
||||||
|
price: raw_transaction.price,
|
||||||
is_sell: raw_transaction.is_sell,
|
is_sell: raw_transaction.is_sell,
|
||||||
quantity: raw_transaction.quantity,
|
quantity: raw_transaction.quantity,
|
||||||
amount: raw_transaction.amount,
|
amount: raw_transaction.amount,
|
||||||
@ -65,33 +83,37 @@ impl From<RawTransaction> for Transaction {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct RawTransaction {
|
pub struct RawTransaction {
|
||||||
pub id: u32,
|
pub id: Option<u32>,
|
||||||
pub shop_id: u32,
|
pub shop_id: u32,
|
||||||
pub mod_name: *const c_char,
|
pub mod_name: *const c_char,
|
||||||
pub local_form_id: u32,
|
pub local_form_id: u32,
|
||||||
|
pub name: *const c_char,
|
||||||
|
pub form_type: u32,
|
||||||
|
pub is_food: bool,
|
||||||
|
pub price: u32,
|
||||||
pub is_sell: bool,
|
pub is_sell: bool,
|
||||||
pub quantity: u32,
|
pub quantity: u32,
|
||||||
pub amount: u32,
|
pub amount: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Transaction> for RawTransaction {
|
impl From<Transaction> for RawTransaction {
|
||||||
type Error = anyhow::Error;
|
fn from(transaction: Transaction) -> Self {
|
||||||
|
Self {
|
||||||
fn try_from(transaction: Transaction) -> Result<Self> {
|
id: transaction.id,
|
||||||
if let Some(id) = transaction.id {
|
shop_id: transaction.shop_id,
|
||||||
Ok(Self {
|
mod_name: CString::new(transaction.mod_name)
|
||||||
id,
|
.unwrap_or_default()
|
||||||
shop_id: transaction.shop_id,
|
.into_raw(),
|
||||||
mod_name: CString::new(transaction.mod_name)
|
local_form_id: transaction.local_form_id,
|
||||||
.unwrap_or_default()
|
name: CString::new(transaction.name)
|
||||||
.into_raw(),
|
.unwrap_or_default()
|
||||||
local_form_id: transaction.local_form_id,
|
.into_raw(),
|
||||||
is_sell: transaction.is_sell,
|
form_type: transaction.form_type,
|
||||||
quantity: transaction.quantity,
|
is_food: transaction.is_food,
|
||||||
amount: transaction.amount,
|
price: transaction.price,
|
||||||
})
|
is_sell: transaction.is_sell,
|
||||||
} else {
|
quantity: transaction.quantity,
|
||||||
Err(anyhow!("transaction.id is None"))
|
amount: transaction.amount,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,18 +130,11 @@ pub struct RawTransactionVec {
|
|||||||
pub extern "C" fn create_transaction(
|
pub extern "C" fn create_transaction(
|
||||||
api_url: *const c_char,
|
api_url: *const c_char,
|
||||||
api_key: *const c_char,
|
api_key: *const c_char,
|
||||||
shop_id: u32,
|
raw_transaction: RawTransaction,
|
||||||
mod_name: *const c_char,
|
|
||||||
local_form_id: u32,
|
|
||||||
is_sell: bool,
|
|
||||||
quantity: u32,
|
|
||||||
amount: u32,
|
|
||||||
) -> FFIResult<RawTransaction> {
|
) -> FFIResult<RawTransaction> {
|
||||||
let api_url = unsafe { CStr::from_ptr(api_url) }.to_string_lossy();
|
let api_url = unsafe { CStr::from_ptr(api_url) }.to_string_lossy();
|
||||||
let api_key = unsafe { CStr::from_ptr(api_key) }.to_string_lossy();
|
let api_key = unsafe { CStr::from_ptr(api_key) }.to_string_lossy();
|
||||||
let mod_name = unsafe { CStr::from_ptr(mod_name) }.to_string_lossy();
|
let transaction = Transaction::from(raw_transaction);
|
||||||
let transaction =
|
|
||||||
Transaction::from_game(shop_id, &mod_name, local_form_id, is_sell, quantity, amount);
|
|
||||||
info!(
|
info!(
|
||||||
"create_transaction api_url: {:?}, api_key: {:?}, transaction: {:?}",
|
"create_transaction api_url: {:?}, api_key: {:?}, transaction: {:?}",
|
||||||
api_url, api_key, transaction
|
api_url, api_key, transaction
|
||||||
@ -190,11 +205,15 @@ mod tests {
|
|||||||
r#"{
|
r#"{
|
||||||
"amount": 100,
|
"amount": 100,
|
||||||
"created_at": "2020-08-18T00:00:00.000",
|
"created_at": "2020-08-18T00:00:00.000",
|
||||||
|
"form_type": 41,
|
||||||
"id": 1,
|
"id": 1,
|
||||||
|
"is_food": false,
|
||||||
"is_sell": false,
|
"is_sell": false,
|
||||||
"local_form_id": 1,
|
"local_form_id": 1,
|
||||||
"mod_name": "Skyrim.esm",
|
"mod_name": "Skyrim.esm",
|
||||||
|
"name": "Item",
|
||||||
"owner_id": 1,
|
"owner_id": 1,
|
||||||
|
"price": 100,
|
||||||
"quantity": 1,
|
"quantity": 1,
|
||||||
"shop_id": 1,
|
"shop_id": 1,
|
||||||
"updated_at": "2020-08-18T00:00:00.000"
|
"updated_at": "2020-08-18T00:00:00.000"
|
||||||
@ -205,17 +224,38 @@ mod tests {
|
|||||||
let api_url = CString::new("url").unwrap().into_raw();
|
let api_url = CString::new("url").unwrap().into_raw();
|
||||||
let api_key = CString::new("api-key").unwrap().into_raw();
|
let api_key = CString::new("api-key").unwrap().into_raw();
|
||||||
let mod_name = CString::new("Skyrim.esm").unwrap().into_raw();
|
let mod_name = CString::new("Skyrim.esm").unwrap().into_raw();
|
||||||
let result = create_transaction(api_url, api_key, 1, mod_name, 1, false, 1, 100);
|
let name = CString::new("Item").unwrap().into_raw();
|
||||||
|
let raw_transaction = RawTransaction {
|
||||||
|
id: None,
|
||||||
|
shop_id: 1,
|
||||||
|
mod_name,
|
||||||
|
local_form_id: 1,
|
||||||
|
name,
|
||||||
|
form_type: 41,
|
||||||
|
is_food: false,
|
||||||
|
price: 100,
|
||||||
|
is_sell: false,
|
||||||
|
amount: 100,
|
||||||
|
quantity: 1,
|
||||||
|
};
|
||||||
|
let result = create_transaction(api_url, api_key, raw_transaction);
|
||||||
mock.assert();
|
mock.assert();
|
||||||
match result {
|
match result {
|
||||||
FFIResult::Ok(raw_transaction) => {
|
FFIResult::Ok(raw_transaction) => {
|
||||||
assert_eq!(raw_transaction.id, 1);
|
assert_eq!(raw_transaction.id, Some(1));
|
||||||
assert_eq!(raw_transaction.shop_id, 1);
|
assert_eq!(raw_transaction.shop_id, 1);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
unsafe { CStr::from_ptr(raw_transaction.mod_name).to_string_lossy() },
|
unsafe { CStr::from_ptr(raw_transaction.mod_name).to_string_lossy() },
|
||||||
"Skyrim.esm"
|
"Skyrim.esm"
|
||||||
);
|
);
|
||||||
assert_eq!(raw_transaction.local_form_id, 1);
|
assert_eq!(raw_transaction.local_form_id, 1);
|
||||||
|
assert_eq!(
|
||||||
|
unsafe { CStr::from_ptr(raw_transaction.name).to_string_lossy() },
|
||||||
|
"Item"
|
||||||
|
);
|
||||||
|
assert_eq!(raw_transaction.form_type, 41);
|
||||||
|
assert_eq!(raw_transaction.is_food, false);
|
||||||
|
assert_eq!(raw_transaction.price, 100);
|
||||||
assert_eq!(raw_transaction.is_sell, false);
|
assert_eq!(raw_transaction.is_sell, false);
|
||||||
assert_eq!(raw_transaction.quantity, 1);
|
assert_eq!(raw_transaction.quantity, 1);
|
||||||
assert_eq!(raw_transaction.amount, 100);
|
assert_eq!(raw_transaction.amount, 100);
|
||||||
@ -236,7 +276,21 @@ mod tests {
|
|||||||
let api_url = CString::new("url").unwrap().into_raw();
|
let api_url = CString::new("url").unwrap().into_raw();
|
||||||
let api_key = CString::new("api-key").unwrap().into_raw();
|
let api_key = CString::new("api-key").unwrap().into_raw();
|
||||||
let mod_name = CString::new("Skyrim.esm").unwrap().into_raw();
|
let mod_name = CString::new("Skyrim.esm").unwrap().into_raw();
|
||||||
let result = create_transaction(api_url, api_key, 1, mod_name, 1, false, 1, 100);
|
let name = CString::new("Item").unwrap().into_raw();
|
||||||
|
let raw_transaction = RawTransaction {
|
||||||
|
id: None,
|
||||||
|
shop_id: 1,
|
||||||
|
mod_name,
|
||||||
|
local_form_id: 1,
|
||||||
|
name,
|
||||||
|
form_type: 41,
|
||||||
|
is_food: false,
|
||||||
|
price: 100,
|
||||||
|
is_sell: false,
|
||||||
|
amount: 100,
|
||||||
|
quantity: 1,
|
||||||
|
};
|
||||||
|
let result = create_transaction(api_url, api_key, raw_transaction);
|
||||||
mock.assert();
|
mock.assert();
|
||||||
match result {
|
match result {
|
||||||
FFIResult::Ok(raw_transaction) => panic!(
|
FFIResult::Ok(raw_transaction) => panic!(
|
||||||
|
Loading…
Reference in New Issue
Block a user